Java Code Examples for com.google.gson.stream.JsonToken#NAME

The following examples show how to use com.google.gson.stream.JsonToken#NAME . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BsonReader.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Override
public JsonToken peek() throws IOException {
  switch (state()) {
  case INITIAL:
  case SCOPE_DOCUMENT:
  case TYPE:
    advance();
    return peek();
  case NAME:
    return JsonToken.NAME;
  case END_OF_DOCUMENT:
    return JsonToken.END_OBJECT;
  case END_OF_ARRAY:
    return JsonToken.END_ARRAY;
  case DONE:
    return JsonToken.END_DOCUMENT;
  case VALUE:
    return toGsonToken(delegate.getCurrentBsonType());
  default:
    throw new IllegalStateException("Unexpected state: " + state() + " currentType:" +
        delegate.getCurrentBsonType());
  }
}
 
Example 2
Source File: UsersStreamDeserializer.java    From java-json-benchmark with MIT License 6 votes vote down vote up
@Override
public Users gson(JsonReader reader) throws IOException {
    Users uc = new Users();
    reader.beginObject();

    JsonToken token;
    while ((token = reader.peek()) != JsonToken.END_OBJECT) {
        if (token == JsonToken.NAME) {
            String fieldname = reader.nextName();
            if ("users".equals(fieldname)) {
                uc.users = new ArrayList<>();
                reader.beginArray();
                while (reader.peek() != JsonToken.END_ARRAY) {
                    uc.users.add(gsonUser(reader));
                }
                reader.endArray();
            }
        }
    }
    reader.endObject();
    return uc;
}
 
Example 3
Source File: JsonTreeReader.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override public void skipValue() throws IOException {
  if (peek() == JsonToken.NAME) {
    nextName();
    pathNames[stackSize - 2] = "null";
  } else {
    popStack();
    if (stackSize > 0) {
      pathNames[stackSize - 1] = "null";
    }
  }
  if (stackSize > 0) {
    pathIndices[stackSize - 1]++;
  }
}
 
Example 4
Source File: JsonUtils.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
public static int startJson(final JsonReader reader) throws EntityProviderException {
  //The enclosing "d" and "results" are optional - so we cannot check for the presence
  //but we have to read over them in case they are present.
  JsonToken token;
  try {
    token = reader.peek();
    int openJsonObjects = 0;
    if (JsonToken.BEGIN_OBJECT == token) {
      reader.beginObject();
      openJsonObjects++;
      token = reader.peek();
      if (JsonToken.NAME == token) {
        String name = reader.nextName();
        if (!("d".equals(name) ^ "results".equals(name))) {
          //TODO I18N
          throw new EntityProviderException(EntityProviderException.COMMON, name + " not expected, only d or results");
        }
      }

      token = reader.peek();
      if (JsonToken.BEGIN_OBJECT == token) {
        reader.beginObject();
        openJsonObjects++;
      } else if (JsonToken.BEGIN_ARRAY == token) {
        //TODO I18N
        throw new EntityProviderException(EntityProviderException.COMMON, "Array not expected");
      }
    }

    return openJsonObjects;
  } catch (IOException e) {
    //TODO I18N
    throw new EntityProviderException(EntityProviderException.COMMON, e);
  }
}
 
Example 5
Source File: JsonParserReader.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static JsonToken toGsonToken(com.fasterxml.jackson.core.JsonToken token) {
  switch (token) {
  case START_ARRAY:
    return JsonToken.BEGIN_ARRAY;
  case END_ARRAY:
    return JsonToken.END_ARRAY;
  case START_OBJECT:
    return JsonToken.BEGIN_OBJECT;
  case END_OBJECT:
    return JsonToken.END_OBJECT;
  case FIELD_NAME:
    return JsonToken.NAME;
  case VALUE_FALSE:
    return JsonToken.BOOLEAN;
  case VALUE_TRUE:
    return JsonToken.BOOLEAN;
  case VALUE_NULL:
    return JsonToken.NULL;
  case VALUE_NUMBER_INT:
    return JsonToken.NUMBER;
  case VALUE_NUMBER_FLOAT:
    return JsonToken.NUMBER;
  case VALUE_STRING:
  case VALUE_EMBEDDED_OBJECT:
    return JsonToken.STRING;
  default: // Not semantically equivalent
    return JsonToken.NULL;
  }
}
 
Example 6
Source File: JsonTreeReader.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public void skipValue() throws IOException {
  if (peek() == JsonToken.NAME) {
    nextName();
  } else {
    popStack();
  }
}
 
Example 7
Source File: JsonErrorDocumentDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param reader
 * @return String
 * @throws IOException
 */
private String readJson(final JsonReader reader) throws IOException {
  StringBuilder sb = new StringBuilder();

  while (reader.hasNext()) {
    JsonToken token = reader.peek();
    if (token == JsonToken.NAME) {
      if (sb.length() > 0) {
        sb.append(",");
      }
      String name = reader.nextName();
      sb.append("\"").append(name).append("\"").append(":");
    } else if (token == JsonToken.BEGIN_OBJECT) {
      reader.beginObject();
      sb.append("{")
          .append(readJson(reader))
          .append("}");
      reader.endObject();
    } else if (token == JsonToken.BEGIN_ARRAY) {
      reader.beginArray();
      sb.append("[")
          .append(readJson(reader))
          .append("]");
      reader.endArray();
    } else {
      sb.append("\"")
          .append(reader.nextString())
          .append("\"");
    }
  }

  return sb.toString();
}
 
Example 8
Source File: JsonUtils.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public static int startJson(final JsonReader reader) throws EntityProviderException {
  // The enclosing "d" and "results" are optional - so we cannot check for the presence
  // but we have to read over them in case they are present.
  JsonToken token;
  try {
    token = reader.peek();
    int openJsonObjects = 0;
    if (JsonToken.BEGIN_OBJECT == token) {
      reader.beginObject();
      openJsonObjects++;
      token = reader.peek();
      if (JsonToken.NAME == token) {
        String name = reader.nextName();
        if (!("d".equals(name) ^ "results".equals(name))) {
          // TODO I18N
          throw new EntityProviderException(EntityProviderException.COMMON, name +
              " not expected, only d or results");
        }
      }

      token = reader.peek();
      if (JsonToken.BEGIN_OBJECT == token) {
        reader.beginObject();
        openJsonObjects++;
      } else if (JsonToken.BEGIN_ARRAY == token) {
        // TODO I18N
        throw new EntityProviderException(EntityProviderException.COMMON, "Array not expected");
      }
    }

    return openJsonObjects;
  } catch (IOException e) {
    // TODO I18N
    throw new EntityProviderException(EntityProviderException.COMMON, e);
  }
}
 
Example 9
Source File: JsonErrorDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private String readJson(final JsonReader reader) throws IOException {
  StringBuilder sb = new StringBuilder();

  while (reader.hasNext()) {
    JsonToken token = reader.peek();
    if (token == JsonToken.NAME) {
      if (sb.length() > 0) {
        sb.append(",");
      }
      String name = reader.nextName();
      sb.append("\"").append(name).append("\"").append(":");
    } else if (token == JsonToken.BEGIN_OBJECT) {
      reader.beginObject();
      sb.append("{")
          .append(readJson(reader))
          .append("}");
      reader.endObject();
    } else if (token == JsonToken.BEGIN_ARRAY) {
      reader.beginArray();
      sb.append("[")
          .append(readJson(reader))
          .append("]");
      reader.endArray();
    } else {
      sb.append("\"")
          .append(reader.nextString())
          .append("\"");
    }
  }

  return sb.toString();
}
 
Example 10
Source File: JsonTreeReader.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public void skipValue()
{
    if (peek() == JsonToken.NAME)
    {
        nextName();
        return;
    } else
    {
        b();
        return;
    }
}
 
Example 11
Source File: PolymorphicTypeFields.java    From rockscript with Apache License 2.0 5 votes vote down vote up
public void read(JsonReader in, Object bean) throws Exception {
  while (in.peek() == JsonToken.NAME) {
    String fieldName = in.nextName();
    PolymorphicField field = polymorphicFields.get(fieldName);
    // ignore non-existing fields
    if (field!=null) {
      field.read(in, bean);
    }
  }
}
 
Example 12
Source File: JsonTreeReader.java    From letv with Apache License 2.0 5 votes vote down vote up
public void skipValue() throws IOException {
    if (peek() == JsonToken.NAME) {
        nextName();
    } else {
        popStack();
    }
}
 
Example 13
Source File: IpcdJsonReader.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the type of the next token without consuming it.
 */
public JsonToken peek() throws IOException {
   int p = peeked;
   if (p == PEEKED_NONE) {
      p = doPeek();
   }

   switch (p) {
      case PEEKED_BEGIN_OBJECT:
         return JsonToken.BEGIN_OBJECT;
      case PEEKED_END_OBJECT:
         return JsonToken.END_OBJECT;
      case PEEKED_BEGIN_ARRAY:
         return JsonToken.BEGIN_ARRAY;
      case PEEKED_END_ARRAY:
         return JsonToken.END_ARRAY;
      case PEEKED_SINGLE_QUOTED_NAME:
      case PEEKED_DOUBLE_QUOTED_NAME:
      case PEEKED_UNQUOTED_NAME:
         return JsonToken.NAME;
      case PEEKED_TRUE:
      case PEEKED_FALSE:
         return JsonToken.BOOLEAN;
      case PEEKED_NULL:
         return JsonToken.NULL;
      case PEEKED_SINGLE_QUOTED:
      case PEEKED_DOUBLE_QUOTED:
      case PEEKED_UNQUOTED:
      case PEEKED_BUFFERED:
         return JsonToken.STRING;
      case PEEKED_LONG:
      case PEEKED_NUMBER:
         return JsonToken.NUMBER;
      case PEEKED_EOF:
         return JsonToken.END_DOCUMENT;
      default:
         throw new AssertionError();
   }
}
 
Example 14
Source File: MessageBodyTypeAdapter.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public MessageBody read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
      in.nextNull();
      return null;
   }
   
   String messageType = null;
   ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();

   in.beginObject();
   while (in.hasNext()) {
      switch (in.nextName()) {
      case ATTR_MESSAGE_TYPE:
         if (in.peek() != JsonToken.NULL) {
            messageType = in.nextString();
         } else {
            in.nextNull();
         }
         break;
         
      case ATTR_ATTRIBUTES:
         if (in.peek() != JsonToken.NULL) {
            in.beginObject();
            while(in.peek() == JsonToken.NAME) {
            	String name = in.nextName();
            	if(in.peek() != JsonToken.NULL) {
            		builder.put(name, valueAdapter.read(in));
            	}
            	else {
            		in.nextNull();
            	}
            }
            in.endObject();
         } else {
            in.nextNull();
         }
         break;
         
      default:
         // ignore extra fields
     	   in.skipValue();
         break;
      }
   }
   in.endObject();

   return MessageBody.buildMessage(messageType, builder.build());
}
 
Example 15
Source File: JsonTreeReader.java    From gson with Apache License 2.0 4 votes vote down vote up
@Override public JsonToken peek() throws IOException {
  if (stackSize == 0) {
    return JsonToken.END_DOCUMENT;
  }

  Object o = peekStack();
  if (o instanceof Iterator) {
    boolean isObject = stack[stackSize - 2] instanceof JsonObject;
    Iterator<?> iterator = (Iterator<?>) o;
    if (iterator.hasNext()) {
      if (isObject) {
        return JsonToken.NAME;
      } else {
        push(iterator.next());
        return peek();
      }
    } else {
      return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
    }
  } else if (o instanceof JsonObject) {
    return JsonToken.BEGIN_OBJECT;
  } else if (o instanceof JsonArray) {
    return JsonToken.BEGIN_ARRAY;
  } else if (o instanceof JsonPrimitive) {
    JsonPrimitive primitive = (JsonPrimitive) o;
    if (primitive.isString()) {
      return JsonToken.STRING;
    } else if (primitive.isBoolean()) {
      return JsonToken.BOOLEAN;
    } else if (primitive.isNumber()) {
      return JsonToken.NUMBER;
    } else {
      throw new AssertionError();
    }
  } else if (o instanceof JsonNull) {
    return JsonToken.NULL;
  } else if (o == SENTINEL_CLOSED) {
    throw new IllegalStateException("JsonReader is closed");
  } else {
    throw new AssertionError();
  }
}
 
Example 16
Source File: JsonTreeReader.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public JsonToken peek()
{
    if (c.isEmpty())
    {
        return JsonToken.END_DOCUMENT;
    }
    Object obj = a();
    if (obj instanceof Iterator)
    {
        boolean flag = c.get(-2 + c.size()) instanceof JsonObject;
        Iterator iterator = (Iterator)obj;
        if (iterator.hasNext())
        {
            if (flag)
            {
                return JsonToken.NAME;
            } else
            {
                c.add(iterator.next());
                return peek();
            }
        }
        if (flag)
        {
            return JsonToken.END_OBJECT;
        } else
        {
            return JsonToken.END_ARRAY;
        }
    }
    if (obj instanceof JsonObject)
    {
        return JsonToken.BEGIN_OBJECT;
    }
    if (obj instanceof JsonArray)
    {
        return JsonToken.BEGIN_ARRAY;
    }
    if (obj instanceof JsonPrimitive)
    {
        JsonPrimitive jsonprimitive = (JsonPrimitive)obj;
        if (jsonprimitive.isString())
        {
            return JsonToken.STRING;
        }
        if (jsonprimitive.isBoolean())
        {
            return JsonToken.BOOLEAN;
        }
        if (jsonprimitive.isNumber())
        {
            return JsonToken.NUMBER;
        } else
        {
            throw new AssertionError();
        }
    }
    if (obj instanceof JsonNull)
    {
        return JsonToken.NULL;
    }
    if (obj == b)
    {
        throw new IllegalStateException("JsonReader is closed");
    } else
    {
        throw new AssertionError();
    }
}
 
Example 17
Source File: JsonTreeReader.java    From framework with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override public JsonToken peek() throws IOException {
  if (stack.isEmpty()) {
    return JsonToken.END_DOCUMENT;
  }

  Object o = peekStack();
  if (o instanceof Iterator) {
    boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
    Iterator<?> iterator = (Iterator<?>) o;
    if (iterator.hasNext()) {
      if (isObject) {
        return JsonToken.NAME;
      } else {
        stack.add(iterator.next());
        return peek();
      }
    } else {
      return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
    }
  } else if (o instanceof JsonObject) {
    return JsonToken.BEGIN_OBJECT;
  } else if (o instanceof JsonArray) {
    return JsonToken.BEGIN_ARRAY;
  } else if (o instanceof JsonPrimitive) {
    JsonPrimitive primitive = (JsonPrimitive) o;
    if (primitive.isString()) {
      return JsonToken.STRING;
    } else if (primitive.isBoolean()) {
      return JsonToken.BOOLEAN;
    } else if (primitive.isNumber()) {
      return JsonToken.NUMBER;
    } else {
      throw new AssertionError();
    }
  } else if (o instanceof JsonNull) {
    return JsonToken.NULL;
  } else if (o == SENTINEL_CLOSED) {
    throw new IllegalStateException("JsonReader is closed");
  } else {
    throw new AssertionError();
  }
}
 
Example 18
Source File: JsonTreeReader.java    From letv with Apache License 2.0 4 votes vote down vote up
public JsonToken peek() throws IOException {
    if (this.stack.isEmpty()) {
        return JsonToken.END_DOCUMENT;
    }
    Iterator<?> o = peekStack();
    if (o instanceof Iterator) {
        boolean isObject = this.stack.get(this.stack.size() - 2) instanceof JsonObject;
        Iterator<?> iterator = o;
        if (!iterator.hasNext()) {
            return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
        } else {
            if (isObject) {
                return JsonToken.NAME;
            }
            this.stack.add(iterator.next());
            return peek();
        }
    } else if (o instanceof JsonObject) {
        return JsonToken.BEGIN_OBJECT;
    } else {
        if (o instanceof JsonArray) {
            return JsonToken.BEGIN_ARRAY;
        }
        if (o instanceof JsonPrimitive) {
            JsonPrimitive primitive = (JsonPrimitive) o;
            if (primitive.isString()) {
                return JsonToken.STRING;
            }
            if (primitive.isBoolean()) {
                return JsonToken.BOOLEAN;
            }
            if (primitive.isNumber()) {
                return JsonToken.NUMBER;
            }
            throw new AssertionError();
        } else if (o instanceof JsonNull) {
            return JsonToken.NULL;
        } else {
            if (o == SENTINEL_CLOSED) {
                throw new IllegalStateException("JsonReader is closed");
            }
            throw new AssertionError();
        }
    }
}