Java Code Examples for com.google.gson.stream.JsonReader#nextBoolean()
The following examples show how to use
com.google.gson.stream.JsonReader#nextBoolean() .
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: NodeLayoutStore.java From olca-app with Mozilla Public License 2.0 | 6 votes |
private static NodeLayoutInfo parseLayoutInfo( JsonReader r) throws IOException { NodeLayoutInfo info = new NodeLayoutInfo(); r.beginObject(); r.nextName(); info.id = r.nextLong(); r.nextName(); info.x = r.nextInt(); r.nextName(); info.y = r.nextInt(); r.nextName(); info.minimized = r.nextBoolean(); r.nextName(); info.expandedLeft = r.nextBoolean(); r.nextName(); info.expandedRight = r.nextBoolean(); r.nextName(); info.marked = r.nextBoolean(); r.endObject(); return info; }
Example 2
Source File: BooleanTypeAdapter.java From EasyHttp with Apache License 2.0 | 6 votes |
@Override public Boolean read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; case STRING: // 如果后台返回 "true" 或者 "TRUE",则默认处理为 true return Boolean.parseBoolean(in.nextString()); case NUMBER: // 如果这个后台返回是 1,则表示 true,否则表示 false return in.nextInt() == 1; default: return in.nextBoolean(); } }
Example 3
Source File: SupportUtils.java From data-mediator with Apache License 2.0 | 6 votes |
public static Object readPrimitiveOrItsBox(JsonReader reader, Property p) throws IOException { Class<?> type = p.getType(); if (type == void.class || type == Void.class) { return null; } else if (type == boolean.class || type == Boolean.class) { return reader.nextBoolean(); } else if (type == byte.class || type == Byte.class) { return (byte)reader.nextInt(); } else if (type == short.class || type == Short.class) { return (short)reader.nextInt(); } else if (type == int.class || type == Integer.class) { return reader.nextInt(); } else if (type == long.class || type == Long.class) { return reader.nextLong(); } else if (type == char.class || type == Character.class) { return (char)reader.nextLong(); } else if (type == float.class || type == Float.class) { return (float)reader.nextDouble(); } else if (type == double.class || type == Double.class) { return reader.nextDouble(); } else { throw new IllegalStateException(); } }
Example 4
Source File: JsonParser.java From grpc-java with Apache License 2.0 | 6 votes |
private static Object parseRecursive(JsonReader jr) throws IOException { checkState(jr.hasNext(), "unexpected end of JSON"); switch (jr.peek()) { case BEGIN_ARRAY: return parseJsonArray(jr); case BEGIN_OBJECT: return parseJsonObject(jr); case STRING: return jr.nextString(); case NUMBER: return jr.nextDouble(); case BOOLEAN: return jr.nextBoolean(); case NULL: return parseJsonNull(jr); default: throw new IllegalStateException("Bad token: " + jr.getPath()); } }
Example 5
Source File: TypeAdapters.java From gson with Apache License 2.0 | 5 votes |
@Override public JsonElement read(JsonReader in) throws IOException { switch (in.peek()) { case STRING: return new JsonPrimitive(in.nextString()); case NUMBER: String number = in.nextString(); return new JsonPrimitive(new LazilyParsedNumber(number)); case BOOLEAN: return new JsonPrimitive(in.nextBoolean()); case NULL: in.nextNull(); return JsonNull.INSTANCE; case BEGIN_ARRAY: JsonArray array = new JsonArray(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: JsonObject object = new JsonObject(); in.beginObject(); while (in.hasNext()) { object.add(in.nextName(), read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }
Example 6
Source File: TypeAdapters.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@Override public JsonElement read(JsonReader in) throws IOException { switch (in.peek()) { case STRING: return new JsonPrimitive(in.nextString()); case NUMBER: String number = in.nextString(); return new JsonPrimitive(new LazilyParsedNumber(number)); case BOOLEAN: return new JsonPrimitive(in.nextBoolean()); case NULL: in.nextNull(); return JsonNull.INSTANCE; case BEGIN_ARRAY: JsonArray array = new JsonArray(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: JsonObject object = new JsonObject(); in.beginObject(); while (in.hasNext()) { object.add(in.nextName(), read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }
Example 7
Source File: TypeAdapters.java From gson with Apache License 2.0 | 5 votes |
@Override public Boolean read(JsonReader in) throws IOException { JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); return null; } else if (peek == JsonToken.STRING) { // support strings for compatibility with GSON 1.7 return Boolean.parseBoolean(in.nextString()); } return in.nextBoolean(); }
Example 8
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 9
Source File: ObjectTypeAdapter.java From gson with Apache License 2.0 | 5 votes |
@Override public Object read(JsonReader in) throws IOException { JsonToken token = in.peek(); switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray(); while (in.hasNext()) { list.add(read(in)); } in.endArray(); return list; case BEGIN_OBJECT: Map<String, Object> map = new LinkedTreeMap<String, Object>(); in.beginObject(); while (in.hasNext()) { map.put(in.nextName(), read(in)); } in.endObject(); return map; case STRING: return in.nextString(); case NUMBER: return in.nextDouble(); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; default: throw new IllegalStateException(); } }
Example 10
Source File: JsonLdSerializer.java From schemaorg-java with Apache License 2.0 | 5 votes |
private List<SchemaOrgType> readInternal(JsonReader reader, boolean acceptNull) throws IOException { List<SchemaOrgType> list = new ArrayList<>(); switch (reader.peek()) { case STRING: list.add(Text.of(reader.nextString())); break; case NUMBER: list.add(Number.of(reader.nextString())); break; case BOOLEAN: if (reader.nextBoolean()) { list.add(BooleanEnum.TRUE); } else { list.add(BooleanEnum.FALSE); } break; case NULL: if (acceptNull) { reader.nextNull(); list.add(null); } else { throw new JsonLdSyntaxException("Found null value for schema.org property value"); } break; case BEGIN_OBJECT: list.add(readObject(reader)); break; case BEGIN_ARRAY: list.addAll(readArray(reader)); break; case END_DOCUMENT: throw new JsonLdSyntaxException("Meet end of document"); case END_OBJECT: case END_ARRAY: case NAME: break; } return list; }
Example 11
Source File: ObjectTypeAdapter.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@Override public Object read(JsonReader in) throws IOException { JsonToken token = in.peek(); switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray(); while (in.hasNext()) { list.add(read(in)); } in.endArray(); return list; case BEGIN_OBJECT: Map<String, Object> map = new LinkedTreeMap<String, Object>(); in.beginObject(); while (in.hasNext()) { map.put(in.nextName(), read(in)); } in.endObject(); return map; case STRING: return in.nextString(); case NUMBER: return in.nextDouble(); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; default: throw new IllegalStateException(); } }
Example 12
Source File: BufferAttribute.java From BlueMap with MIT License | 5 votes |
public static BufferAttribute readJson(JsonReader json) throws IOException { List<Float> list = new ArrayList<>(1000); int itemSize = 1; boolean normalized = false; json.beginObject(); //root while (json.hasNext()){ String name = json.nextName(); if(name.equals("array")){ json.beginArray(); //array while (json.hasNext()){ list.add(new Float(json.nextDouble())); } json.endArray(); //array } else if (name.equals("itemSize")) { itemSize = json.nextInt(); } else if (name.equals("normalized")) { normalized = json.nextBoolean(); } else json.skipValue(); } json.endObject(); //root //collect values in array float[] values = new float[list.size()]; for (int i = 0; i < values.length; i++) { values[i] = list.get(i); } return new BufferAttribute(values, itemSize, normalized); }
Example 13
Source File: BagOfPrimitivesDeserializationBenchmark.java From gson with Apache License 2.0 | 5 votes |
/** * Benchmark to measure deserializing objects by hand */ public void timeBagOfPrimitivesStreaming(int reps) throws IOException { for (int i=0; i<reps; ++i) { StringReader reader = new StringReader(json); JsonReader jr = new JsonReader(reader); jr.beginObject(); long longValue = 0; int intValue = 0; boolean booleanValue = false; String stringValue = null; while(jr.hasNext()) { String name = jr.nextName(); if (name.equals("longValue")) { longValue = jr.nextLong(); } else if (name.equals("intValue")) { intValue = jr.nextInt(); } else if (name.equals("booleanValue")) { booleanValue = jr.nextBoolean(); } else if (name.equals("stringValue")) { stringValue = jr.nextString(); } else { throw new IOException("Unexpected name: " + name); } } jr.endObject(); new BagOfPrimitives(longValue, intValue, booleanValue, stringValue); } }
Example 14
Source File: AbstractJobConfigurationGsonTypeAdapter.java From shardingsphere-elasticjob-cloud with Apache License 2.0 | 4 votes |
@Override public T read(final JsonReader in) throws IOException { String jobName = ""; String cron = ""; int shardingTotalCount = 0; String shardingItemParameters = ""; String jobParameter = ""; boolean failover = false; boolean misfire = failover; String description = ""; JobProperties jobProperties = new JobProperties(); JobType jobType = null; String jobClass = ""; boolean streamingProcess = false; String scriptCommandLine = ""; Map<String, Object> customizedValueMap = new HashMap<>(32, 1); in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case "jobName": jobName = in.nextString(); break; case "cron": cron = in.nextString(); break; case "shardingTotalCount": shardingTotalCount = in.nextInt(); break; case "shardingItemParameters": shardingItemParameters = in.nextString(); break; case "jobParameter": jobParameter = in.nextString(); break; case "failover": failover = in.nextBoolean(); break; case "misfire": misfire = in.nextBoolean(); break; case "description": description = in.nextString(); break; case "jobProperties": jobProperties = getJobProperties(in); break; case "jobType": jobType = JobType.valueOf(in.nextString()); break; case "jobClass": jobClass = in.nextString(); break; case "streamingProcess": streamingProcess = in.nextBoolean(); break; case "scriptCommandLine": scriptCommandLine = in.nextString(); break; default: addToCustomizedValueMap(jsonName, in, customizedValueMap); break; } } in.endObject(); JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount, shardingItemParameters, jobParameter, failover, misfire, description, jobProperties); JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, streamingProcess, scriptCommandLine); return getJobRootConfiguration(typeConfig, customizedValueMap); }
Example 15
Source File: CloudAppConfigurationGsonFactory.java From shardingsphere-elasticjob-cloud with Apache License 2.0 | 4 votes |
@Override public CloudAppConfiguration read(final JsonReader in) throws IOException { String appURL = ""; String appName = ""; String bootstrapScript = ""; double cpuCount = 1.0d; double memoryMB = 128.0d; boolean appCacheEnable = true; int eventTraceSamplingCount = 0; in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case CloudConfigurationConstants.APP_NAME: appName = in.nextString(); break; case CloudConfigurationConstants.APP_URL: appURL = in.nextString(); break; case CloudConfigurationConstants.BOOTSTRAP_SCRIPT: bootstrapScript = in.nextString(); break; case CloudConfigurationConstants.CPU_COUNT: cpuCount = in.nextDouble(); break; case CloudConfigurationConstants.MEMORY_MB: memoryMB = in.nextDouble(); break; case CloudConfigurationConstants.APP_CACHE_ENABLE: appCacheEnable = in.nextBoolean(); break; case CloudConfigurationConstants.EVENT_TRACE_SAMPLING_COUNT: eventTraceSamplingCount = in.nextInt(); break; default: break; } } in.endObject(); return new CloudAppConfiguration(appName, appURL, bootstrapScript, cpuCount, memoryMB, appCacheEnable, eventTraceSamplingCount); }
Example 16
Source File: JsonPropertyConsumer.java From olingo-odata2 with Apache License 2.0 | 4 votes |
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, final Object typeMapping, final EntityProviderReadProperties readProperties) throws EdmException, EntityProviderException, IOException { final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType(); Object value = null; final JsonToken tokenType = reader.peek(); if (tokenType == JsonToken.NULL) { reader.nextNull(); } else { switch (EdmSimpleTypeKind.valueOf(type.getName())) { case Boolean: if (tokenType == JsonToken.BOOLEAN) { value = reader.nextBoolean(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; case Byte: case SByte: case Int16: case Int32: if (tokenType == JsonToken.NUMBER) { value = reader.nextInt(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; case Single: case Double: if (tokenType == JsonToken.STRING) { value = reader.nextString(); } else if (tokenType == JsonToken.NUMBER) { value = reader.nextDouble(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; default: if (tokenType == JsonToken.STRING) { value = reader.nextString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; } } final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping; final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ? entityPropertyInfo.getFacets() : null; return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass); }
Example 17
Source File: JSONPListParser.java From tm4e with Eclipse Public License 1.0 | 4 votes |
public T parse(InputStream contents) throws Exception { PList<T> pList = new PList<T>(theme); JsonReader reader = new JsonReader(new InputStreamReader(contents, StandardCharsets.UTF_8)); // reader.setLenient(true); boolean parsing = true; while (parsing) { JsonToken nextToken = reader.peek(); switch (nextToken) { case BEGIN_ARRAY: pList.startElement(null, "array", null, null); reader.beginArray(); break; case END_ARRAY: pList.endElement(null, "array", null); reader.endArray(); break; case BEGIN_OBJECT: pList.startElement(null, "dict", null, null); reader.beginObject(); break; case END_OBJECT: pList.endElement(null, "dict", null); reader.endObject(); break; case NAME: String lastName = reader.nextName(); pList.startElement(null, "key", null, null); pList.characters(lastName.toCharArray(), 0, lastName.length()); pList.endElement(null, "key", null); break; case NULL: reader.nextNull(); break; case BOOLEAN: reader.nextBoolean(); break; case NUMBER: reader.nextLong(); break; case STRING: String value = reader.nextString(); pList.startElement(null, "string", null, null); pList.characters(value.toCharArray(), 0, value.length()); pList.endElement(null, "string", null); break; case END_DOCUMENT: parsing = false; break; default: break; } } reader.close(); return pList.getResult(); }
Example 18
Source File: FoodReader.java From TFC2 with GNU General Public License v3.0 | 4 votes |
private FoodJSON readFood(JsonReader reader) throws IOException { String name = ""; int meta = 0; long time = -1; List<FoodGroupPair> fg = new ArrayList<FoodGroupPair>(); boolean edible = true; reader.beginObject(); while(reader.hasNext()) { String key = reader.nextName(); if(key.equals("name")) { name = reader.nextString(); if(name.contains(" ")) { String[] s = name.split(" "); name = s[0]; meta = Integer.parseInt(s[1]); } } else if(key.equals("decay")) time = reader.nextLong(); else if(key.equals("foodgroup")) { if(reader.peek() == JsonToken.BEGIN_OBJECT) { reader.beginObject(); while(reader.hasNext()) { fg.add(new FoodGroupPair(EnumFoodGroup.valueOf(reader.nextName()), (float)reader.nextDouble())); } reader.endObject(); } else fg.add(new FoodGroupPair(EnumFoodGroup.valueOf(reader.nextString()), 100f)); } else if(key.equals("edible")) edible = reader.nextBoolean(); else reader.skipValue(); } reader.endObject(); return new FoodJSON(name, meta, time, fg, edible); }
Example 19
Source File: Adapters.java From salt-netapi-client with MIT License | 4 votes |
@Override public Boolean read(JsonReader in) throws IOException { return in.nextBoolean(); }
Example 20
Source File: DataTypeAdaptor.java From AndroidWallet with GNU General Public License v3.0 | 4 votes |
private Object readInternal(JsonReader in) throws IOException { JsonToken token = in.peek(); switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray(); while (in.hasNext()) { list.add(readInternal(in)); } in.endArray(); return list; case BEGIN_OBJECT: Map<String, Object> map = new LinkedTreeMap<String, Object>(); in.beginObject(); while (in.hasNext()) { map.put(in.nextName(), readInternal(in)); } in.endObject(); return map; case STRING: return in.nextString(); case NUMBER: //将其作为一个字符串读取出来 String numberStr = in.nextString(); //返回的numberStr不会为null if (numberStr.contains(".") || numberStr.contains("e") || numberStr.contains("E")) { return Double.parseDouble(numberStr); } return Long.parseLong(numberStr); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; default: throw new IllegalStateException(); } }