List of usage examples for com.google.gson.stream JsonToken BOOLEAN
JsonToken BOOLEAN
To view the source code for com.google.gson.stream JsonToken BOOLEAN.
Click Source Link
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; }/*from www . ja v a2 s .c o m*/ Object o = peekStack(); if (o instanceof Iterator) { Object secondToTop = stack.get(stack.size() - 2); boolean isObject = secondToTop instanceof JsonElement && ((JsonElement) secondToTop).isJsonObject(); 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 JsonElement) { JsonElement el = (JsonElement) o; if (el.isJsonObject()) { return JsonToken.BEGIN_OBJECT; } else if (el.isJsonArray()) { return JsonToken.BEGIN_ARRAY; } else if (el.isJsonPrimitive()) { 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 (el.isJsonNull()) { return JsonToken.NULL; } throw new AssertionError(); } else if (o == SENTINEL_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } else { throw new AssertionError(); } }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public boolean nextBoolean() throws IOException { expect(JsonToken.BOOLEAN); return ((JsonPrimitive) popStack()).getAsBoolean(); }
From source file:com.cinchapi.concourse.util.Convert.java
License:Apache License
/** * Convert the next JSON object in the {@code reader} to a mapping that * associates each key with the Java objects that represent the * corresponding values.//from www .j a v a 2 s. com * * <p> * This method has the same rules and limitations as * {@link #jsonToJava(String)}. It simply uses a {@link JsonReader} to * handle reading an array of objects. * </p> * <p> * <strong>This method DOES NOT {@link JsonReader#close()} the * {@code reader}.</strong> * </p> * * @param reader the {@link JsonReader} that contains a stream of JSON * @return the JSON data in the form of a {@link Multimap} from keys to * values */ private static Multimap<String, Object> jsonToJava(JsonReader reader) { Multimap<String, Object> data = HashMultimap.create(); try { reader.beginObject(); JsonToken peek0; while ((peek0 = reader.peek()) != JsonToken.END_OBJECT) { String key = reader.nextName(); peek0 = reader.peek(); if (peek0 == JsonToken.BEGIN_ARRAY) { // If we have an array, add the elements individually. If // there are any duplicates in the array, they will be // filtered out by virtue of the fact that a HashMultimap // does not store dupes. reader.beginArray(); JsonToken peek = reader.peek(); do { Object value; if (peek == JsonToken.BOOLEAN) { value = reader.nextBoolean(); } else if (peek == JsonToken.NUMBER) { value = stringToJava(reader.nextString()); } else if (peek == JsonToken.STRING) { String orig = reader.nextString(); value = stringToJava(orig); if (orig.isEmpty()) { value = orig; } // If the token looks like a string, it MUST be // converted to a Java string unless it is a // masquerading double or an instance of Thrift // translatable class that has a special string // representation (i.e. Tag, Link) else if (orig.charAt(orig.length() - 1) != 'D' && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) { value = value.toString(); } } else if (peek == JsonToken.NULL) { reader.skipValue(); continue; } else { throw new JsonParseException("Cannot parse nested object or array within an array"); } data.put(key, value); } while ((peek = reader.peek()) != JsonToken.END_ARRAY); reader.endArray(); } else { Object value; if (peek0 == JsonToken.BOOLEAN) { value = reader.nextBoolean(); } else if (peek0 == JsonToken.NUMBER) { value = stringToJava(reader.nextString()); } else if (peek0 == JsonToken.STRING) { String orig = reader.nextString(); value = stringToJava(orig); if (orig.isEmpty()) { value = orig; } // If the token looks like a string, it MUST be // converted to a Java string unless it is a // masquerading double or an instance of Thrift // translatable class that has a special string // representation (i.e. Tag, Link) else if (orig.charAt(orig.length() - 1) != 'D' && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) { value = value.toString(); } } else if (peek0 == JsonToken.NULL) { reader.skipValue(); continue; } else { throw new JsonParseException("Cannot parse nested object to value"); } data.put(key, value); } } reader.endObject(); return data; } catch (IOException | IllegalStateException e) { throw new JsonParseException(e.getMessage()); } }
From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public static <T> int readJsnArr(JsonReader reader, String tableName, Class<T> clazz) throws IOException, JSONException, Exception { JSONObject o = new JSONObject(); JsonToken peek = reader.peek();//w w w .j a v a 2s .c o m String n = null; reader.beginArray(); int j = 0; int i = 0; while (reader.hasNext()) { peek = reader.peek(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (reader.peek() == JsonToken.END_OBJECT) { reader.endObject(); } o = new JSONObject(); while (reader.hasNext()) { peek = reader.peek(); if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.BOOLEAN) { try { o.put(n, reader.nextBoolean()); } catch (JSONException e) { e.printStackTrace(); } } else if (peek == JsonToken.STRING) { try { o.put(n, reader.nextString()); } catch (JSONException e) { e.printStackTrace(); } } else if (peek == JsonToken.NUMBER) { try { o.put(n, reader.nextDouble()); } catch (JSONException e) { e.printStackTrace(); } } } reader.endObject(); if (o.has("key")) { i = i + 1; j = j + 1; if (j % 100 == 0) { j = 2; } saveEntityFromJson(o, tableName, clazz, i); if (i % 10 == 0) { //notifyUser(context.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". " + context.getString(R.string.hint_run_background), (int)(Math.round(j))); } } } reader.endArray(); return i; }
From source file:com.gd.misc.test.JsonReader.java
License:Apache License
/** * Returns the type of the next token without consuming it. *///from w ww.j a v a 2 s .co m 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(); } }
From source file:com.github.gv2011.hprops.Converter.java
License:Open Source License
private void dispatch(final JsonReader json, final Consumer<Property> target, final JsonToken t, final String key) { if (t.equals(JsonToken.BOOLEAN)) write(target, key, getBoolean(json)); else if (t.equals(JsonToken.STRING)) write(target, key, getString(json)); else if (t.equals(JsonToken.NUMBER)) write(target, key, getNumber(json)); else if (t.equals(JsonToken.BEGIN_OBJECT)) doObject(json, target, key);// w w w . j a va2s. com else if (t.equals(JsonToken.BEGIN_ARRAY)) doList(json, target, key); else if (t.equals(JsonToken.NULL)) getNull(json); else throw new IllegalArgumentException(t.toString()); }
From source file:com.goide.dlv.JsonReaderEx.java
License:Apache License
/** * Returns the type of the next token without consuming it. *//*from w ww. j av a2 s .c om*/ @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;/*from w ww . j a va2 s . c o m*/ 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 w w . jav a 2 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.magnet.android.mms.request.marshall.GsonStreamReader.java
License:Open Source License
@Override public String getAttributeContent(Class<?> type) throws MarshallingException { String result;/*from w w w . j a v a2 s . c om*/ JsonToken tokenType = getJsonToken(); if (tokenType == JsonToken.NULL) { try { jr.nextNull(); return null; } catch (Exception e) { throw new MarshallingException(e); } } try { if (type == String.class) { result = jr.nextString(); } else if (type == Boolean.class || type == boolean.class) { if (tokenType == JsonToken.BOOLEAN) { result = String.valueOf(jr.nextBoolean()); return result; } if (tokenType == JsonToken.NUMBER) { result = String.valueOf(jr.nextInt()); return result; } result = jr.nextString(); } else if (type == int.class) { result = String.valueOf(jr.nextInt()); } else if (type == long.class) { result = String.valueOf(jr.nextLong()); } else if (type == double.class) { result = String.valueOf(jr.nextDouble()); } else { result = jr.nextString(); } } catch (Exception e) { throw new MarshallingException(e); } return result; }