List of usage examples for com.google.gson.stream JsonReader peek
public JsonToken peek() throws IOException
From source file:org.mitre.util.JsonUtils.java
License:Apache License
public static Map readMap(JsonReader reader) throws IOException { Map map = new HashMap<>(); reader.beginObject();/* w w w.jav a2 s . com*/ while (reader.hasNext()) { String name = reader.nextName(); Object value = null; switch (reader.peek()) { case STRING: value = reader.nextString(); break; case BOOLEAN: value = reader.nextBoolean(); break; case NUMBER: value = reader.nextLong(); break; default: logger.debug("Found unexpected entry"); reader.skipValue(); continue; } map.put(name, value); } reader.endObject(); return map; }
From source file:org.mitre.util.JsonUtils.java
License:Apache License
public static Set readSet(JsonReader reader) throws IOException { Set arraySet = null;//from ww w .j a va2 s .c o m reader.beginArray(); switch (reader.peek()) { case STRING: arraySet = new HashSet<>(); while (reader.hasNext()) { arraySet.add(reader.nextString()); } break; case NUMBER: arraySet = new HashSet<>(); while (reader.hasNext()) { arraySet.add(reader.nextLong()); } break; default: arraySet = new HashSet(); break; } reader.endArray(); return arraySet; }
From source file:org.mythdroid.services.GuideService.java
License:Open Source License
private void skipTo(JsonReader jr, JsonToken token) throws IOException { while (jr.hasNext() && jr.peek() != token) jr.skipValue();/* w w w .j a v a2 s. c o m*/ }
From source file:org.netxms.websvc.json.adapters.DateAdapter.java
License:Open Source License
@Override public Date read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();//from w w w . ja v a2 s. c om return null; } try { return new Date(reader.nextLong() * 1000); } catch (NumberFormatException e) { return null; } catch (IllegalStateException e) { return null; } }
From source file:org.netxms.websvc.json.adapters.InetAddressAdapter.java
License:Open Source License
@Override public InetAddress read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();// www . j a v a 2 s . c o m return null; } try { return InetAddress.getByName(reader.nextString()); } catch (UnknownHostException e) { return null; } }
From source file:org.netxms.websvc.json.adapters.MacAddressAdapter.java
License:Open Source License
@Override public MacAddress read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();//from w w w. jav a 2 s .c om return null; } try { return MacAddress.parseMacAddress(reader.nextString()); } catch (MacAddressFormatException e) { return null; } }
From source file:org.onos.yangtools.yang.data.codec.gson.JsonParserStream.java
License:Open Source License
public JsonParserStream parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException { // code copied from gson's JsonParser and Stream classes final boolean lenient = reader.isLenient(); reader.setLenient(true);//from w w w. j a v a 2 s .co m boolean isEmpty = true; try { reader.peek(); isEmpty = false; final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema( parentNode); read(reader, compositeNodeDataWithSchema); compositeNodeDataWithSchema.write(writer); return this; // return read(reader); } catch (final EOFException e) { if (isEmpty) { return this; // return JsonNull.INSTANCE; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (final MalformedJsonException e) { throw new JsonSyntaxException(e); } catch (final IOException e) { throw new JsonIOException(e); } catch (final NumberFormatException e) { throw new JsonSyntaxException(e); } catch (StackOverflowError | OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); } finally { reader.setLenient(lenient); } }
From source file:org.onos.yangtools.yang.data.codec.gson.JsonParserStream.java
License:Open Source License
public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException { switch (in.peek()) { case STRING:/* w w w . j a v a 2s .c om*/ case NUMBER: setValue(parent, in.nextString()); break; case BOOLEAN: setValue(parent, Boolean.toString(in.nextBoolean())); break; case NULL: in.nextNull(); setValue(parent, null); break; case BEGIN_ARRAY: in.beginArray(); while (in.hasNext()) { if (parent instanceof LeafNodeDataWithSchema) { read(in, parent); } else { final AbstractNodeDataWithSchema newChild = newArrayEntry(parent); read(in, newChild); } } in.endArray(); return; case BEGIN_OBJECT: final Set<String> namesakes = new HashSet<>(); in.beginObject(); /* * This allows parsing of incorrectly /as showcased/ * in testconf nesting of list items - eg. * lists with one value are sometimes serialized * without wrapping array. * */ if (isArray(parent)) { parent = newArrayEntry(parent); } while (in.hasNext()) { final String jsonElementName = in.nextName(); final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema()); final String localName = namespaceAndName.getName(); addNamespace(namespaceAndName.getUri()); if (namesakes.contains(jsonElementName)) { throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input."); } namesakes.add(jsonElementName); final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace( parent.getSchema(), localName, getCurrentNamespace()); if (childDataSchemaNodes.isEmpty()) { throw new IllegalStateException("Schema for node with name " + localName + " and namespace " + getCurrentNamespace() + " doesn't exist."); } final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent) .addChild(childDataSchemaNodes); /* * FIXME:anyxml data shouldn't be skipped but should be loaded somehow. * will be able to load anyxml which conforms to YANG data using these * parser, for other anyxml will be harder. */ if (newChild instanceof AnyXmlNodeDataWithSchema) { in.skipValue(); } else { read(in, newChild); } removeNamespace(); } in.endObject(); return; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: break; } }
From source file:org.opendaylight.controller.sal.rest.gson.JsonParser.java
License:Open Source License
public JsonElement parse(JsonReader reader) throws JsonIOException, JsonSyntaxException { // code copied from gson's JsonParser and Stream classes boolean lenient = reader.isLenient(); reader.setLenient(true);/*from w w w .ja v a 2 s .c om*/ boolean isEmpty = true; try { reader.peek(); isEmpty = false; return read(reader); } catch (EOFException e) { if (isEmpty) { return JsonNull.INSTANCE; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (MalformedJsonException e) { throw new JsonSyntaxException(e); } catch (IOException e) { throw new JsonIOException(e); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } catch (StackOverflowError | OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); } finally { reader.setLenient(lenient); } }
From source file:org.opendaylight.controller.sal.rest.gson.JsonParser.java
License:Open Source License
public JsonElement read(JsonReader in) throws IOException { switch (in.peek()) { case STRING://from ww w.jav a2s . co m return new JsonPrimitive(in.nextString()); case NUMBER: String number = in.nextString(); return new JsonPrimitive(new LazilyParsedNumber(number)); case BOOLEAN: return new JsonPrimitive(in.nextBoolean()); case NULL: in.nextNull(); return JsonNull.INSTANCE; case BEGIN_ARRAY: JsonArray array = new JsonArray(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: JsonObject object = new JsonObject(); in.beginObject(); while (in.hasNext()) { final String childName = in.nextName(); if (object.has(childName)) { throw new JsonSyntaxException("Duplicate name " + childName + " in JSON input."); } object.add(childName, read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }