Java Code Examples for com.google.gson.stream.JsonToken#NUMBER
The following examples show how to use
com.google.gson.stream.JsonToken#NUMBER .
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: AbstractPluginMessagingForwardingSink.java From NuVotifier with GNU General Public License v3.0 | 6 votes |
public void handlePluginMessage(byte[] message) { String strMessage = new String(message, StandardCharsets.UTF_8); try (CharArrayReader reader = new CharArrayReader(strMessage.toCharArray())) { JsonReader r = new JsonReader(reader); r.setLenient(true); while (r.peek() != JsonToken.END_DOCUMENT) { r.beginObject(); JsonObject o = new JsonObject(); while (r.hasNext()) { String name = r.nextName(); if (r.peek() == JsonToken.NUMBER) o.add(name, new JsonPrimitive(r.nextLong())); else o.add(name, new JsonPrimitive(r.nextString())); } r.endObject(); Vote v = new Vote(o); listener.onForward(v); } } catch (IOException e) { e.printStackTrace(); // Should never happen. } }
Example 2
Source File: WordTimingTypeAdapter.java From java-sdk with Apache License 2.0 | 6 votes |
@Override public WordTiming read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } final WordTiming wordTiming = new WordTiming(); in.beginArray(); if (in.peek() == JsonToken.STRING) { wordTiming.setWord(in.nextString()); } if (in.peek() == JsonToken.NUMBER) { wordTiming.setStartTime(in.nextDouble()); } if (in.peek() == JsonToken.NUMBER) { wordTiming.setEndTime(in.nextDouble()); } in.endArray(); return wordTiming; }
Example 3
Source File: SpeechWordConfidenceTypeAdapter.java From java-sdk with Apache License 2.0 | 6 votes |
@Override public SpeechWordConfidence read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } final SpeechWordConfidence speechWordConfidence = new SpeechWordConfidence(); reader.beginArray(); if (reader.peek() == JsonToken.STRING) { speechWordConfidence.setWord(reader.nextString()); } if (reader.peek() == JsonToken.NUMBER) { speechWordConfidence.setConfidence(reader.nextDouble()); } reader.endArray(); return speechWordConfidence; }
Example 4
Source File: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@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); } int result = ((JsonPrimitive) peekStack()).getAsInt(); popStack(); return result; }
Example 5
Source File: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@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 6
Source File: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@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 7
Source File: ChannelReader.java From packagedrone with Eclipse Public License 1.0 | 5 votes |
private Instant readTime ( final JsonReader jr ) throws IOException { final JsonToken peek = jr.peek (); if ( peek == JsonToken.NUMBER ) { return Instant.ofEpochMilli ( jr.nextLong () ); } else if ( peek == JsonToken.NULL ) { jr.nextNull (); return null; } else if ( peek == JsonToken.STRING ) { final String str = jr.nextString (); try { return Instant.ofEpochMilli ( Long.parseLong ( str ) ); } catch ( final NumberFormatException e ) { try { return this.dateFormat.parse ( str ).toInstant (); } catch ( final ParseException e2 ) { throw new IOException ( e2 ); } } } else { throw new IOException ( String.format ( "Invalid timestamp token: %s", peek ) ); } }
Example 8
Source File: DayOfWeekAdapter.java From google-maps-services-java with Apache License 2.0 | 5 votes |
@Override public DayOfWeek read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } if (reader.peek() == JsonToken.NUMBER) { int day = reader.nextInt(); switch (day) { case 0: return DayOfWeek.SUNDAY; case 1: return DayOfWeek.MONDAY; case 2: return DayOfWeek.TUESDAY; case 3: return DayOfWeek.WEDNESDAY; case 4: return DayOfWeek.THURSDAY; case 5: return DayOfWeek.FRIDAY; case 6: return DayOfWeek.SATURDAY; } } return DayOfWeek.UNKNOWN; }
Example 9
Source File: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@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); } long result = ((JsonPrimitive) peekStack()).getAsLong(); popStack(); return result; }
Example 10
Source File: EitherTypeAdapter.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
protected JsonToken getExpectedToken(Class<?> rawType) { if (rawType.isArray() || Collection.class.isAssignableFrom(rawType) || Tuple.class.isAssignableFrom(rawType)) { return JsonToken.BEGIN_ARRAY; } if (Boolean.class.isAssignableFrom(rawType)) { return JsonToken.BOOLEAN; } if (Number.class.isAssignableFrom(rawType) || Enum.class.isAssignableFrom(rawType)) { return JsonToken.NUMBER; } if (Character.class.isAssignableFrom(rawType) || String.class.isAssignableFrom(rawType)) { return JsonToken.STRING; } return JsonToken.BEGIN_OBJECT; }
Example 11
Source File: JsonTreeReader.java From letv with Apache License 2.0 | 5 votes |
public int nextInt() throws IOException { JsonToken token = peek(); if (token == JsonToken.NUMBER || token == JsonToken.STRING) { int result = ((JsonPrimitive) peekStack()).getAsInt(); popStack(); return result; } throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token); }
Example 12
Source File: JsonTreeReader.java From letv with Apache License 2.0 | 5 votes |
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 13
Source File: JsonTreeReader.java From MiBandDecompiled with Apache License 2.0 | 5 votes |
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 14
Source File: InstantAdapter.java From google-maps-services-java with Apache License 2.0 | 5 votes |
/** Read a time from the Places API and convert to a {@link Instant} */ @Override public Instant read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } if (reader.peek() == JsonToken.NUMBER) { // Number is the number of seconds since Epoch. return Instant.ofEpochMilli(reader.nextLong() * 1000L); } throw new UnsupportedOperationException("Unsupported format"); }
Example 15
Source File: JsonPropertyConsumer.java From cloud-odata-java with Apache License 2.0 | 5 votes |
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, final Object typeMapping) 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; 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; return type.valueOfString((String) value, EdmLiteralKind.JSON, entityPropertyInfo.getFacets(), typeMappingClass); }
Example 16
Source File: BsonReader.java From immutables with Apache License 2.0 | 5 votes |
private static JsonToken toGsonToken(BsonType type) { switch (type) { case END_OF_DOCUMENT: return JsonToken.END_DOCUMENT; case DOCUMENT: return JsonToken.BEGIN_OBJECT; case ARRAY: return JsonToken.BEGIN_ARRAY; case BOOLEAN: return JsonToken.BOOLEAN; case STRING: case SYMBOL: case OBJECT_ID: case BINARY: case REGULAR_EXPRESSION: return JsonToken.STRING; case DATE_TIME: case DOUBLE: case INT32: case INT64: case TIMESTAMP: case DECIMAL128: return JsonToken.NUMBER; case NULL: case UNDEFINED: return JsonToken.NULL; default: // not really sure what to do with this type return JsonToken.NULL; } }
Example 17
Source File: MessageTypeAdapter.java From lsp4j with Eclipse Public License 2.0 | 4 votes |
@Override public Message read(JsonReader in) throws IOException, JsonIOException, JsonSyntaxException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); String jsonrpc = null, method = null; Either<String, Number> id = null; Object rawParams = null; Object rawResult = null; ResponseError responseError = null; try { while (in.hasNext()) { String name = in.nextName(); switch (name) { case "jsonrpc": { jsonrpc = in.nextString(); break; } case "id": { if (in.peek() == JsonToken.NUMBER) id = Either.forRight(in.nextInt()); else id = Either.forLeft(in.nextString()); break; } case "method": { method = in.nextString(); break; } case "params": { rawParams = parseParams(in, method); break; } case "result": { rawResult = parseResult(in, id != null ? id.get().toString() : null); break; } case "error": { responseError = gson.fromJson(in, ResponseError.class); break; } default: in.skipValue(); } } Object params = parseParams(rawParams, method); Object result = parseResult(rawResult, id != null ? id.get().toString() : null); in.endObject(); return createMessage(jsonrpc, id, method, params, result, responseError); } catch (JsonSyntaxException | MalformedJsonException | EOFException exception) { if (id != null || method != null) { // Create a message and bundle it to an exception with an issue that wraps the original exception Message message = createMessage(jsonrpc, id, method, rawParams, rawResult, responseError); MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), exception); throw new MessageIssueException(message, issue); } else { throw exception; } } }
Example 18
Source File: JsonTreeReader.java From gson with Apache License 2.0 | 4 votes |
@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 19
Source File: JsonTreeReader.java From letv with Apache License 2.0 | 4 votes |
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: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 4 votes |
@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(); } }