Java Code Examples for com.google.gson.stream.JsonReader#nextName()
The following examples show how to use
com.google.gson.stream.JsonReader#nextName() .
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: DesignateAdapters.java From denominator with Apache License 2.0 | 6 votes |
@Override public List<X> read(JsonReader reader) throws IOException { List<X> elements = new LinkedList<X>(); reader.beginObject(); while (reader.hasNext()) { String nextName = reader.nextName(); if (jsonKey().equals(nextName)) { reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); elements.add(build(reader)); reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); Collections.sort(elements, toStringComparator()); return elements; }
Example 2
Source File: SourceFilePositionJsonSerializer.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public SourceFilePosition read(JsonReader in) throws IOException { in.beginObject(); SourceFile file = SourceFile.UNKNOWN; SourcePosition position = SourcePosition.UNKNOWN; while(in.hasNext()) { String name = in.nextName(); if (name.equals(FILE)) { file = mSourceFileJsonTypeAdapter.read(in); } else if (name.equals(POSITION)) { position = mSourcePositionJsonTypeAdapter.read(in); } else { in.skipValue(); } } in.endObject(); return new SourceFilePosition(file, position); }
Example 3
Source File: CommandStub.java From ForgeHax with MIT License | 5 votes |
@Override public void deserialize(JsonReader reader) throws IOException { reader.beginObject(); reader.nextName(); int kc = reader.nextInt(); if (kc > -1) { bind(kc); } reader.endObject(); }
Example 4
Source File: JsonServiceDocumentConsumer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
private void readContent(final JsonReader reader) throws IOException, EdmException, EntityProviderException { currentHandledObjectName = reader.nextName(); if (FormatJson.ENTITY_SETS.equals(currentHandledObjectName)) { reader.beginArray(); readEntitySets(reader); reader.endArray(); } }
Example 5
Source File: RectangleTypeAdapter.java From jadx with Apache License 2.0 | 5 votes |
@Override public Rectangle read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); Rectangle rectangle = new Rectangle(); while (in.hasNext()) { String name = in.nextName(); switch (name) { case "x": rectangle.x = in.nextInt(); break; case "y": rectangle.y = in.nextInt(); break; case "width": rectangle.width = in.nextInt(); break; case "height": rectangle.height = in.nextInt(); break; default: throw new IllegalArgumentException("Unknown field in Rectangle: " + name); } } in.endObject(); return rectangle; }
Example 6
Source File: QuerySolrIT.java From nifi with Apache License 2.0 | 5 votes |
@Test public void testStats() throws IOException { SolrClient solrClient = createSolrClient(); TestRunner runner = createRunnerWithSolrClient(solrClient); runner.setProperty("stats", "true"); runner.setProperty("stats.field", "integer_single"); runner.enqueue(new ByteArrayInputStream(new byte[0])); runner.run(); runner.assertTransferCount(QuerySolr.STATS, 1); JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream( runner.getContentAsByteArray(runner.getFlowFilesForRelationship(QuerySolr.STATS).get(0))))); reader.beginObject(); assertEquals(reader.nextName(), "stats_fields"); reader.beginObject(); assertEquals(reader.nextName(), "integer_single"); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "min": assertEquals(reader.nextString(), "0.0"); break; case "max": assertEquals(reader.nextString(), "9.0"); break; case "count": assertEquals(reader.nextInt(), 10); break; case "sum": assertEquals(reader.nextString(), "45.0"); break; default: reader.skipValue(); break; } } reader.endObject(); reader.endObject(); reader.endObject(); reader.close(); solrClient.close(); }
Example 7
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 8
Source File: SizeWeightReader.java From TFC2 with GNU General Public License v3.0 | 5 votes |
private SizeWeightJSON read(JsonReader reader) throws IOException { String name = ""; int meta = -1; EnumSize size = EnumSize.VERYSMALL; EnumWeight weight = EnumWeight.VERYLIGHT; reader.beginObject(); while(reader.hasNext()) { String key = reader.nextName(); if(key.equals("name")) { name = reader.nextString(); if(name.contains(" ")) { String[] s = name.split(" "); name = s[0]; meta = Integer.parseInt(s[1]); } } else if(key.equals("size")) { size = EnumSize.fromString(reader.nextString()); } else if(key.equals("weight")) { weight = EnumWeight.fromString(reader.nextString()); } else reader.skipValue(); } reader.endObject(); return new SizeWeightJSON(name, meta, size, weight); }
Example 9
Source File: LatLngAdapter.java From google-maps-services-java 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 10
Source File: PolymorphicTypeNameStrategy.java From rockscript with Apache License 2.0 | 5 votes |
@Override public Object read(JsonReader in, FieldsReader fieldsReader) throws Exception{ in.beginObject(); String typeName = in.nextName(); Object bean = fieldsReader.instantiateBean(typeName); in.beginObject(); fieldsReader.readFields(bean); in.endObject(); in.endObject(); return bean; }
Example 11
Source File: FeatureCollectionAdapter.java From geogson with Apache License 2.0 | 5 votes |
@Override public FeatureCollection read(JsonReader in) throws IOException { FeatureCollection featureCollection = null; if (in.peek() == JsonToken.NULL) { in.nextNull(); } else if (in.peek() == JsonToken.BEGIN_OBJECT) { in.beginObject(); LinkedList<Feature> features = null; while (in.hasNext()) { String name = in.nextName(); if ("features".equalsIgnoreCase(name)) { in.beginArray(); features = new LinkedList<>(); while(in.peek() == JsonToken.BEGIN_OBJECT) { Feature feature = gson.fromJson(in, Feature.class); features.add(feature); } in.endArray(); } else { in.skipValue(); } } if (features == null) { throw new IllegalArgumentException("Required field 'features' is missing"); } featureCollection = new FeatureCollection(features); in.endObject(); } else { throw new IllegalArgumentException("The given json is not a valid FeatureCollection: " + in.peek()); } return featureCollection; }
Example 12
Source File: MessageBodyTypeAdapter.java From arcusplatform with Apache License 2.0 | 4 votes |
@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 13
Source File: Feature.java From mapbox-java with MIT License | 4 votes |
@Override public Feature read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); return null; } jsonReader.beginObject(); String type = null; BoundingBox bbox = null; String id = null; Geometry geometry = null; JsonObject properties = null; while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); continue; } switch (name) { case "type": TypeAdapter<String> strTypeAdapter = this.stringTypeAdapter; if (strTypeAdapter == null) { strTypeAdapter = gson.getAdapter(String.class); this.stringTypeAdapter = strTypeAdapter; } type = strTypeAdapter.read(jsonReader); break; case "bbox": TypeAdapter<BoundingBox> boundingBoxTypeAdapter = this.boundingBoxTypeAdapter; if (boundingBoxTypeAdapter == null) { boundingBoxTypeAdapter = gson.getAdapter(BoundingBox.class); this.boundingBoxTypeAdapter = boundingBoxTypeAdapter; } bbox = boundingBoxTypeAdapter.read(jsonReader); break; case "id": strTypeAdapter = this.stringTypeAdapter; if (strTypeAdapter == null) { strTypeAdapter = gson.getAdapter(String.class); this.stringTypeAdapter = strTypeAdapter; } id = strTypeAdapter.read(jsonReader); break; case "geometry": TypeAdapter<Geometry> geometryTypeAdapter = this.geometryTypeAdapter; if (geometryTypeAdapter == null) { geometryTypeAdapter = gson.getAdapter(Geometry.class); this.geometryTypeAdapter = geometryTypeAdapter; } geometry = geometryTypeAdapter.read(jsonReader); break; case "properties": TypeAdapter<JsonObject> jsonObjectTypeAdapter = this.jsonObjectTypeAdapter; if (jsonObjectTypeAdapter == null) { jsonObjectTypeAdapter = gson.getAdapter(JsonObject.class); this.jsonObjectTypeAdapter = jsonObjectTypeAdapter; } properties = jsonObjectTypeAdapter.read(jsonReader); break; default: jsonReader.skipValue(); } } jsonReader.endObject(); return new Feature(type, bbox, id, geometry, properties); }
Example 14
Source File: D.java From MiBandDecompiled with Apache License 2.0 | 4 votes |
public Calendar a(JsonReader jsonreader) { int i = 0; if (jsonreader.peek() == JsonToken.NULL) { jsonreader.nextNull(); return null; } jsonreader.beginObject(); int j = 0; int k = 0; int l = 0; int i1 = 0; int j1 = 0; do { if (jsonreader.peek() == JsonToken.END_OBJECT) { break; } String s = jsonreader.nextName(); int k1 = jsonreader.nextInt(); if ("year".equals(s)) { j1 = k1; } else if ("month".equals(s)) { i1 = k1; } else if ("dayOfMonth".equals(s)) { l = k1; } else if ("hourOfDay".equals(s)) { k = k1; } else if ("minute".equals(s)) { j = k1; } else if ("second".equals(s)) { i = k1; } } while (true); jsonreader.endObject(); return new GregorianCalendar(j1, i1, l, k, j, i); }
Example 15
Source File: MessageJsonSerializer.java From javaide with GNU General Public License v3.0 | 4 votes |
@Override public Message read(JsonReader in) throws IOException { in.beginObject(); Message.Kind kind = Message.Kind.UNKNOWN; String text = ""; String rawMessage = null; ImmutableList.Builder<SourceFilePosition> positions = new ImmutableList.Builder<SourceFilePosition>(); SourceFile legacyFile = SourceFile.UNKNOWN; SourcePosition legacyPosition = SourcePosition.UNKNOWN; while (in.hasNext()) { String name = in.nextName(); if (name.equals(KIND)) { //noinspection StringToUpperCaseOrToLowerCaseWithoutLocale Message.Kind theKind = KIND_STRING_ENUM_MAP.inverse() .get(in.nextString().toLowerCase()); kind = (theKind != null) ? theKind : Message.Kind.UNKNOWN; } else if (name.equals(TEXT)) { text = in.nextString(); } else if (name.equals(RAW_MESSAGE)) { rawMessage = in.nextString(); } else if (name.equals(SOURCE_FILE_POSITIONS)) { switch (in.peek()) { case BEGIN_ARRAY: in.beginArray(); while(in.hasNext()) { positions.add(mSourceFilePositionTypeAdapter.read(in)); } in.endArray(); break; case BEGIN_OBJECT: positions.add(mSourceFilePositionTypeAdapter.read(in)); break; default: in.skipValue(); break; } } else if (name.equals(LEGACY_SOURCE_PATH)) { legacyFile = new SourceFile(new File(in.nextString())); } else if (name.equals(LEGACY_POSITION)) { legacyPosition = mSourcePositionTypeAdapter.read(in); } else { in.skipValue(); } } in.endObject(); if (legacyFile != SourceFile.UNKNOWN || legacyPosition != SourcePosition.UNKNOWN) { positions.add(new SourceFilePosition(legacyFile, legacyPosition)); } if (rawMessage == null) { rawMessage = text; } ImmutableList<SourceFilePosition> sourceFilePositions = positions.build(); if (!sourceFilePositions.isEmpty()) { return new Message(kind, text, rawMessage, sourceFilePositions); } else { return new Message(kind, text, rawMessage, ImmutableList.of(SourceFilePosition.UNKNOWN)); } }
Example 16
Source File: ChannelReader.java From packagedrone with Eclipse Public License 1.0 | 4 votes |
private Map<String, ArtifactInformation> readArtifacts ( final JsonReader jr ) throws IOException { jr.beginObject (); final Map<String, ArtifactInformation> result = new HashMap<> (); while ( jr.hasNext () ) { final String id = jr.nextName (); jr.beginObject (); String name = null; Long size = null; Instant creationTimestamp = null; String parentId = null; Set<String> childIds = Collections.emptySet (); Set<String> facets = Collections.emptySet (); String virtualizerAspectId = null; List<ValidationMessage> validationMessages = Collections.emptyList (); Map<MetaKey, String> extractedMetaData = Collections.emptyMap (); Map<MetaKey, String> providedMetaData = Collections.emptyMap (); while ( jr.hasNext () ) { final String ele = jr.nextName (); switch ( ele ) { case "name": name = jr.nextString (); break; case "size": size = jr.nextLong (); break; case "date": creationTimestamp = readTime ( jr ); break; case "parentId": parentId = jr.nextString (); break; case "childIds": childIds = readSet ( jr ); break; case "facets": facets = readSet ( jr ); break; case "virtualizerAspectId": virtualizerAspectId = jr.nextString (); break; case "extractedMetaData": extractedMetaData = readMetadata ( jr ); break; case "providedMetaData": providedMetaData = readMetadata ( jr ); break; case "validationMessages": validationMessages = readValidationMessages ( jr ); break; default: jr.skipValue (); break; } } jr.endObject (); if ( id == null || name == null || size == null || creationTimestamp == null ) { throw new IOException ( "Missing values for artifact" ); } this.numberOfBytes += size; result.put ( id, new ArtifactInformation ( id, parentId, childIds, name, size, creationTimestamp, facets, validationMessages, providedMetaData, extractedMetaData, virtualizerAspectId ) ); } jr.endObject (); return result; }
Example 17
Source File: ExtractorsTypeAdapter.java From zap-extensions with Apache License 2.0 | 4 votes |
@Override public Extractors read(JsonReader in) throws IOException { Extractors extractors = new Extractors(); in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case Extractors.TYPE_FUNC: in.beginArray(); List<String> funcs = new ArrayList<>(); while (in.hasNext()) { String aFunc = in.nextString(); aFunc = fixPattern(aFunc); funcs.add(aFunc); } extractors.setFunc(funcs); in.endArray(); break; case Extractors.TYPE_FILENAME: in.beginArray(); List<String> filenames = new ArrayList<>(); while (in.hasNext()) { String aFilename = in.nextString(); aFilename = fixPattern(aFilename); filenames.add(aFilename); } extractors.setFilename(filenames); in.endArray(); break; case Extractors.TYPE_FILECONTENT: in.beginArray(); List<String> filecontents = new ArrayList<>(); while (in.hasNext()) { String aFilecontent = in.nextString(); aFilecontent = fixPattern(aFilecontent); filecontents.add(aFilecontent); } extractors.setFilecontent(filecontents); in.endArray(); break; case Extractors.TYPE_URI: in.beginArray(); List<String> uris = new ArrayList<>(); while (in.hasNext()) { String aUri = in.nextString(); aUri = fixPattern(aUri); uris.add(aUri); } extractors.setUri(uris); in.endArray(); break; case Extractors.TYPE_HASHES: in.beginObject(); Map<String, String> hashes = new HashMap<String, String>(); while (in.hasNext()) { String key = in.nextName(); String value = in.nextString(); hashes.put(key, value); } extractors.setHashes(hashes); in.endObject(); break; case "filecontentreplace": in.beginArray(); while (in.hasNext()) { @SuppressWarnings("unused") String ignore = in.nextString(); // Ignore } in.endArray(); break; } } in.endObject(); return extractors; }
Example 18
Source File: JsonPropertyConsumer.java From cloud-odata-java with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") private Object readComplexProperty(final JsonReader reader, final EntityComplexPropertyInfo complexPropertyInfo, final Object typeMapping) throws EdmException, EntityProviderException, IOException { if (reader.peek().equals(JsonToken.NULL)) { reader.nextNull(); if (complexPropertyInfo.isMandatory()) { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(complexPropertyInfo.getName())); } return null; } reader.beginObject(); Map<String, Object> data = new HashMap<String, Object>(); Map<String, Object> mapping; if (typeMapping != null) { if (typeMapping instanceof Map) { mapping = (Map<String, Object>) typeMapping; } else { throw new EntityProviderException(EntityProviderException.INVALID_MAPPING.addContent(complexPropertyInfo.getName())); } } else { mapping = new HashMap<String, Object>(); } while (reader.hasNext()) { String childName = reader.nextName(); if (FormatJson.METADATA.equals(childName)) { reader.beginObject(); childName = reader.nextName(); if (!FormatJson.TYPE.equals(childName)) { throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE.addContent(FormatJson.TYPE).addContent(FormatJson.METADATA)); } String actualTypeName = reader.nextString(); String expectedTypeName = complexPropertyInfo.getType().getNamespace() + Edm.DELIMITER + complexPropertyInfo.getType().getName(); if (!expectedTypeName.equals(actualTypeName)) { throw new EntityProviderException(EntityProviderException.INVALID_ENTITYTYPE.addContent(expectedTypeName).addContent(actualTypeName)); } reader.endObject(); } else { EntityPropertyInfo childPropertyInfo = complexPropertyInfo.getPropertyInfo(childName); if (childPropertyInfo == null) { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(childName)); } Object childData = readPropertyValue(reader, childPropertyInfo, mapping.get(childName)); if (data.containsKey(childName)) { throw new EntityProviderException(EntityProviderException.DOUBLE_PROPERTY.addContent(childName)); } data.put(childName, childData); } } reader.endObject(); return data; }
Example 19
Source File: IndexTMDB.java From quaerite with Apache License 2.0 | 4 votes |
private static Movie nextMovie(JsonReader jsonReader) throws IOException { String id = jsonReader.nextName(); jsonReader.beginObject(); Movie movie = new Movie(); movie.id = id; while (jsonReader.hasNext()) { String name = getName(jsonReader, ""); if ("adult".equals(name)) { movie.adult = getBoolean(jsonReader, false); } else if ("genres".equals(name)) { movie.setGenres(extractNamesFromArrayOfObjections(jsonReader)); } else if ("original_language".equals(name)) { movie.originalLanguage = getString(jsonReader, StringUtils.EMPTY); } else if ("original_title".equals(name)) { movie.originalTitle = getString(jsonReader, StringUtils.EMPTY); } else if ("overview".equals(name)) { movie.overview = getString(jsonReader, StringUtils.EMPTY); } else if ("title".equals(name)) { movie.title = getString(jsonReader, StringUtils.EMPTY); } else if ("popularity".equals(name)) { movie.popularity = getDouble(jsonReader, -1.0); } else if ("vote_count".equals(name)) { movie.voteCount = getInt(jsonReader, -1); } else if ("vote_average".equals(name)) { movie.voteAverage = getDouble(jsonReader, -1.0f); } else if ("cast".equals(name)) { movie.setCast(extractNamesFromArrayOfObjections(jsonReader)); } else if ("directors".equals(name)) { movie.setDirectors(extractNamesFromArrayOfObjections(jsonReader)); } else if ("release_date".equals(name)) { movie.setReleaseDate(getString(jsonReader, "")); } else if ("production_companies".equals(name)) { movie.setProductionCompanies(extractNamesFromArrayOfObjections(jsonReader)); } else if ("budget".equals(name)) { movie.budget = getDouble(jsonReader, -1.0); } else if ("revenue".equals(name)) { movie.revenue = getDouble(jsonReader, -1.0); } else { jsonReader.skipValue(); } } jsonReader.endObject(); return movie; }
Example 20
Source File: StructuredJsonExamplesProvider.java From vw-webservice with BSD 3-Clause "New" or "Revised" License | 2 votes |
private void readFeatureIntoNamespace(long exampleNumber, NamespaceBuilder nsBuilder, JsonReader jsonReader) throws IOException { jsonReader.beginObject(); String name = null; Float value = null; boolean nameRead = false, valueRead = false; while (jsonReader.hasNext()) { String propertyNameOriginal = jsonReader.nextName(); String propertyName = propertyNameOriginal.toLowerCase(); if (propertyName.equals(StructuredJsonPropertyNames.FEATURE_NAME_PROPERTY)) { if (nameRead) { throw new ExampleFormatException(exampleNumber, "The 'name' property can only appear once in a feature!"); } name = jsonReader.nextString(); //feature name should never be null, so not doing the null check here. if it's null, let the exception //be propagated. nameRead = true; } else if (propertyName.equals(StructuredJsonPropertyNames.FEATURE_VALUE_PROPERTY)) { if (valueRead) { throw new ExampleFormatException(exampleNumber, "The 'value' property can only appear once in a feature!"); } if (jsonReader.peek() == JsonToken.NULL) jsonReader.nextNull(); else value = Float.valueOf((float) jsonReader.nextDouble()); valueRead = true; } else { throw new ExampleFormatException(exampleNumber, "Unknown property: " + propertyNameOriginal + " found while reading feature!"); } } jsonReader.endObject(); if (StringUtils.isBlank(name) == false) //add feature only if the name exists. nsBuilder.addFeature(name, value); }