List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY
JsonToken BEGIN_ARRAY
To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.
Click Source Link
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public void beginArray() throws IOException { expect(JsonToken.BEGIN_ARRAY); JsonArray array = ((JsonElement) peekStack()).getAsJsonArray(); stack.add(array.iterator());/*from w w w .ja va2 s .c om*/ }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; }/*from w w w. j a v a 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:com.cinchapi.concourse.util.Convert.java
License:Apache License
/** * Takes a JSON string representation of an object or an array of JSON * objects and returns a list of {@link Multimap multimaps} with the * corresponding data. Unlike {@link #jsonToJava(String)}, this method will * allow the top level element to be an array in the {code json} string. * // ww w . ja v a2s .c o m * @param json * @return A list of Java objects */ public static List<Multimap<String, Object>> anyJsonToJava(String json) { List<Multimap<String, Object>> result = Lists.newArrayList(); try (JsonReader reader = new JsonReader(new StringReader(json))) { reader.setLenient(true); if (reader.peek() == JsonToken.BEGIN_ARRAY) { try { reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { result.add(jsonToJava(reader)); } reader.endArray(); } catch (IllegalStateException e) { throw new JsonParseException(e.getMessage()); } } else { result.add(jsonToJava(reader)); } } catch (IOException e) { throw Throwables.propagate(e); } return result; }
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 w w w .j a va2 s . c o m*/ * * <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.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java
License:Apache License
@Override public MailChimpObject read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();//from www .ja va2 s . c o m return null; } MailChimpObject result; try { result = constructor.newInstance(); } catch (Exception e) { throw new RuntimeException("Failed to invoke " + constructor + " with no args", e); } in.beginObject(); while (in.hasNext()) { final String key; if (in.peek() == JsonToken.NAME) { key = in.nextName(); } else { key = in.nextString(); } final Object value; Type valueType = result.getReflectiveMappingTypes().get(key); if (valueType != null) { value = gson.getAdapter(TypeToken.get(valueType)).read(in); } else { if (in.peek() == JsonToken.BEGIN_OBJECT) { value = gson.getAdapter(MailChimpObject.class).read(in); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { value = readList(in); } else { value = gson.getAdapter(Object.class).read(in); } } if (result.put(key, value) != null) { throw new JsonSyntaxException("duplicate key: " + key); } } in.endObject(); return result; }
From source file:com.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java
License:Apache License
private List<?> readList(JsonReader in) throws IOException { List<Object> result = new ArrayList<Object>(); in.beginArray();//from www . jav a2 s . c om while (in.peek() != JsonToken.END_ARRAY) { final Object element; if (in.peek() == JsonToken.BEGIN_OBJECT) { element = gson.getAdapter(MailChimpObject.class).read(in); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { element = readList(in); } else { element = gson.getAdapter(Object.class).read(in); } result.add(element); } in.endArray(); return result; }
From source file:com.esri.gpt.framework.dcat.DcatParser.java
License:Apache License
/** * Parses data (DOM building style).//w w w .ja v a 2 s . c o m * * @param policy limit policy * @return list of records. * @throws DcatParseException if parsing fails */ public DcatRecordList parse(final LimitPolicy policy) throws DcatParseException, IOException { final DcatRecordListImpl list = new DcatRecordListImpl(); DcatParser.ListenerInternal listener = new DcatParser.ListenerInternal() { private void parse(final ListenerInternal listener) throws DcatParseException, IOException { if (!jsonReader.hasNext()) { throw new DcatParseException("No more data available."); } JsonToken token = jsonReader.peek(); if (token != JsonToken.BEGIN_ARRAY) { throw new DcatParseException("No array found."); } parseRecords(listener); } @Override public boolean onRecord(DcatRecord record) { list.add(record); if (policy == null || list.size() >= policy.getLimit()) { return true; } else { policy.onLimit(list); return false; } } }; parse(listener); return list; }
From source file:com.esri.gpt.framework.dcat.DcatParser.java
License:Apache License
/** * Parses DCAT using internal listener./*from ww w. j av a 2 s .c o m*/ * * @param listener internal listener * @throws DcatParseException if parsing DCAT fails */ void parse(final ListenerInternal listener) throws DcatParseException, IOException { if (!jsonReader.hasNext()) { throw new DcatParseException("No more data available."); } JsonToken token = jsonReader.peek(); if (token != JsonToken.BEGIN_ARRAY) { throw new DcatParseException("No array found."); } jsonReader.beginArray(); parseRecords(listener); }
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 ww .j a v a 2s . c o m*/ 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.gd.misc.test.JsonReader.java
License:Apache License
/** * Returns the type of the next token without consuming it. *//* w w w.j ava 2s .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(); } }