Java Code Examples for com.google.gson.stream.JsonToken#BEGIN_OBJECT
The following examples show how to use
com.google.gson.stream.JsonToken#BEGIN_OBJECT .
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: CategoriesTypeAdapter.java From greenbeans with Apache License 2.0 | 6 votes |
@Override public Categories read(JsonReader reader) throws IOException { reader.beginObject(); checkState(NAME_CATEGORIES.equals(reader.nextName())); reader.beginArray(); ImmutableList.Builder<Category> elements = ImmutableList.<Category> builder(); while (reader.hasNext()) { if (reader.peek() == JsonToken.BEGIN_OBJECT) { elements.add(gson.getAdapter(Category.class).read(reader)); } else { elements.add(readCategory(reader.nextString())); } } reader.endArray(); reader.endObject(); return new Categories(elements.build()); }
Example 2
Source File: JsonLdSerializer.java From schemaorg-java with Apache License 2.0 | 6 votes |
private ListMultimap<String, Thing> readReverse(JsonReader reader) throws IOException { reader.beginObject(); ListMultimap<String, Thing> reverseMap = LinkedListMultimap.create(); while (reader.hasNext()) { String property = reader.nextName(); JsonToken token = reader.peek(); if (token == JsonToken.BEGIN_OBJECT) { reverseMap.put(property, readObject(reader)); } else if (token == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { reverseMap.put(property, readObject(reader)); } reader.endArray(); } else { throw new JsonLdSyntaxException( String.format( "Invalid value token %s in @reverse. Should be a JSON-LD object or an " + "array of JSON-LD objects", token)); } } reader.endObject(); return reverseMap; }
Example 3
Source File: JsonLdSerializer.java From schemaorg-java with Apache License 2.0 | 6 votes |
private List<JsonLdContext> readContextArray(JsonReader reader) throws IOException { reader.beginArray(); List<JsonLdContext> list = new ArrayList<>(); while (reader.hasNext()) { JsonToken token = reader.peek(); if (token == JsonToken.STRING) { list.add(readContextUrl(reader)); } else if (token == JsonToken.NULL) { reader.nextNull(); list.add(null); } else if (token == JsonToken.BEGIN_OBJECT) { list.add(readContextDef(reader)); } else { throw new JsonLdSyntaxException( String.format( "Invalid @context value token %s. Should be URL, context definition", token)); } } reader.endArray(); return list; }
Example 4
Source File: ArgumentsAdapter.java From salt-netapi-client with MIT License | 6 votes |
@Override public Arguments read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { throw new JsonParseException("null is not a valid value for Arguments"); } Arguments result = new Arguments(); jsonReader.beginArray(); while (jsonReader.hasNext()) { if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { Map<String, Object> arg = readObjectArgument(jsonReader); if (isKwarg(arg)) { arg.remove(KWARG_KEY); result.getKwargs().putAll(arg); } else { result.getArgs().add(arg); } } else { result.getArgs().add(JsonParser.GSON.fromJson(jsonReader, Object.class)); } } jsonReader.endArray(); return result; }
Example 5
Source File: GeometryAdapterFactory.java From geogson with Apache License 2.0 | 6 votes |
private Geometry<?> parseGeometries(JsonReader in) throws IOException { Optional<Geometry<?>> parsed = Optional.empty(); if (in.peek() != JsonToken.BEGIN_ARRAY) { throw new IllegalArgumentException("The given json is not a valid GeometryCollection"); } in.beginArray(); if (in.peek() == JsonToken.BEGIN_OBJECT) { LinkedList<Geometry<?>> geometries = new LinkedList<>(); while (in.hasNext()) { @SuppressWarnings("rawtypes") Geometry geometry = geometryAdapter.read(in); geometries.add(geometry); } parsed = Optional.<Geometry<?>>of(GeometryCollection.of(geometries)); } in.endArray(); return parsed.orElse(null); }
Example 6
Source File: JsonErrorDocumentConsumer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
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 7
Source File: GsonJsonMarshallerFactory.java From softlayer-java with MIT License | 5 votes |
@Override public List<T> read(JsonReader in) throws IOException { // We only take over if it's the beginning of an object, otherwise delegate if (in.peek() == JsonToken.BEGIN_OBJECT) { // Send back a mutable list of 1 List<T> result = new ArrayList<>(1); result.add(instanceDelegate.read(in)); return result; } return listDelegate.read(in); }
Example 8
Source File: JsonErrorDocumentDeserializer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
/** * * @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 9
Source File: ProteusTypeAdapterFactory.java From proteus with Apache License 2.0 | 5 votes |
public Map<String, Value> readData(JsonReader in) throws IOException { JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); return new HashMap<>(); } if (peek != JsonToken.BEGIN_OBJECT) { throw new JsonSyntaxException("data must be a Map<String, String>."); } Map<String, Value> data = new LinkedHashMap<>(); in.beginObject(); while (in.hasNext()) { JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in); String key = in.nextString(); Value value = VALUE_TYPE_ADAPTER.read(in); Value compiled = AttributeProcessor.staticPreCompile(value, context, PROTEUS_INSTANCE_HOLDER.getProteus().functions); if (compiled != null) { value = compiled; } Value replaced = data.put(key, value); if (replaced != null) { throw new JsonSyntaxException("duplicate key: " + key); } } in.endObject(); return data; }
Example 10
Source File: JsonEntryDeserializer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
/** * * @param navigationPropertyName * @throws IOException * @throws EntityProviderException * @throws EdmException */ private void readNavigationProperty(final String navigationPropertyName) throws IOException, EntityProviderException, EdmException { NavigationPropertyInfo navigationPropertyInfo = eia.getNavigationPropertyInfo(navigationPropertyName); if (navigationPropertyInfo == null) { throw new EntityProviderException(EntityProviderException.ILLEGAL_ARGUMENT.addContent(navigationPropertyName)); } JsonToken peek = reader.peek(); if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); String name = reader.nextName(); if (FormatJson.DEFERRED.equals(name)) { reader.beginObject(); String uri = reader.nextName(); if (FormatJson.URI.equals(uri)) { String value = reader.nextString(); entryMetadata.putAssociationUri(navigationPropertyInfo.getName(), value); } else { throw new EntityProviderException(EntityProviderException.ILLEGAL_ARGUMENT.addContent(uri)); } reader.endObject(); } else { handleInlineEntries(navigationPropertyName, name); } reader.endObject(); } else if (peek == JsonToken.NULL) { reader.nextNull(); } else { handleArray(navigationPropertyName); } }
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: JsonErrorDocumentConsumer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
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 13
Source File: GeometryAdapterFactory.java From geogson with Apache License 2.0 | 5 votes |
@Override public Geometry<?> read(JsonReader in) throws IOException { Geometry<?> geometry = null; if (in.peek() == JsonToken.NULL) { in.nextNull(); } else if (in.peek() == JsonToken.BEGIN_OBJECT) { in.beginObject(); Geometry.Type type = null; Positions positions = null; Geometry<?> geometries = null; while (in.hasNext()) { String name = in.nextName(); if ("type".equals(name)) { type = Geometry.Type.forValue(in.nextString()); } else if ("coordinates".equals(name)) { positions = positionsAdapter.read(in); } else if ("geometries".equals(name)) { geometries = readGeometries(in); } else { in.skipValue(); } } geometry = buildGeometry(type, positions, geometries); in.endObject(); } else { throw new IllegalArgumentException("The given json is not a valid Geometry: " + in.peek()); } return geometry; }
Example 14
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 15
Source File: FeatureAdapter.java From geogson with Apache License 2.0 | 5 votes |
@Override public Feature read(JsonReader in) throws IOException { Feature.Builder builder = Feature.builder(); if (in.peek() == JsonToken.NULL) { in.nextNull(); } else if (in.peek() == JsonToken.BEGIN_OBJECT) { in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (TYPE_NAME.equals(name)) { String value = in.nextString(); if(!value.equalsIgnoreCase(FEATURE_TYPE)) { throw new IllegalArgumentException("The given json is not a valid Feature, the type must be Feature, current type=" + value); } } else if (PROPERTIES_NAME.equals(name)) { readProperties(in, builder); } else if (GEOMETRY_NAME.equals(name)) { builder.withGeometry(gson.fromJson(in, Geometry.class)); } else if (ID_NAME.equals(name)) { builder.withId(Optional.ofNullable(in.nextString())); } else { // Skip unknown value. in.skipValue(); } } in.endObject(); } else { throw new IllegalArgumentException("The given json is not a valid Feature: " + in.peek()); } return builder.build(); }
Example 16
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 17
Source File: IpcdJsonReader.java From arcusplatform with Apache License 2.0 | 5 votes |
/** * 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 18
Source File: JsonProfile.java From bazel with Apache License 2.0 | 4 votes |
public JsonProfile(InputStream inputStream) throws IOException { try (JsonReader reader = new JsonReader( new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)))) { if (reader.peek() == JsonToken.BEGIN_OBJECT) { reader.beginObject(); while (reader.hasNext()) { String objectKey = reader.nextName(); if ("otherData".equals(objectKey)) { buildMetadata = parseBuildMetadata(reader); } else if ("traceEvents".equals(objectKey)) { traceEvents = TraceEvent.parseTraceEvents(reader); phaseSummaryStatistics = new PhaseSummaryStatistics(); TraceEvent lastPhaseEvent = null; Duration maxEndTime = Duration.ZERO; for (TraceEvent traceEvent : traceEvents) { if (traceEvent.timestamp() != null) { Duration curEndTime = traceEvent.timestamp(); if (traceEvent.duration() != null) { curEndTime = curEndTime.plus(traceEvent.duration()); } if (curEndTime.compareTo(maxEndTime) > 0) { maxEndTime = curEndTime; } } if (ProfilerTask.PHASE.description.equals(traceEvent.category())) { if (lastPhaseEvent != null) { phaseSummaryStatistics.addProfilePhase( ProfilePhase.getPhaseFromDescription(lastPhaseEvent.name()), traceEvent.timestamp().minus(lastPhaseEvent.timestamp())); } lastPhaseEvent = traceEvent; } } if (lastPhaseEvent != null) { phaseSummaryStatistics.addProfilePhase( ProfilePhase.getPhaseFromDescription(lastPhaseEvent.name()), maxEndTime.minus(lastPhaseEvent.timestamp())); } } else { reader.skipValue(); } } } } }
Example 19
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(); } }
Example 20
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(); } } }