List of usage examples for com.google.gson.stream JsonToken NAME
JsonToken NAME
To view the source code for com.google.gson.stream JsonToken NAME.
Click Source Link
From source file:com.github.kevinsawicki.halligan.Resource.java
License:Open Source License
/** * Parse resources from current value//from ww w . j a va2 s. c o m * * @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(); }
From source file:com.github.lindenb.gatkui.Json2Xml.java
License:Open Source License
private void parse(String label, JsonReader r) throws Exception { if (!r.hasNext()) return;/* www. ja v a2 s .c o m*/ JsonToken token = r.peek(); switch (token) { case NAME: break; case BEGIN_OBJECT: { r.beginObject(); parseObject(label, r); break; } case END_OBJECT: { break; } case BEGIN_ARRAY: { r.beginArray(); parseArray(label, r); break; } case END_ARRAY: { break; } case NULL: { r.nextNull(); w.writeEmptyElement(NS, "null"); if (label != null) w.writeAttribute("name", label); break; } case STRING: { w.writeStartElement(NS, "string"); if (label != null) w.writeAttribute("name", label); w.writeCharacters(r.nextString()); w.writeEndElement(); break; } case NUMBER: { w.writeStartElement(NS, "number"); if (label != null) w.writeAttribute("name", label); String s; try { s = String.valueOf(r.nextLong()); } catch (Exception err) { s = String.valueOf(r.nextDouble()); } w.writeCharacters(s); w.writeEndElement(); break; } case BOOLEAN: { w.writeStartElement(NS, "boolean"); if (label != null) w.writeAttribute("name", label); w.writeCharacters(String.valueOf(r.nextBoolean())); w.writeEndElement(); break; } case END_DOCUMENT: { break; } default: throw new IllegalStateException(token.name()); } }
From source file:com.github.lindenb.gatkui.Json2Xml.java
License:Open Source License
private void parseObject(String label, JsonReader r) throws Exception { w.writeStartElement(NS, "object"); if (label != null) w.writeAttribute("name", label); for (;;) {/* w w w. j a v a 2s . c om*/ if (r.peek() == JsonToken.END_OBJECT) break; if (r.peek() != JsonToken.NAME) throw new IllegalStateException(r.peek().name()); String s = r.nextName(); parse(s, r); } w.writeEndElement(); r.endObject(); }
From source file:com.goide.dlv.JsonReaderEx.java
License:Apache License
/** * Returns the type of the next token without consuming it. */// w w w . j a v a2 s . c o m @NotNull public JsonToken peek() { 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(); } }
From source file:com.gs.reladomo.serial.gson.GsonWrappedListTypedAdapter.java
License:Apache License
@Override public SerializedList<U, T> read(JsonReader jsonReader) throws IOException { ReladomoDeserializer deserializer;// w ww.j a v a 2 s.c om if (this.typeClass == null) { deserializer = new ReladomoDeserializer(); } else { deserializer = new ReladomoDeserializer(typeClass); } deserializer.setIgnoreUnknown(); JsonDeserializerState state = JsonDeserializerState.ListStartState.INSTANCE; while (true) { JsonToken nextToken = jsonReader.peek(); //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT; if (JsonToken.BEGIN_OBJECT == nextToken) { jsonReader.beginObject(); state = state.startObject(deserializer); } else if (JsonToken.END_OBJECT == nextToken) { jsonReader.endObject(); state = state.endObject(deserializer); } else if (JsonToken.BEGIN_ARRAY == nextToken) { jsonReader.beginArray(); state = state.startArray(deserializer); } else if (JsonToken.END_ARRAY == nextToken) { jsonReader.endArray(); state = state.endArray(deserializer); } else if (JsonToken.BOOLEAN == nextToken) { if (jsonReader.nextBoolean()) { state = state.valueTrue(deserializer); } else { state = state.valueFalse(deserializer); } } else if (JsonToken.NAME == nextToken) { String name = jsonReader.nextName(); state = state.fieldName(name, deserializer); } else if (JsonToken.NUMBER == nextToken) { String value = jsonReader.nextString(); state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss } else if (JsonToken.STRING == nextToken) { String value = jsonReader.nextString(); Attribute attribute = deserializer.getCurrentAttribute(); if (attribute instanceof TimestampAttribute) { Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value); state = state.valueTimestamp(timestamp, deserializer); } else { state = state.valueString(value, deserializer); } } else if (JsonToken.NULL == nextToken) { jsonReader.nextNull(); state = state.valueNull(deserializer); } else if (JsonToken.END_DOCUMENT == nextToken) { break; } } return deserializer.getDeserializedResultAsList(); }
From source file:com.gs.reladomo.serial.gson.GsonWrappedTypedAdapter.java
License:Apache License
@Override public Serialized<T> read(JsonReader jsonReader) throws IOException { ReladomoDeserializer deserializer;//from w ww .j a v a2 s .c o m if (this.typeClass == null) { deserializer = new ReladomoDeserializer(); } else { deserializer = new ReladomoDeserializer(typeClass); } deserializer.setIgnoreUnknown(); JsonDeserializerState state = JsonDeserializerState.NormalParserState.INSTANCE; while (true) { JsonToken nextToken = jsonReader.peek(); //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT; if (JsonToken.BEGIN_OBJECT == nextToken) { jsonReader.beginObject(); state = state.startObject(deserializer); } else if (JsonToken.END_OBJECT == nextToken) { jsonReader.endObject(); state = state.endObject(deserializer); } else if (JsonToken.BEGIN_ARRAY == nextToken) { jsonReader.beginArray(); state = state.startArray(deserializer); } else if (JsonToken.END_ARRAY == nextToken) { jsonReader.endArray(); state = state.endArray(deserializer); } else if (JsonToken.BOOLEAN == nextToken) { if (jsonReader.nextBoolean()) { state = state.valueTrue(deserializer); } else { state = state.valueFalse(deserializer); } } else if (JsonToken.NAME == nextToken) { String name = jsonReader.nextName(); state = state.fieldName(name, deserializer); } else if (JsonToken.NUMBER == nextToken) { String value = jsonReader.nextString(); state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss } else if (JsonToken.STRING == nextToken) { String value = jsonReader.nextString(); Attribute attribute = deserializer.getCurrentAttribute(); if (attribute instanceof TimestampAttribute) { Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value); state = state.valueTimestamp(timestamp, deserializer); } else { state = state.valueString(value, deserializer); } } else if (JsonToken.NULL == nextToken) { jsonReader.nextNull(); state = state.valueNull(deserializer); } else if (JsonToken.END_DOCUMENT == nextToken) { break; } } return deserializer.getDeserializedResult(); }
From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java
License:Open Source License
/** * Checks the next {@link JsonToken} to decide the next appropriate parsing method. * * @param in {@link JsonReader} object used for parsing * @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object * @throws IOException signals that there has been an IO exception *///from www . java 2 s . c om private void parseNext(JsonReader in, HashMap<String, Object> objMap) throws IOException { JsonToken token = in.peek(); String lastName = ""; if (token == JsonToken.NAME) { lastName = in.nextName(); token = in.peek(); } switch (token) { case BEGIN_ARRAY: parseArray(in, objMap, lastName); break; case BEGIN_OBJECT: parseObject(in, objMap, lastName); break; case STRING: objMap.put(lastName, in.nextString()); break; case NUMBER: objMap.put(lastName, in.nextDouble()); break; case BOOLEAN: objMap.put(lastName, in.nextBoolean()); break; default: throw new IOException("Unexpected JSON token encountered"); } collapseMap(objMap); }
From source file:com.magnet.android.mms.request.marshall.GsonStreamReader.java
License:Open Source License
@Override public int getTokenType() throws MarshallingException { int result = TOKEN_TYPE_FIELD_VALUE; JsonToken tokenType;// www. j a v a 2 s.c o m try { tokenType = jr.peek(); } catch (Exception e) { throw new MarshallingException(e); } if (tokenType == JsonToken.BEGIN_OBJECT) { result = TOKEN_TYPE_START_OBJECT; } else if (tokenType == JsonToken.BEGIN_ARRAY) { result = TOKEN_TYPE_START_ARRAY; } else if (tokenType == JsonToken.END_OBJECT) { result = TOKEN_TYPE_END_OBJECT; } else if (tokenType == JsonToken.END_ARRAY) { result = TOKEN_TYPE_END_ARRAY; } else if (tokenType == JsonToken.NAME) { result = TOKEN_TYPE_FIELD_NAME; } else if (tokenType == JsonToken.NULL) { result = TOKEN_TYPE_NULL; } else if (tokenType == JsonToken.END_DOCUMENT) { result = TOKEN_TYPE_END_DOCUMENT; } return result; }
From source file:com.nridge.core.io.gson.JSONDocument.java
License:Open Source License
/** * Parses an JSON stream and loads it into an internally managed * document instance.//from w w w. ja v a2 s .co m * * @param aReader Json reader stream instance. * * @throws IOException I/O related exception. */ private void loadDocument(JsonReader aReader, Document aDocument) throws IOException { DataBag dataBag; JsonToken jsonToken; DataField dataField; String jsonName, jsonValue, jsonTitle; aReader.beginObject(); jsonToken = aReader.peek(); while (jsonToken == JsonToken.NAME) { jsonName = aReader.nextName(); jsonTitle = Field.nameToTitle(jsonName); jsonToken = aReader.peek(); switch (jsonToken) { case BOOLEAN: dataBag = aDocument.getBag(); dataField = new DataField(jsonName, jsonTitle, aReader.nextBoolean()); dataBag.add(dataField); break; case NUMBER: dataBag = aDocument.getBag(); jsonValue = aReader.nextString(); if (StringUtils.contains(jsonValue, StrUtl.CHAR_DOT)) dataField = new DataField(jsonName, jsonTitle, Double.valueOf(jsonValue)); else dataField = new DataField(jsonName, jsonTitle, Long.valueOf(jsonValue)); dataBag.add(dataField); break; case STRING: dataBag = aDocument.getBag(); jsonValue = aReader.nextString(); Date dateValue = DatUtl.detectCreateDate(jsonValue); if (dateValue != null) dataField = new DataField(jsonName, jsonTitle, dateValue); else dataField = new DataField(Field.Type.Text, jsonName, jsonTitle, jsonValue); dataBag.add(dataField); break; case NULL: dataBag = aDocument.getBag(); aReader.nextNull(); dataField = new DataField(Field.Type.Text, jsonName, jsonTitle); dataBag.add(dataField); break; case BEGIN_ARRAY: aReader.beginArray(); if (isNextTokenAnObject(aReader)) { Document childDocument = new Document(jsonTitle); childDocument.setName(jsonName); aDocument.addRelationship(jsonTitle, childDocument); loadDocumentArray(aReader, childDocument); } else { dataBag = aDocument.getBag(); dataField = new DataField(Field.Type.Text, jsonName, jsonTitle); jsonToken = aReader.peek(); while (jsonToken != JsonToken.END_ARRAY) { jsonValue = aReader.nextString(); dataField.addValue(jsonValue); jsonToken = aReader.peek(); } dataBag.add(dataField); } aReader.endArray(); break; case BEGIN_OBJECT: Document childDocument = new Document(jsonTitle); childDocument.setName(jsonName); aDocument.addRelationship(jsonTitle, childDocument); loadDocument(aReader, childDocument); break; default: aReader.skipValue(); break; } jsonToken = aReader.peek(); } aReader.endObject(); }
From source file:com.sap.core.odata.core.ep.consumer.JsonLinkConsumer.java
License:Apache License
/** * Reads a collection of links, optionally wrapped in a "d" object, * and optionally wrapped in an "results" object, where an additional "__count" * object could appear on the same level as the "results". * @param reader//from w ww . j a v a2s. c o m * @param entitySet * @return links as List of Strings * @throws EntityProviderException */ public List<String> readLinks(final JsonReader reader, final EdmEntitySet entitySet) throws EntityProviderException { List<String> links = null; int openedObjects = 0; try { String nextName; if (reader.peek() == JsonToken.BEGIN_ARRAY) { nextName = FormatJson.RESULTS; } else { reader.beginObject(); openedObjects++; nextName = reader.nextName(); } if (FormatJson.D.equals(nextName)) { if (reader.peek() == JsonToken.BEGIN_ARRAY) { nextName = FormatJson.RESULTS; } else { reader.beginObject(); openedObjects++; nextName = reader.nextName(); } } FeedMetadataImpl feedMetadata = new FeedMetadataImpl(); if (FormatJson.COUNT.equals(nextName)) { JsonFeedConsumer.readInlineCount(reader, feedMetadata); nextName = reader.nextName(); } if (FormatJson.RESULTS.equals(nextName)) { links = readLinksArray(reader); } else { throw new EntityProviderException(EntityProviderException.INVALID_CONTENT .addContent(FormatJson.RESULTS).addContent(nextName)); } if (reader.hasNext() && reader.peek() == JsonToken.NAME) { if (FormatJson.COUNT.equals(reader.nextName())) { JsonFeedConsumer.readInlineCount(reader, feedMetadata); } else { throw new EntityProviderException(EntityProviderException.INVALID_CONTENT .addContent(FormatJson.COUNT).addContent(nextName)); } } for (; openedObjects > 0; openedObjects--) { reader.endObject(); } reader.peek(); // to assert end of document } catch (final IOException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } catch (final IllegalStateException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } return links; }