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

The following examples show how to use com.google.gson.stream.JsonToken#STRING . 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: JsonLinkConsumer.java    From cloud-odata-java with Apache License 2.0 6 votes vote down vote up
private List<String> readLinksArray(final JsonReader reader) throws IOException, EntityProviderException {
  List<String> links = new ArrayList<String>();

  reader.beginArray();
  while (reader.hasNext()) {
    reader.beginObject();
    String nextName = reader.nextName();
    if (FormatJson.URI.equals(nextName) && reader.peek() == JsonToken.STRING) {
      links.add(reader.nextString());
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_CONTENT.addContent(FormatJson.URI).addContent(nextName));
    }
    reader.endObject();
  }
  reader.endArray();

  return links;
}
 
Example 2
Source File: MarkTimingTypeAdapter.java    From java-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public MarkTiming read(JsonReader in) throws IOException {
  if (in.peek() == JsonToken.NULL) {
    in.nextNull();
    return null;
  }

  final MarkTiming markTiming = new MarkTiming();
  in.beginArray();

  if (in.peek() == JsonToken.STRING) {
    markTiming.setMark(in.nextString());
  }
  if (in.peek() == JsonToken.NUMBER) {
    markTiming.setTime(in.nextDouble());
  }

  in.endArray();
  return markTiming;
}
 
Example 3
Source File: JsonTreeReader.java    From gson with Apache License 2.0 6 votes vote down vote up
@Override public double nextDouble() throws IOException {
  JsonToken token = peek();
  if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
    throw new IllegalStateException(
        "Expected " + JsonToken.NUMBER + " but was " + token + locationString());
  }
  double result = ((JsonPrimitive) peekStack()).getAsDouble();
  if (!isLenient() && (Double.isNaN(result) || Double.isInfinite(result))) {
    throw new NumberFormatException("JSON forbids NaN and infinities: " + result);
  }
  popStack();
  if (stackSize > 0) {
    pathIndices[stackSize - 1]++;
  }
  return result;
}
 
Example 4
Source File: JsonTreeReader.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public double nextDouble()
{
    JsonToken jsontoken = peek();
    if (jsontoken != JsonToken.NUMBER && jsontoken != JsonToken.STRING)
    {
        throw new IllegalStateException((new StringBuilder()).append("Expected ").append(JsonToken.NUMBER).append(" but was ").append(jsontoken).toString());
    }
    double d1 = ((JsonPrimitive)a()).getAsDouble();
    if (!isLenient() && (Double.isNaN(d1) || Double.isInfinite(d1)))
    {
        throw new NumberFormatException((new StringBuilder()).append("JSON forbids NaN and infinities: ").append(d1).toString());
    } else
    {
        b();
        return d1;
    }
}
 
Example 5
Source File: JsonFeedDeserializer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param reader
 * @param feedMetadata
 * @throws IOException
 * @throws EntityProviderException
 */
protected static void readInlineCount(final JsonReader reader, final FeedMetadataImpl feedMetadata)
    throws IOException, EntityProviderException {
  if (reader.peek() == JsonToken.STRING && feedMetadata.getInlineCount() == null) {
    int inlineCount;
    try {
      inlineCount = reader.nextInt();
    } catch (final NumberFormatException e) {
      throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(""), e);
    }
    if (inlineCount >= 0) {
      feedMetadata.setInlineCount(inlineCount);
    } else {
      throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(inlineCount));
    }
  } else {
    throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(reader.peek()));
  }
}
 
Example 6
Source File: JsonLdSerializer.java    From schemaorg-java with Apache License 2.0 6 votes vote down vote up
private JsonLdContext readContextDef(JsonReader reader) throws IOException {
  reader.beginObject();
  JsonLdContext.Builder contextBuilder = JsonLdFactory.newContextBuilder();
  while (reader.hasNext()) {
    String key = reader.nextName();
    if (!JsonLdConstants.BASE.equals(key)) {
      throw new JsonLdSyntaxException(
          String.format("Unsupported '%s' in JSON-LD context", key));
    }
    JsonToken valueToken = reader.peek();
    if (valueToken == JsonToken.STRING) {
      contextBuilder.setBase(reader.nextString());
    } else if (valueToken == JsonToken.NULL) {
      reader.nextNull();
      contextBuilder.setBase(null);
    } else {
      throw new JsonLdSyntaxException(
          String.format("The value of @base is '%s', should be a string", valueToken));
    }
  }
  reader.endObject();
  return contextBuilder.build();
}
 
Example 7
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 8
Source File: JsonTreeReader.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public String nextString() throws IOException {
  JsonToken token = peek();
  if (token != JsonToken.STRING && token != JsonToken.NUMBER) {
    throw new IllegalStateException("Expected " + JsonToken.STRING + " but was " + token);
  }
  return ((JsonPrimitive) popStack()).getAsString();
}
 
Example 9
Source File: ChannelReader.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private Map<String, String> readAspects ( final JsonReader jr ) throws IOException
{
    final Map<String, String> result = new HashMap<> ();

    jr.beginObject ();
    while ( jr.hasNext () )
    {
        switch ( jr.nextName () )
        {
            case "map":
                jr.beginObject ();
                while ( jr.hasNext () )
                {
                    final String id = jr.nextName ();
                    String value = null;
                    if ( jr.peek () == JsonToken.STRING )
                    {
                        value = jr.nextString ();
                    }
                    else
                    {
                        jr.skipValue ();
                    }
                    result.put ( id, value );
                }
                jr.endObject ();
                break;
        }
    }
    jr.endObject ();

    return result;
}
 
Example 10
Source File: JsonTreeReader.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public double nextDouble() throws IOException {
  JsonToken token = peek();
  if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
    throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token);
  }
  double result = ((JsonPrimitive) peekStack()).getAsDouble();
  if (!isLenient() && (Double.isNaN(result) || Double.isInfinite(result))) {
    throw new NumberFormatException("JSON forbids NaN and infinities: " + result);
  }
  popStack();
  return result;
}
 
Example 11
Source File: JsonFeedConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void handleName(final String nextName) throws IOException, EdmException, EntityProviderException {
  if (FormatJson.RESULTS.equals(nextName)) {
    resultsArrayPresent = true;
    readArrayContent();

  } else if (FormatJson.COUNT.equals(nextName)) {
    readInlineCount(reader, feedMetadata);

  } else if (FormatJson.NEXT.equals(nextName)) {
    if (reader.peek() == JsonToken.STRING && feedMetadata.getNextLink() == null) {
      String nextLink = reader.nextString();
      feedMetadata.setNextLink(nextLink);
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_CONTENT.addContent(nextName).addContent(
          "JsonFeed"));
    }

  } else if (FormatJson.DELTA.equals(nextName)) {
    if (reader.peek() == JsonToken.STRING && feedMetadata.getDeltaLink() == null) {
      String deltaLink = reader.nextString();
      feedMetadata.setDeltaLink(deltaLink);
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_CONTENT.addContent(nextName).addContent(
          "JsonFeed"));
    }
  } else {
    throw new EntityProviderException(EntityProviderException.INVALID_CONTENT.addContent(nextName).addContent(
        "JsonFeed"));
  }
}
 
Example 12
Source File: TypeAdapters.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean read(JsonReader in) throws IOException {
  JsonToken peek = in.peek();
  if (peek == JsonToken.NULL) {
    in.nextNull();
    return null;
  } else if (peek == JsonToken.STRING) {
    // support strings for compatibility with GSON 1.7
    return Boolean.parseBoolean(in.nextString());
  }
  return in.nextBoolean();
}
 
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: JsonTreeReader.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override public int nextInt() throws IOException {
  JsonToken token = peek();
  if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
    throw new IllegalStateException(
        "Expected " + JsonToken.NUMBER + " but was " + token + locationString());
  }
  int result = ((JsonPrimitive) peekStack()).getAsInt();
  popStack();
  if (stackSize > 0) {
    pathIndices[stackSize - 1]++;
  }
  return result;
}
 
Example 15
Source File: JsonTreeReader.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override public long nextLong() throws IOException {
  JsonToken token = peek();
  if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
    throw new IllegalStateException(
        "Expected " + JsonToken.NUMBER + " but was " + token + locationString());
  }
  long result = ((JsonPrimitive) peekStack()).getAsLong();
  popStack();
  if (stackSize > 0) {
    pathIndices[stackSize - 1]++;
  }
  return result;
}
 
Example 16
Source File: JsonTreeReader.java    From letv with Apache License 2.0 5 votes vote down vote up
public long nextLong() throws IOException {
    JsonToken token = peek();
    if (token == JsonToken.NUMBER || token == JsonToken.STRING) {
        long result = ((JsonPrimitive) peekStack()).getAsLong();
        popStack();
        return result;
    }
    throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token);
}
 
Example 17
Source File: JsonErrorDocumentDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param reader
 * @param errorContext
 * @throws IOException
 */
private void parseInnerError(final JsonReader reader, final ODataErrorContext errorContext) throws IOException {
  JsonToken token = reader.peek();
  if (token == JsonToken.STRING) {
    // implementation for parse content as provided by JsonErrorDocumentProducer
    String innerError = reader.nextString();
    errorContext.setInnerError(innerError);
  } else if (token == JsonToken.BEGIN_OBJECT) {
    // implementation for OData v2 Section 2.2.8.1.2 JSON Error Response
    // (RFC4627 Section 2.2 -> https://www.ietf.org/rfc/rfc4627.txt))
    // currently partial provided
    errorContext.setInnerError(readJson(reader));
  }
}
 
Example 18
Source File: JsonTreeReader.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public String nextString()
{
    JsonToken jsontoken = peek();
    if (jsontoken != JsonToken.STRING && jsontoken != JsonToken.NUMBER)
    {
        throw new IllegalStateException((new StringBuilder()).append("Expected ").append(JsonToken.STRING).append(" but was ").append(jsontoken).toString());
    } else
    {
        return ((JsonPrimitive)b()).getAsString();
    }
}
 
Example 19
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();
        }
    }
}
 
Example 20
Source File: JsonPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final DeserializerProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}