Java Code Examples for com.google.gson.stream.JsonToken#END_ARRAY
The following examples show how to use
com.google.gson.stream.JsonToken#END_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: PositionsAdapter.java From geogson with Apache License 2.0 | 6 votes |
private static Positions parseSinglePosition(JsonReader in) throws IOException { double lon = in.nextDouble(); double lat = in.nextDouble(); double alt = Double.NaN; if (in.hasNext()) { alt = in.nextDouble(); } // Skip eventual other dimensions while (in.peek() != JsonToken.END_ARRAY) { in.skipValue(); } return new SinglePosition(lon, lat, alt); }
Example 2
Source File: UsersStreamDeserializer.java From java-json-benchmark with MIT License | 6 votes |
@Override public Users gson(JsonReader reader) throws IOException { Users uc = new Users(); reader.beginObject(); JsonToken token; while ((token = reader.peek()) != JsonToken.END_OBJECT) { if (token == JsonToken.NAME) { String fieldname = reader.nextName(); if ("users".equals(fieldname)) { uc.users = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { uc.users.add(gsonUser(reader)); } reader.endArray(); } } } reader.endObject(); return uc; }
Example 3
Source File: StreamAclJsonAdapter.java From esjc with MIT License | 6 votes |
private static List<String> readRoles(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.STRING) { return singletonList(reader.nextString()); } else { List<String> roles = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { roles.add(reader.nextString()); } reader.endArray(); return roles; } }
Example 4
Source File: JsonMapper.java From firebase-admin-java with Apache License 2.0 | 5 votes |
private static List<Object> unwrapJsonArray(JsonReader jsonReader) throws IOException { List<Object> list = new ArrayList<>(); jsonReader.beginArray(); while (jsonReader.peek() != JsonToken.END_ARRAY) { list.add(unwrapJson(jsonReader)); } jsonReader.endArray(); return list; }
Example 5
Source File: TraceEvent.java From bazel with Apache License 2.0 | 5 votes |
private static ImmutableList<Object> parseArray(JsonReader reader) throws IOException { ImmutableList.Builder<Object> builder = ImmutableList.builder(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { Object val = parseSingleValueRecursively(reader); builder.add(val); } reader.endArray(); return builder.build(); }
Example 6
Source File: TypeAdapters.java From framework with GNU Affero General Public License v3.0 | 5 votes |
public BitSet read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } BitSet bitset = new BitSet(); in.beginArray(); int i = 0; JsonToken tokenType = in.peek(); while (tokenType != JsonToken.END_ARRAY) { boolean set; switch (tokenType) { case NUMBER: set = in.nextInt() != 0; break; case BOOLEAN: set = in.nextBoolean(); break; case STRING: String stringValue = in.nextString(); try { set = Integer.parseInt(stringValue) != 0; } catch (NumberFormatException e) { throw new JsonSyntaxException( "Error: Expecting: bitset number value (1, 0), Found: " + stringValue); } break; default: throw new JsonSyntaxException("Invalid bitset value type: " + tokenType); } if (set) { bitset.set(i); } ++i; tokenType = in.peek(); } in.endArray(); return bitset; }
Example 7
Source File: TypeAdapters.java From gson with Apache License 2.0 | 5 votes |
@Override public BitSet read(JsonReader in) throws IOException { BitSet bitset = new BitSet(); in.beginArray(); int i = 0; JsonToken tokenType = in.peek(); while (tokenType != JsonToken.END_ARRAY) { boolean set; switch (tokenType) { case NUMBER: set = in.nextInt() != 0; break; case BOOLEAN: set = in.nextBoolean(); break; case STRING: String stringValue = in.nextString(); try { set = Integer.parseInt(stringValue) != 0; } catch (NumberFormatException e) { throw new JsonSyntaxException( "Error: Expecting: bitset number value (1, 0), Found: " + stringValue); } break; default: throw new JsonSyntaxException("Invalid bitset value type: " + tokenType); } if (set) { bitset.set(i); } ++i; tokenType = in.peek(); } in.endArray(); return bitset; }
Example 8
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 9
Source File: SemanticVersionTypeAdapter.java From ProjectAres with GNU Affero General Public License v3.0 | 5 votes |
@Override public SemanticVersion read(JsonReader in) throws IOException { in.beginArray(); int major = in.nextInt(); int minor = in.nextInt(); int patch = in.peek() == JsonToken.END_ARRAY ? 0 : in.nextInt(); in.endArray(); return new SemanticVersion(major, minor, patch); }
Example 10
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 11
Source File: StreamMetadataJsonAdapter.java From esjc with MIT License | 4 votes |
@Override public StreamMetadata read(JsonReader reader) throws IOException { StreamMetadata.Builder builder = StreamMetadata.newBuilder(); if (reader.peek() == JsonToken.NULL) { return null; } reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT && reader.hasNext()) { String name = reader.nextName(); switch (name) { case MAX_COUNT: builder.maxCount(reader.nextLong()); break; case MAX_AGE: builder.maxAge(Duration.ofSeconds(reader.nextLong())); break; case TRUNCATE_BEFORE: builder.truncateBefore(reader.nextLong()); break; case CACHE_CONTROL: builder.cacheControl(Duration.ofSeconds(reader.nextLong())); break; case ACL: StreamAcl acl = streamAclJsonAdapter.read(reader); if (acl != null) { builder.aclReadRoles(acl.readRoles); builder.aclWriteRoles(acl.writeRoles); builder.aclDeleteRoles(acl.deleteRoles); builder.aclMetaReadRoles(acl.metaReadRoles); builder.aclMetaWriteRoles(acl.metaWriteRoles); } break; default: switch (reader.peek()) { case NULL: reader.nextNull(); builder.customProperty(name, (String) null); break; case BEGIN_ARRAY: List<Object> values = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { switch (reader.peek()) { case NULL: reader.nextNull(); values.add(null); break; case BOOLEAN: values.add(reader.nextBoolean()); break; case NUMBER: values.add(new BigDecimal(reader.nextString())); break; case STRING: values.add(reader.nextString()); } } reader.endArray(); if (values.stream().anyMatch(v -> v instanceof Boolean)) { builder.customProperty(name, values.stream().toArray(Boolean[]::new)); } else if (values.stream().anyMatch(v -> v instanceof Number)) { builder.customProperty(name, values.stream().toArray(Number[]::new)); } else { builder.customProperty(name, values.stream().toArray(String[]::new)); } break; case BOOLEAN: builder.customProperty(name, reader.nextBoolean()); break; case NUMBER: builder.customProperty(name, new BigDecimal(reader.nextString())); break; case STRING: builder.customProperty(name, reader.nextString()); break; default: reader.skipValue(); } } } reader.endObject(); return builder.build(); }
Example 12
Source File: TypeAdapters.java From letv with Apache License 2.0 | 4 votes |
public BitSet read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } BitSet bitset = new BitSet(); in.beginArray(); int i = 0; JsonToken tokenType = in.peek(); while (tokenType != JsonToken.END_ARRAY) { boolean set; switch (tokenType) { case NUMBER: if (in.nextInt() == 0) { set = false; break; } set = true; break; case BOOLEAN: set = in.nextBoolean(); break; case STRING: String stringValue = in.nextString(); try { set = Integer.parseInt(stringValue) != 0; break; } catch (NumberFormatException e) { throw new JsonSyntaxException("Error: Expecting: bitset number value (1, 0), Found: " + stringValue); } default: throw new JsonSyntaxException("Invalid bitset value type: " + tokenType); } if (set) { bitset.set(i); } i++; tokenType = in.peek(); } in.endArray(); return bitset; }
Example 13
Source File: JsonTreeReader.java From MiBandDecompiled with Apache License 2.0 | 4 votes |
public JsonToken peek() { if (c.isEmpty()) { return JsonToken.END_DOCUMENT; } Object obj = a(); if (obj instanceof Iterator) { boolean flag = c.get(-2 + c.size()) instanceof JsonObject; Iterator iterator = (Iterator)obj; if (iterator.hasNext()) { if (flag) { return JsonToken.NAME; } else { c.add(iterator.next()); return peek(); } } if (flag) { return JsonToken.END_OBJECT; } else { return JsonToken.END_ARRAY; } } if (obj instanceof JsonObject) { return JsonToken.BEGIN_OBJECT; } if (obj instanceof JsonArray) { return JsonToken.BEGIN_ARRAY; } if (obj instanceof JsonPrimitive) { JsonPrimitive jsonprimitive = (JsonPrimitive)obj; if (jsonprimitive.isString()) { return JsonToken.STRING; } if (jsonprimitive.isBoolean()) { return JsonToken.BOOLEAN; } if (jsonprimitive.isNumber()) { return JsonToken.NUMBER; } else { throw new AssertionError(); } } if (obj instanceof JsonNull) { return JsonToken.NULL; } if (obj == b) { throw new IllegalStateException("JsonReader is closed"); } else { throw new AssertionError(); } }
Example 14
Source File: JsonTreeReader.java From gson with Apache License 2.0 | 4 votes |
@Override public boolean hasNext() throws IOException { JsonToken token = peek(); return token != JsonToken.END_OBJECT && token != JsonToken.END_ARRAY; }
Example 15
Source File: JsonTreeReader.java From gson with Apache License 2.0 | 4 votes |
@Override public JsonToken peek() throws IOException { if (stackSize == 0) { return JsonToken.END_DOCUMENT; } Object o = peekStack(); if (o instanceof Iterator) { boolean isObject = stack[stackSize - 2] instanceof JsonObject; Iterator<?> iterator = (Iterator<?>) o; if (iterator.hasNext()) { if (isObject) { return JsonToken.NAME; } else { push(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(); } }
Example 16
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 17
Source File: JsonTreeReader.java From framework with GNU Affero General Public License v3.0 | 4 votes |
@Override public boolean hasNext() throws IOException { JsonToken token = peek(); return token != JsonToken.END_OBJECT && token != JsonToken.END_ARRAY; }
Example 18
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(); } }
Example 19
Source File: JsonTreeReader.java From letv with Apache License 2.0 | 4 votes |
public boolean hasNext() throws IOException { JsonToken token = peek(); return (token == JsonToken.END_OBJECT || token == JsonToken.END_ARRAY) ? false : true; }
Example 20
Source File: SpliceDoNotRetryIOExceptionWrapping.java From spliceengine with GNU Affero General Public License v3.0 | 4 votes |
@Override public StandardException read(JsonReader in) throws IOException { in.beginObject(); int severity = 0; String textMessage = null; String sqlState = null; String messageId = null; List<String> objectStrings = null; while (in.peek() != JsonToken.END_OBJECT) { String nextName = in.nextName(); if ("severity".equals(nextName)) severity = in.nextInt(); else if ("textMessage".equals(nextName)) textMessage = in.nextString(); else if ("sqlState".equals(nextName)) sqlState = in.nextString(); else if ("messageId".equals(nextName)) messageId = in.nextString(); else if ("arguments".equals(nextName)) { if (in.peek() != JsonToken.NULL) { in.beginArray(); objectStrings = new ArrayList<String>(); while (in.peek() != JsonToken.END_ARRAY) { objectStrings.add(in.nextString()); } in.endArray(); } else in.nextNull(); } } in.endObject(); StandardException se; if (objectStrings != null) { Object[] objects = new Object[objectStrings.size()]; objectStrings.toArray(objects); se = StandardException.newException(messageId, objects); } else { se = StandardException.newException(messageId); } se.setSeverity(severity); se.setSqlState(sqlState); se.setTextMessage(textMessage); return se; }