List of usage examples for com.google.gson.stream JsonToken STRING
JsonToken STRING
To view the source code for com.google.gson.stream JsonToken STRING.
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; }// w w w .j a va 2 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 String nextString() throws IOException { JsonToken token = peek();/*from w ww. j a v a2 s .c o m*/ if (token != JsonToken.STRING && token != JsonToken.NUMBER) { throw new IllegalStateException("Expected " + JsonToken.STRING + " but was " + token); } return ((JsonPrimitive) popStack()).getAsString(); }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public double nextDouble() throws IOException { JsonToken token = peek();// ww w .j a v a2 s . c o m if (token != JsonToken.NUMBER && token != JsonToken.STRING) { throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token); } double result = ((JsonPrimitive) peekStack()).getAsDouble(); if (!isLenient() && (Double.isNaN(result) || Double.isInfinite(result))) { throw new NumberFormatException("JSON forbids NaN and infinities: " + result); } popStack(); return result; }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public long nextLong() throws IOException { JsonToken token = peek();//from w w w. j a v a 2s . com if (token != JsonToken.NUMBER && token != JsonToken.STRING) { throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token); } long result = ((JsonPrimitive) peekStack()).getAsLong(); popStack(); return result; }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public int nextInt() throws IOException { JsonToken token = peek();/*w ww .j av a 2 s. co m*/ if (token != JsonToken.NUMBER && token != JsonToken.STRING) { throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token); } int result = ((JsonPrimitive) peekStack()).getAsInt(); popStack(); return result; }
From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java
License:Open Source License
@Override public Usage read(JsonReader in) throws IOException { if (in.peek() == JsonToken.STRING) { String val = in.nextString(); Asserts.assertEquals("NoUsage", val, "Invalid JSON. Expected 'NoUsage', but found '" + val + "'."); return new NoUsage(); }/*from ww w .j a va2 s . c om*/ Query q = new Query(); q.setAllCallsites(null); in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (TYPE.equals(name)) { q.setType(CoReTypeName.get(in.nextString())); } else if (CLASS_CTX.equals(name)) { q.setClassContext(CoReTypeName.get(in.nextString())); } else if (METHOD_CTX.equals(name)) { q.setMethodContext(CoReMethodName.get(in.nextString())); } else if (DEFINITION.equals(name)) { q.setDefinition(readDefinition(in)); } else if (SITES.equals(name)) { q.setAllCallsites(readCallSites(in)); } else { // skip value (most likely $type key from .net serialization) in.nextString(); } } in.endObject(); return q; }
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./*w w w .j a v a 2 s .c om*/ * * <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 readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts) throws IOException, JSONException, Exception { String n = null;/* w w w . j a v a 2 s.c om*/ int i = 0; while (reader.hasNext()) { JsonToken peek = reader.peek(); String v = null; if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_ARRAY) { if (n.equals(tableName)) { i = readJsnArr(reader, tableName, clazz); } else { if (n.equals("params")) { reader.beginArray(); if (reader.hasNext()) { reader.beginObject(); if (reader.hasNext()) { n = reader.nextName(); v = reader.nextString(); } reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } } } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.END_ARRAY) { reader.endArray(); } else if (peek == JsonToken.STRING) { reader.skipValue(); } else { reader.skipValue(); } } return i; }
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();/*from ww w. j av a2 s.c om*/ 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 va2s .c o 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(); } }