List of usage examples for com.google.gson.stream JsonReader peek
public JsonToken peek() throws IOException
From source file:com.gilecode.yagson.adapters.DefaultTypeAdapterRuntimeWrapper.java
License:Apache License
@Override public T read(JsonReader in, ReadContext ctx) throws IOException { // Although similar check exists in ReadContext.doRead(), we need to duplicate it here, as wrappers hide // simple delegate adapters if (in.peek() == JsonToken.BEGIN_OBJECT && AdapterUtils.isSimpleTypeAdapter(defaultTypeAdapter)) { // if default adapter is simple and '{' is found, we expect and parse type advice here, and fail otherwise return TypeUtils.readTypeAdvisedValue(gson, in, formalType, ctx); } else {/*from w w w . ja v a 2 s.c o m*/ // no type advice, or defaultTypeAdapter is able to process type advice itself, as it is non-Simple return defaultTypeAdapter.read(in, ctx); } }
From source file:com.gilecode.yagson.adapters.TypeAdviceReadingSimpleAdapterWrapper.java
License:Apache License
@Override public T read(JsonReader in, ReadContext ctx) throws IOException { if (in.peek() == JsonToken.BEGIN_OBJECT) { // if '{' is found for the simple type, we expect and parse type advice here, and fail otherwise return TypeUtils.readTypeAdvisedValue(gson, in, null, ctx); } else {/*from w w w. j a v a 2 s .c om*/ // no type advice, use delegate return delegateTypeAdapter.read(in, ctx); } }
From source file:com.gilecode.yagson.adapters.TypeAdvisableComplexTypeAdapter.java
License:Apache License
public T read(JsonReader in, ReadContext ctx) throws IOException { JsonToken nextToken = in.peek(); if (nextToken == JsonToken.NULL) { in.nextNull();/* w w w.j av a2 s . c o m*/ return null; } else if (nextToken == JsonToken.STRING) { // for complex type adapters, each string is a reference, no isReferenceString() match required String reference = in.nextString(); T referenced = ctx.refsContext().getReferencedObject(reference); return referenced; } return readOptionallyAdvisedInstance(in, ctx); }
From source file:com.gilecode.yagson.ReadContext.java
License:Apache License
public <T> T doRead(JsonReader reader, TypeAdapter<T> typeAdapter, String pathElement) throws IOException { // PROBLEM: as JsonReader does not support lookaheads for more than one token, we cannot // distinguish regular objects like "{field:value}" from type-advised primitives like // "{@type:Long, @val:1}" here. ///*from w w w. j av a2s.co m*/ // SOLUTION: all non-Simple TypeAdapters shall handle type advices themselves, so we delegate to them. // For simple delegates, if '{' found, we expect and parse type advice here, and fail otherwise if (AdapterUtils.isSimpleTypeAdapter(typeAdapter) && reader.peek() == JsonToken.BEGIN_OBJECT && gson.getTypeInfoPolicy().isEnabled()) { // simple type adapters do not use '{', so this is definitely a type advice. Use a wrapper adapter // to correctly process it typeAdapter = new TypeAdviceReadingSimpleAdapterWrapper<T>(gson, typeAdapter); } return rctx.doRead(reader, typeAdapter, pathElement, this); }
From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java
License:Apache License
@Override public Positions read(JsonReader in) throws IOException { Positions parsed;//from ww w. j ava 2 s . c o m JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); parsed = null; } else if (peek == JsonToken.BEGIN_ARRAY) { parsed = parsePositions(in); } else { throw new IllegalArgumentException("The json must be an array or null: " + in.peek()); } return parsed; }
From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java
License:Apache License
private Positions parsePositions(JsonReader in) throws IOException { Optional<Positions> parsed = Optional.absent(); if (in.peek() != JsonToken.BEGIN_ARRAY) { throw new IllegalArgumentException("The given json is not a valid positions"); }// www .j av a2 s .com in.beginArray(); if (in.peek() == JsonToken.NUMBER) { parsed = Optional.of(parseSinglePosition(in)); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { while (in.hasNext()) { Positions thisPositions = parsePositions(in); // fix bug #30: according to the recursion (i.e. the array structure; // recognize that we came from a recursion because no parsed has no // value yet): convert the already parsed Positions to the // LinearPositions/AreaPositions matching the recursion level if (parsed.equals(Optional.absent()) && thisPositions instanceof LinearPositions) { AreaPositions areaPositions = new AreaPositions( ImmutableList.of((LinearPositions) thisPositions)); parsed = Optional.of((Positions) areaPositions); } else if (parsed.equals(Optional.absent()) && thisPositions instanceof AreaPositions) { MultiDimensionalPositions multiPositions = new MultiDimensionalPositions( ImmutableList.of((AreaPositions) thisPositions)); parsed = Optional.of((Positions) multiPositions); } else { // mergeFn() does all the rest, if parsed has a value parsed = parsed.transform(mergeFn(thisPositions)).or(Optional.of(thisPositions)); } } } in.endArray(); return parsed.orNull(); }
From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java
License:Apache License
private Positions parseSinglePosition(JsonReader in) throws IOException { Positions parsed;/*from w w w . j a v a 2 s . c o m*/ double lon = in.nextDouble(); double lat = in.nextDouble(); double alt = 0.0; if (in.hasNext()) { alt = in.nextDouble(); } // Skip eventual altitude or other dimensions while (in.peek() != JsonToken.END_ARRAY) { in.skipValue(); } parsed = new SinglePosition(Coordinates.of(lon, lat, alt)); return parsed; }
From source file:com.github.gv2011.jsong.JsongAdapter.java
License:Open Source License
private JsonNode deserialize(final JsonFactoryImp jf, final JsonReader in) { return call(() -> { switch (in.peek()) { case STRING: return jf.primitive(in.nextString()); case NUMBER: return jf.primitive(new BigDecimal(in.nextString())); case BOOLEAN: return jf.primitive(in.nextBoolean()); case NULL: in.nextNull();// w ww. j av a 2 s .c o m return jf.jsonNull(); case BEGIN_ARRAY: in.beginArray(); final JsonList list = XStream.fromIterator(new It(jf, in)).collect(jf.toJsonList()); in.endArray(); return list; case BEGIN_OBJECT: in.beginObject(); final JsonObject obj = XStream.fromIterator(new Itm(jf, in)).collect(jf.toJsonObject()); in.endObject(); return obj; case NAME: case END_DOCUMENT: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }); }
From source file:com.github.kevinsawicki.halligan.Resource.java
License:Open Source License
/** * Fill this resource by parsing the next object in the reader * * @param reader/*from w w w .j a v a 2 s . c o m*/ * @return this resource * @throws IOException */ protected Resource parse(final JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext() && reader.peek() == NAME) { String name = reader.nextName(); if ("_links".equals(name)) parseLinks(reader); else if ("_embedded".equals(name)) parseResources(reader); else parseProperty(reader, name); } reader.endObject(); return this; }
From source file:com.github.kevinsawicki.halligan.Resource.java
License:Open Source License
/** * Parse resources from current value//ww w . j a v a 2 s . c om * * @param reader * @throws IOException */ protected void parseResources(final JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); JsonToken next = reader.peek(); switch (next) { case BEGIN_OBJECT: resources.put(name, Collections.singletonList(createResource().parse(reader))); break; case BEGIN_ARRAY: reader.beginArray(); List<Resource> entries = new ArrayList<Resource>(); while (reader.peek() == BEGIN_OBJECT) entries.add(createResource().parse(reader)); reader.endArray(); resources.put(name, entries); break; default: throw new IOException( "_embedded object value is a " + next.name() + " and must be an array or object"); } } reader.endObject(); }