Java Code Examples for com.google.gson.stream.JsonReader#nextDouble()
The following examples show how to use
com.google.gson.stream.JsonReader#nextDouble() .
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: JsonParser.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
private static Object parseRecursive(JsonReader jr) throws IOException { checkState(jr.hasNext(), "unexpected end of JSON"); switch (jr.peek()) { case BEGIN_ARRAY: return parseJsonArray(jr); case BEGIN_OBJECT: return parseJsonObject(jr); case STRING: return jr.nextString(); case NUMBER: return jr.nextDouble(); case BOOLEAN: return jr.nextBoolean(); case NULL: return parseJsonNull(jr); default: throw new IllegalStateException("Bad token: " + jr.getPath()); } }
Example 2
Source File: Gson.java From gson with Apache License 2.0 | 6 votes |
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) { return TypeAdapters.DOUBLE; } return new TypeAdapter<Number>() { @Override public Double read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return in.nextDouble(); } @Override public void write(JsonWriter out, Number value) throws IOException { if (value == null) { out.nullValue(); return; } double doubleValue = value.doubleValue(); checkValidFloatingPoint(doubleValue); out.value(value); } }; }
Example 3
Source File: DoubleTypeAdapter.java From EasyHttp with Apache License 2.0 | 6 votes |
@Override public Number read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; case NUMBER: // 如果后台返回数值,则按照正常逻辑解析 return in.nextDouble(); case STRING: try { return Double.parseDouble(in.nextString()); } catch (NumberFormatException e) { // 如果是空字符串则会抛出这个异常 return 0; } default: in.skipValue(); return 0; } }
Example 4
Source File: SupportUtils.java From data-mediator with Apache License 2.0 | 6 votes |
public static Object readPrimitiveOrItsBox(JsonReader reader, Property p) throws IOException { Class<?> type = p.getType(); if (type == void.class || type == Void.class) { return null; } else if (type == boolean.class || type == Boolean.class) { return reader.nextBoolean(); } else if (type == byte.class || type == Byte.class) { return (byte)reader.nextInt(); } else if (type == short.class || type == Short.class) { return (short)reader.nextInt(); } else if (type == int.class || type == Integer.class) { return reader.nextInt(); } else if (type == long.class || type == Long.class) { return reader.nextLong(); } else if (type == char.class || type == Character.class) { return (char)reader.nextLong(); } else if (type == float.class || type == Float.class) { return (float)reader.nextDouble(); } else if (type == double.class || type == Double.class) { return reader.nextDouble(); } else { throw new IllegalStateException(); } }
Example 5
Source File: ObjectTypeAdapter.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@Override public Object read(JsonReader in) throws IOException { JsonToken token = in.peek(); switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray(); while (in.hasNext()) { list.add(read(in)); } in.endArray(); return list; case BEGIN_OBJECT: Map<String, Object> map = new LinkedTreeMap<String, Object>(); in.beginObject(); while (in.hasNext()) { map.put(in.nextName(), read(in)); } in.endObject(); return map; case STRING: return in.nextString(); case NUMBER: return in.nextDouble(); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; default: throw new IllegalStateException(); } }
Example 6
Source File: ObjectTypeAdapter.java From gson with Apache License 2.0 | 5 votes |
@Override public Object read(JsonReader in) throws IOException { JsonToken token = in.peek(); switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray(); while (in.hasNext()) { list.add(read(in)); } in.endArray(); return list; case BEGIN_OBJECT: Map<String, Object> map = new LinkedTreeMap<String, Object>(); in.beginObject(); while (in.hasNext()) { map.put(in.nextName(), read(in)); } in.endObject(); return map; case STRING: return in.nextString(); case NUMBER: return in.nextDouble(); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; default: throw new IllegalStateException(); } }
Example 7
Source File: DoubleTypeAdapter.java From RegexGenerator with GNU General Public License v3.0 | 5 votes |
@Override public Double read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } Double nextDouble; try { //nextDouble parses as a double or fallback to parsing a string using Java parseDouble which MANAGES NaN nextDouble = in.nextDouble(); } catch (NumberFormatException ex) { nextDouble = Double.NaN; } return nextDouble; }
Example 8
Source File: ProfileGrapher.java From bazel with Apache License 2.0 | 5 votes |
private static Map<String, Object> decodeJsonObject(JsonReader reader) throws IOException { reader.beginObject(); Map<String, Object> data = new HashMap<>(); while (reader.hasNext()) { String name = reader.nextName(); Object value; switch (reader.peek()) { case BOOLEAN: value = reader.nextBoolean(); break; case NUMBER: value = reader.nextDouble(); break; case STRING: value = reader.nextString(); break; case BEGIN_OBJECT: value = null; Map<String, Object> childData = decodeJsonObject(reader); for (Map.Entry<String, Object> entry : childData.entrySet()) { data.put(name + "." + entry.getKey(), entry.getValue()); } break; default: reader.skipValue(); continue; } data.put(name, value); } reader.endObject(); return data; }
Example 9
Source File: DateAdapter.java From salt-netapi-client with MIT License | 5 votes |
@Override public Date read(JsonReader jsonReader) throws IOException { try { double dateMilliseconds = jsonReader.nextDouble() * 1000; return new Date((long) dateMilliseconds); } catch (NumberFormatException | IllegalStateException e) { throw new JsonParseException(e); } }
Example 10
Source File: LatLngAdapter.java From android-places-demos with Apache License 2.0 | 5 votes |
/** * Reads in a JSON object and try to create a LatLng in one of the following formats. * * <pre>{ * "lat" : -33.8353684, * "lng" : 140.8527069 * } * * { * "latitude": -33.865257570508334, * "longitude": 151.19287000481452 * }</pre> */ @Override public LatLng read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } double lat = 0; double lng = 0; boolean hasLat = false; boolean hasLng = false; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("lat".equals(name) || "latitude".equals(name)) { lat = reader.nextDouble(); hasLat = true; } else if ("lng".equals(name) || "longitude".equals(name)) { lng = reader.nextDouble(); hasLng = true; } } reader.endObject(); if (hasLat && hasLng) { return new LatLng(lat, lng); } else { return null; } }
Example 11
Source File: LatLngAdapter.java From android-places-demos with Apache License 2.0 | 5 votes |
/** * Reads in a JSON object and try to create a LatLng in one of the following formats. * * <pre>{ * "lat" : -33.8353684, * "lng" : 140.8527069 * } * * { * "latitude": -33.865257570508334, * "longitude": 151.19287000481452 * }</pre> */ @Override public LatLng read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } double lat = 0; double lng = 0; boolean hasLat = false; boolean hasLng = false; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("lat".equals(name) || "latitude".equals(name)) { lat = reader.nextDouble(); hasLat = true; } else if ("lng".equals(name) || "longitude".equals(name)) { lng = reader.nextDouble(); hasLng = true; } } reader.endObject(); if (hasLat && hasLng) { return new LatLng(lat, lng); } else { return null; } }
Example 12
Source File: EnumTest.java From openapi-generator with Apache License 2.0 | 4 votes |
@Override public EnumNumberEnum read(final JsonReader jsonReader) throws IOException { Double value = jsonReader.nextDouble(); return EnumNumberEnum.fromValue(value); }
Example 13
Source File: EnumTest.java From openapi-generator with Apache License 2.0 | 4 votes |
@Override public EnumNumberEnum read(final JsonReader jsonReader) throws IOException { Double value = jsonReader.nextDouble(); return EnumNumberEnum.fromValue(value); }
Example 14
Source File: EnumTest.java From openapi-generator with Apache License 2.0 | 4 votes |
@Override public EnumNumberEnum read(final JsonReader jsonReader) throws IOException { Double value = jsonReader.nextDouble(); return EnumNumberEnum.fromValue(value); }
Example 15
Source File: CloudAppConfigurationGsonFactory.java From shardingsphere-elasticjob-cloud with Apache License 2.0 | 4 votes |
@Override public CloudAppConfiguration read(final JsonReader in) throws IOException { String appURL = ""; String appName = ""; String bootstrapScript = ""; double cpuCount = 1.0d; double memoryMB = 128.0d; boolean appCacheEnable = true; int eventTraceSamplingCount = 0; in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case CloudConfigurationConstants.APP_NAME: appName = in.nextString(); break; case CloudConfigurationConstants.APP_URL: appURL = in.nextString(); break; case CloudConfigurationConstants.BOOTSTRAP_SCRIPT: bootstrapScript = in.nextString(); break; case CloudConfigurationConstants.CPU_COUNT: cpuCount = in.nextDouble(); break; case CloudConfigurationConstants.MEMORY_MB: memoryMB = in.nextDouble(); break; case CloudConfigurationConstants.APP_CACHE_ENABLE: appCacheEnable = in.nextBoolean(); break; case CloudConfigurationConstants.EVENT_TRACE_SAMPLING_COUNT: eventTraceSamplingCount = in.nextInt(); break; default: break; } } in.endObject(); return new CloudAppConfiguration(appName, appURL, bootstrapScript, cpuCount, memoryMB, appCacheEnable, eventTraceSamplingCount); }
Example 16
Source File: EnumTest.java From openapi-generator with Apache License 2.0 | 4 votes |
@Override public EnumNumberEnum read(final JsonReader jsonReader) throws IOException { Double value = jsonReader.nextDouble(); return EnumNumberEnum.fromValue(value); }
Example 17
Source File: Adapters.java From salt-netapi-client with MIT License | 4 votes |
@Override public Double read(JsonReader in) throws IOException { return in.nextDouble(); }
Example 18
Source File: StructuredJsonExamplesProvider.java From vw-webservice with BSD 3-Clause "New" or "Revised" License | 4 votes |
private Namespace readNamespace(long exampleNumber, JsonReader jsonReader) throws IOException { jsonReader.beginObject(); StructuredExample.Namespace.NamespaceBuilder nsBuilder = new StructuredExample.Namespace.NamespaceBuilder(); boolean nameRead = false, scalingFactorRead = false, featuresRead = false; while (jsonReader.hasNext()) { String propertyNameOriginal = jsonReader.nextName(); String propertyName = propertyNameOriginal.trim().toLowerCase(); if (propertyName.equals(StructuredJsonPropertyNames.NAMESPACE_NAME_PROPERTY)) { if (nameRead) { throw new ExampleFormatException(exampleNumber, "The 'name' property must only appear once in a namespace!"); } if (jsonReader.peek() == JsonToken.NULL) jsonReader.nextNull(); else { String namespace = jsonReader.nextString(); nsBuilder.setName(namespace); } nameRead = true; } else if (propertyName.equals(StructuredJsonPropertyNames.NAMESPACE_SCALING_FACTOR_PROPERTY)) { if (scalingFactorRead) { throw new ExampleFormatException(exampleNumber, "The 'value' property must only appear once in a namespace!"); } if (jsonReader.peek() == JsonToken.NULL) jsonReader.nextNull(); else { double scalingFactor = jsonReader.nextDouble(); nsBuilder.setScalingFactor(Float.valueOf((float) scalingFactor)); } scalingFactorRead = true; } else if (propertyName.equals(StructuredJsonPropertyNames.NAMESPACE_FEATURES_PROPERTY)) { if (featuresRead) { throw new ExampleFormatException(exampleNumber, "The 'features' property must only appear once in a namespace!"); } if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else { jsonReader.beginArray(); int numFeaturesAdded = 0; while (jsonReader.hasNext()) { readFeatureIntoNamespace(exampleNumber, nsBuilder, jsonReader); numFeaturesAdded++; if (maxNumberOfFeaturesPerNamespace > 0 && maxNumberOfFeaturesPerNamespace < Integer.MAX_VALUE && numFeaturesAdded > maxNumberOfFeaturesPerNamespace) { throw new ExampleFormatException(exampleNumber, "The maximum number of features per namespace, " + maxNumberOfFeaturesPerNamespace + " was exceeded!"); } } jsonReader.endArray(); } featuresRead = true; } else { throw new ExampleFormatException(exampleNumber, "Unknown property: " + propertyNameOriginal + " found while reading namespace!"); } } jsonReader.endObject(); return nsBuilder.build(); }
Example 19
Source File: GeolocationResponseAdapter.java From google-maps-services-java with Apache License 2.0 | 4 votes |
/** * Reads in a JSON object to create a Geolocation Response. See: * https://developers.google.com/maps/documentation/geolocation/intro#responses * * <p>Success Case: * * <pre> * { * "location": { * "lat": 51.0, * "lng": -0.1 * }, * "accuracy": 1200.4 * } * </pre> * * Error Case: The response contains an object with a single error object with the following keys: * * <p>code: This is the same as the HTTP status of the response. {@code message}: A short * description of the error. {@code errors}: A list of errors which occurred. Each error contains * an identifier for the type of error (the reason) and a short description (the message). For * example, sending invalid JSON will return the following error: * * <pre> * { * "error": { * "errors": [ { * "domain": "geolocation", * "reason": "notFound", * "message": "Not Found", * "debugInfo": "status: ZERO_RESULTS\ncom.google.api.server.core.Fault: Immu... * }], * "code": 404, * "message": "Not Found" * } * } * </pre> */ @Override public GeolocationApi.Response read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } GeolocationApi.Response response = new GeolocationApi.Response(); LatLngAdapter latLngAdapter = new LatLngAdapter(); reader.beginObject(); // opening { while (reader.hasNext()) { String name = reader.nextName(); // two different objects could be returned a success object containing "location" and // "accuracy" keys or an error object containing an "error" key if (name.equals("location")) { // we already have a parser for the LatLng object so lets use that response.location = latLngAdapter.read(reader); } else if (name.equals("accuracy")) { response.accuracy = reader.nextDouble(); } else if (name.equals("error")) { reader.beginObject(); // the error key leads to another object... while (reader.hasNext()) { String errName = reader.nextName(); // ...with keys "errors", "code" and "message" if (errName.equals("code")) { response.code = reader.nextInt(); } else if (errName.equals("message")) { response.message = reader.nextString(); } else if (errName.equals("errors")) { reader.beginArray(); // its plural because its an array of errors... while (reader.hasNext()) { reader.beginObject(); // ...and each error array element is an object... while (reader.hasNext()) { errName = reader.nextName(); // ...with keys "reason", "domain", "debugInfo", "location", "locationType", and // "message" (again) if (errName.equals("reason")) { response.reason = reader.nextString(); } else if (errName.equals("domain")) { response.domain = reader.nextString(); } else if (errName.equals("debugInfo")) { response.debugInfo = reader.nextString(); } else if (errName.equals("message")) { // have this already reader.nextString(); } else if (errName.equals("location")) { reader.nextString(); } else if (errName.equals("locationType")) { reader.nextString(); } } reader.endObject(); } reader.endArray(); } } reader.endObject(); // closing } } } reader.endObject(); return response; }
Example 20
Source File: JsonPropertyDeserializer.java From olingo-odata2 with Apache License 2.0 | 4 votes |
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); }