Java Code Examples for com.google.gson.stream.JsonToken#BEGIN_ARRAY
The following examples show how to use
com.google.gson.stream.JsonToken#BEGIN_ARRAY .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonFeedDeserializer.java From olingo-odata2 with Apache License 2.0 | 6 votes |
/** * * @throws IOException * @throws EdmException * @throws EntityProviderException */ private void readFeed() throws IOException, EdmException, EntityProviderException { JsonToken peek = reader.peek(); if (peek == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); final String nextName = reader.nextName(); if (FormatJson.D.equals(nextName)) { if (reader.peek() == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); readFeedContent(); reader.endObject(); } } else { handleName(nextName); readFeedContent(); } reader.endObject(); } }
Example 2
Source File: ListOfPointCoordinatesTypeAdapter.java From mapbox-java with MIT License | 6 votes |
@Override public List<Point> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { throw new NullPointerException(); } if (in.peek() == JsonToken.BEGIN_ARRAY) { List<Point> points = new ArrayList<>(); in.beginArray(); while (in.peek() == JsonToken.BEGIN_ARRAY) { points.add(readPoint(in)); } in.endArray(); return points; } throw new GeoJsonException("coordinates should be non-null array of array of double"); }
Example 3
Source File: ByteArrayTypeAdapter.java From sumk with Apache License 2.0 | 6 votes |
@Override public byte[] read(JsonReader in) throws IOException { JsonToken token = in.peek(); if (token == JsonToken.NULL) { in.nextNull(); return null; } if (token == JsonToken.STRING) { String s = in.nextString(); if (s.isEmpty()) { return new byte[0]; } return S.base64().decode(s.getBytes(StandardCharsets.UTF_8)); } if (token == JsonToken.BEGIN_ARRAY) { return rawRead(in); } throw new IOException(token + " is not valid byte array token"); }
Example 4
Source File: JsonLdSerializer.java From schemaorg-java with Apache License 2.0 | 6 votes |
private ListMultimap<String, Thing> readReverse(JsonReader reader) throws IOException { reader.beginObject(); ListMultimap<String, Thing> reverseMap = LinkedListMultimap.create(); while (reader.hasNext()) { String property = reader.nextName(); JsonToken token = reader.peek(); if (token == JsonToken.BEGIN_OBJECT) { reverseMap.put(property, readObject(reader)); } else if (token == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { reverseMap.put(property, readObject(reader)); } reader.endArray(); } else { throw new JsonLdSyntaxException( String.format( "Invalid value token %s in @reverse. Should be a JSON-LD object or an " + "array of JSON-LD objects", token)); } } reader.endObject(); return reverseMap; }
Example 5
Source File: PositionsAdapter.java From geogson with Apache License 2.0 | 6 votes |
@Override public Positions read(JsonReader in) throws IOException { Positions parsed; JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); parsed = null; } else if (peek == JsonToken.BEGIN_ARRAY) { parsed = parsePositions(in); } else { throw new IllegalArgumentException("The json must be an array or null: " + in.peek()); } return parsed; }
Example 6
Source File: JsonFeedConsumer.java From cloud-odata-java with Apache License 2.0 | 6 votes |
private void readFeed() throws IOException, EdmException, EntityProviderException { if (reader.peek() == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); final String nextName = reader.nextName(); if (FormatJson.D.equals(nextName)) { if (reader.peek() == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); readFeedContent(); reader.endObject(); } } else { handleName(nextName); readFeedContent(); } reader.endObject(); } }
Example 7
Source File: BsonReader.java From immutables with Apache License 2.0 | 5 votes |
private static JsonToken toGsonToken(BsonType type) { switch (type) { case END_OF_DOCUMENT: return JsonToken.END_DOCUMENT; case DOCUMENT: return JsonToken.BEGIN_OBJECT; case ARRAY: return JsonToken.BEGIN_ARRAY; case BOOLEAN: return JsonToken.BOOLEAN; case STRING: case SYMBOL: case OBJECT_ID: case BINARY: case REGULAR_EXPRESSION: return JsonToken.STRING; case DATE_TIME: case DOUBLE: case INT32: case INT64: case TIMESTAMP: case DECIMAL128: return JsonToken.NUMBER; case NULL: case UNDEFINED: return JsonToken.NULL; default: // not really sure what to do with this type return JsonToken.NULL; } }
Example 8
Source File: JsonErrorDocumentDeserializer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
/** * * @param reader * @return String * @throws IOException */ private String readJson(final JsonReader reader) throws IOException { StringBuilder sb = new StringBuilder(); while (reader.hasNext()) { JsonToken token = reader.peek(); if (token == JsonToken.NAME) { if (sb.length() > 0) { sb.append(","); } String name = reader.nextName(); sb.append("\"").append(name).append("\"").append(":"); } else if (token == JsonToken.BEGIN_OBJECT) { reader.beginObject(); sb.append("{") .append(readJson(reader)) .append("}"); reader.endObject(); } else if (token == JsonToken.BEGIN_ARRAY) { reader.beginArray(); sb.append("[") .append(readJson(reader)) .append("]"); reader.endArray(); } else { sb.append("\"") .append(reader.nextString()) .append("\""); } } return sb.toString(); }
Example 9
Source File: JsonUtils.java From olingo-odata2 with Apache License 2.0 | 5 votes |
public static int startJson(final JsonReader reader) throws EntityProviderException { // The enclosing "d" and "results" are optional - so we cannot check for the presence // but we have to read over them in case they are present. JsonToken token; try { token = reader.peek(); int openJsonObjects = 0; if (JsonToken.BEGIN_OBJECT == token) { reader.beginObject(); openJsonObjects++; token = reader.peek(); if (JsonToken.NAME == token) { String name = reader.nextName(); if (!("d".equals(name) ^ "results".equals(name))) { // TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, name + " not expected, only d or results"); } } token = reader.peek(); if (JsonToken.BEGIN_OBJECT == token) { reader.beginObject(); openJsonObjects++; } else if (JsonToken.BEGIN_ARRAY == token) { // TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, "Array not expected"); } } return openJsonObjects; } catch (IOException e) { // TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, e); } }
Example 10
Source File: GeometryAdapterFactory.java From geogson with Apache License 2.0 | 5 votes |
private Geometry<?> readGeometries(JsonReader in) throws IOException { Geometry<?> parsed; JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); parsed = null; } else if (peek == JsonToken.BEGIN_ARRAY) { parsed = parseGeometries(in); } else { throw new IllegalArgumentException("The json must be an array or null: " + in.peek()); } return parsed; }
Example 11
Source File: JsonErrorDocumentConsumer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
private String readJson(final JsonReader reader) throws IOException { StringBuilder sb = new StringBuilder(); while (reader.hasNext()) { JsonToken token = reader.peek(); if (token == JsonToken.NAME) { if (sb.length() > 0) { sb.append(","); } String name = reader.nextName(); sb.append("\"").append(name).append("\"").append(":"); } else if (token == JsonToken.BEGIN_OBJECT) { reader.beginObject(); sb.append("{") .append(readJson(reader)) .append("}"); reader.endObject(); } else if (token == JsonToken.BEGIN_ARRAY) { reader.beginArray(); sb.append("[") .append(readJson(reader)) .append("]"); reader.endArray(); } else { sb.append("\"") .append(reader.nextString()) .append("\""); } } return sb.toString(); }
Example 12
Source File: JsonParserReader.java From immutables with Apache License 2.0 | 5 votes |
private static JsonToken toGsonToken(com.fasterxml.jackson.core.JsonToken token) { switch (token) { case START_ARRAY: return JsonToken.BEGIN_ARRAY; case END_ARRAY: return JsonToken.END_ARRAY; case START_OBJECT: return JsonToken.BEGIN_OBJECT; case END_OBJECT: return JsonToken.END_OBJECT; case FIELD_NAME: return JsonToken.NAME; case VALUE_FALSE: return JsonToken.BOOLEAN; case VALUE_TRUE: return JsonToken.BOOLEAN; case VALUE_NULL: return JsonToken.NULL; case VALUE_NUMBER_INT: return JsonToken.NUMBER; case VALUE_NUMBER_FLOAT: return JsonToken.NUMBER; case VALUE_STRING: case VALUE_EMBEDDED_OBJECT: return JsonToken.STRING; default: // Not semantically equivalent return JsonToken.NULL; } }
Example 13
Source File: f.java From MiBandDecompiled with Apache License 2.0 | 5 votes |
public Map a(JsonReader jsonreader) { JsonToken jsontoken = jsonreader.peek(); if (jsontoken == JsonToken.NULL) { jsonreader.nextNull(); return null; } Map map = (Map)d.construct(); if (jsontoken == JsonToken.BEGIN_ARRAY) { jsonreader.beginArray(); for (; jsonreader.hasNext(); jsonreader.endArray()) { jsonreader.beginArray(); Object obj1 = b.read(jsonreader); if (map.put(obj1, c.read(jsonreader)) != null) { throw new JsonSyntaxException((new StringBuilder()).append("duplicate key: ").append(obj1).toString()); } } jsonreader.endArray(); return map; } jsonreader.beginObject(); while (jsonreader.hasNext()) { JsonReaderInternalAccess.INSTANCE.promoteNameToValue(jsonreader); Object obj = b.read(jsonreader); if (map.put(obj, c.read(jsonreader)) != null) { throw new JsonSyntaxException((new StringBuilder()).append("duplicate key: ").append(obj).toString()); } } jsonreader.endObject(); return map; }
Example 14
Source File: MessageTypeAdapter.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
/** * Convert the json input into the parameters object corresponding to the call * made by method. * * If the method is not known until after parsing, call * {@link #parseParams(Object, String)} on the return value of this call for a * second chance conversion. * * @param in * json input to read from * @param method * method name of request * @return correctly typed object if the correct expected type can be * determined, or a JsonElement representing the parameters */ protected Object parseParams(JsonReader in, String method) throws IOException, JsonIOException { JsonToken next = in.peek(); if (next == JsonToken.NULL) { in.nextNull(); return null; } Type[] parameterTypes = getParameterTypes(method); if (parameterTypes.length == 1) { return fromJson(in, parameterTypes[0]); } if (parameterTypes.length > 1 && next == JsonToken.BEGIN_ARRAY) { List<Object> parameters = new ArrayList<Object>(parameterTypes.length); int index = 0; in.beginArray(); while (in.hasNext()) { Type parameterType = index < parameterTypes.length ? parameterTypes[index] : null; Object parameter = fromJson(in, parameterType); parameters.add(parameter); index++; } in.endArray(); while (index < parameterTypes.length) { parameters.add(null); index++; } return parameters; } JsonElement rawParams = new JsonParser().parse(in); if (method != null && parameterTypes.length == 0 && ( rawParams.isJsonArray() && rawParams.getAsJsonArray().size() == 0 || rawParams.isJsonObject() && rawParams.getAsJsonObject().size() == 0)) { return null; } return rawParams; }
Example 15
Source File: ListofListofListOfPointCoordinatesTypeAdapter.java From mapbox-java with MIT License | 5 votes |
@Override public List<List<List<Point>>> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { throw new NullPointerException(); } if (in.peek() == JsonToken.BEGIN_ARRAY) { in.beginArray(); List<List<List<Point>>> listOfListOflistOfPoints = new ArrayList<>(); while (in.peek() == JsonToken.BEGIN_ARRAY) { in.beginArray(); List<List<Point>> listOfListOfPoints = new ArrayList<>(); while (in.peek() == JsonToken.BEGIN_ARRAY) { in.beginArray(); List<Point> listOfPoints = new ArrayList<>(); while (in.peek() == JsonToken.BEGIN_ARRAY) { listOfPoints.add(readPoint(in)); } in.endArray(); listOfListOfPoints.add(listOfPoints); } in.endArray(); listOfListOflistOfPoints.add(listOfListOfPoints); } in.endArray(); return listOfListOflistOfPoints; } throw new GeoJsonException("coordinates should be array of array of array of double"); }
Example 16
Source File: ListOfListOfPointCoordinatesTypeAdapter.java From mapbox-java with MIT License | 5 votes |
@Override public List<List<Point>> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { throw new NullPointerException(); } if (in.peek() == JsonToken.BEGIN_ARRAY) { in.beginArray(); List<List<Point>> points = new ArrayList<>(); while (in.peek() == JsonToken.BEGIN_ARRAY) { in.beginArray(); List<Point> listOfPoints = new ArrayList<>(); while (in.peek() == JsonToken.BEGIN_ARRAY) { listOfPoints.add(readPoint(in)); } in.endArray(); points.add(listOfPoints); } in.endArray(); return points; } throw new GeoJsonException("coordinates should be array of array of array of double"); }
Example 17
Source File: PositionsAdapter.java From geogson with Apache License 2.0 | 5 votes |
private static Positions parsePositions(JsonReader in) throws IOException { Positions parsed = null; if (in.peek() != JsonToken.BEGIN_ARRAY) { throw new IllegalArgumentException("The given json is not a valid positions"); } in.beginArray(); if (in.peek() == JsonToken.NUMBER) { parsed = parseSinglePosition(in); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { AbstractPositions.PositionsBuilder builder = null; while (in.hasNext()) { Positions p = parsePositions(in); if(builder == null) { builder = AbstractPositions.PositionsBuilder.builderOf(p); } builder.addChild(p); } if(builder != null) { parsed = builder.build(); } } in.endArray(); return parsed; }
Example 18
Source File: IpcdJsonReader.java From arcusplatform with Apache License 2.0 | 5 votes |
/** * Returns the type of the next token without consuming it. */ 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(); } }
Example 19
Source File: JsonTreeReader.java From letv with Apache License 2.0 | 4 votes |
public JsonToken peek() throws IOException { if (this.stack.isEmpty()) { return JsonToken.END_DOCUMENT; } Iterator<?> o = peekStack(); if (o instanceof Iterator) { boolean isObject = this.stack.get(this.stack.size() - 2) instanceof JsonObject; Iterator<?> iterator = o; if (!iterator.hasNext()) { return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY; } else { if (isObject) { return JsonToken.NAME; } this.stack.add(iterator.next()); return peek(); } } else if (o instanceof JsonObject) { return JsonToken.BEGIN_OBJECT; } else { if (o instanceof JsonArray) { return JsonToken.BEGIN_ARRAY; } if (o instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) o; if (primitive.isString()) { return JsonToken.STRING; } if (primitive.isBoolean()) { return JsonToken.BOOLEAN; } if (primitive.isNumber()) { return JsonToken.NUMBER; } throw new AssertionError(); } else if (o instanceof JsonNull) { return JsonToken.NULL; } else { if (o == SENTINEL_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } throw new AssertionError(); } } }
Example 20
Source File: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 4 votes |
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; } Object o = peekStack(); if (o instanceof Iterator) { boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject; 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 JsonObject) { return JsonToken.BEGIN_OBJECT; } else if (o instanceof JsonArray) { return JsonToken.BEGIN_ARRAY; } else if (o instanceof JsonPrimitive) { 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 (o instanceof JsonNull) { return JsonToken.NULL; } else if (o == SENTINEL_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } else { throw new AssertionError(); } }