List of usage examples for com.google.gson.stream JsonReader nextBoolean
public boolean nextBoolean() throws IOException
From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java
License:Apache License
static Map<String, Object> parseObject(final JsonReader jsonReader) throws IOException { Map<String, Object> map = new HashMap<>(); String tempKey;/* ww w .ja v a2 s . co m*/ JsonToken tok; // while (jsonReader.hasNext()) { // since we want to restore a map we assue a key/value pair tempKey = jsonReader.nextName(); // tok = jsonReader.peek(); // we reach the end of this array if (JsonToken.END_ARRAY == tok) { jsonReader.endArray(); // return break; } // what token switch (tok) { // if array/map - parse and append case BEGIN_ARRAY: map.put(tempKey, parseArray(jsonReader)); break; case BEGIN_OBJECT: map.put(tempKey, parseObject(jsonReader)); break; // if raw type case STRING: map.put(tempKey, jsonReader.nextString()); break; case BOOLEAN: map.put(tempKey, jsonReader.nextBoolean()); break; case NUMBER: map.put(tempKey, jsonReader.nextDouble()); break; // if null , add null and consume case NULL: map.put(tempKey, null); jsonReader.nextNull(); break; // all other cases are errors default: throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array"); } } return map; }
From source file:io.grpc.internal.JsonParser.java
License:Apache License
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:// w w w . j a v a2 s . c o m 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()); } }
From source file:me.ixfan.wechatkit.message.out.json.MassMessageGsonTypeAdapter.java
License:Open Source License
@Override public MessageForMassSend read(JsonReader in) throws IOException { MessageForMassSend.Filter filter = null; List<String> toUser = null; String msgType = null;/*from w ww . j a va 2s.c o m*/ String msgContent = null; in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "msgtype": msgType = in.nextString(); break; case "filter": in.beginObject(); String tagId = null; boolean isToAll = false; while (in.hasNext()) { switch (in.nextName()) { case "is_to_all": isToAll = in.nextBoolean(); break; case "tag_id": tagId = in.nextString(); break; default: break; } } in.endObject(); filter = new MessageForMassSend.Filter(tagId, isToAll); break; case "touser": in.beginArray(); toUser = new ArrayList<>(); while (in.hasNext()) { toUser.add(in.nextString()); } in.endArray(); break; case "text": case "image": case "voice": case "mpnews": case "mpvideo": case "wxcard": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "content": case "media_id": case "card_id": msgContent = in.nextString(); break; default: break; } } in.endObject(); break; default: break; } } in.endObject(); if (null != filter) { return new MessageForMassSend(OutMessageType.valueOf(msgType), msgContent, filter.getTagId(), filter.isToAll()); } else if (null != toUser) { return new MessageForMassSend(OutMessageType.valueOf(msgType), msgContent, toUser); } return null; }
From source file:ninja.leaping.configurate.gson.GsonConfigurationLoader.java
License:Apache License
private void parseValue(JsonReader parser, ConfigurationNode node) throws IOException { JsonToken token = parser.peek();/*w w w. ja va 2 s . com*/ switch (token) { case BEGIN_OBJECT: parseObject(parser, node); break; case BEGIN_ARRAY: parseArray(parser, node); break; case NUMBER: double nextDouble = parser.nextDouble(); int nextInt = (int) nextDouble; long nextLong = (long) nextDouble; if (nextInt == nextDouble) { node.setValue(nextInt); // They don't do much for us here in Gsonland } else if (nextLong == nextDouble) { node.setValue(nextLong); } else { node.setValue(nextDouble); } break; case STRING: node.setValue(parser.nextString()); break; case BOOLEAN: node.setValue(parser.nextBoolean()); break; case NULL: // Ignored values case NAME: break; default: throw new IOException("Unsupported token type: " + token); } }
From source file:org.apache.ambari.view.hive.resources.uploads.parsers.json.JSONIterator.java
License:Apache License
private LinkedHashMap<String, String> readNextObject(JsonReader reader) throws IOException, EndOfDocumentException { LinkedHashMap<String, String> row = new LinkedHashMap<>(); boolean objectStarted = false; boolean shouldBeName = false; String currentName = null;//from w ww . j ava2 s . c o m while (true) { JsonToken token = reader.peek(); switch (token) { case BEGIN_ARRAY: throw new IllegalArgumentException("Row data cannot have an array."); case END_ARRAY: throw new EndOfDocumentException("End of Json Array document."); case BEGIN_OBJECT: if (objectStarted == true) { throw new IllegalArgumentException("Nested objects not supported."); } if (shouldBeName == true) { throw new IllegalArgumentException("name expected, got begin_object"); } objectStarted = true; shouldBeName = true; reader.beginObject(); break; case END_OBJECT: if (shouldBeName == false) { throw new IllegalArgumentException("value expected, got end_object"); } reader.endObject(); return row; case NAME: if (shouldBeName == false) { throw new IllegalArgumentException("name not expected at this point."); } shouldBeName = false; currentName = reader.nextName(); break; case NUMBER: case STRING: if (shouldBeName == true) { throw new IllegalArgumentException("value not expected at this point."); } String n = reader.nextString(); row.put(currentName, n); shouldBeName = true; break; case BOOLEAN: if (shouldBeName == true) { throw new IllegalArgumentException("value not expected at this point."); } String b = String.valueOf(reader.nextBoolean()); row.put(currentName, b); shouldBeName = true; break; case NULL: if (shouldBeName == true) { throw new IllegalArgumentException("value not expected at this point."); } reader.nextNull(); row.put(currentName, ""); shouldBeName = true; break; case END_DOCUMENT: return row; default: throw new IllegalArgumentException( "Illegal token detected inside json: token : " + token.toString()); } } }
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonPropertyConsumer.java
License:Apache License
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;// ww w .j a v a 2s . c o m 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); }
From source file:org.arl.fjage.remote.GenericValueAdapterFactory.java
License:BSD License
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.equals(GenericValue.class)) return null; final GenericValueAdapterFactory parent = this; return new TypeAdapter<T>() { @Override//from w w w.j a v a 2 s . c o m public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); return; } Class type = ((GenericValue) value).getType(); if (type == null) { out.nullValue(); return; } if (Number.class.isAssignableFrom(type)) out.value((Number) ((GenericValue) value).getValue()); else if (type.equals(String.class)) out.value((String) ((GenericValue) value).getValue()); else if (type.equals(Boolean.class)) out.value((Boolean) ((GenericValue) value).getValue()); else { out.beginObject(); out.name("clazz").value(type.getName()); out.name("data"); TypeAdapter delegate = gson.getAdapter(TypeToken.get(type)); Object v = ((GenericValue) value).getValue(); delegate.write(out, v); out.endObject(); } } @Override public T read(JsonReader in) throws IOException { JsonToken tok = in.peek(); if (tok == JsonToken.NULL) { in.nextNull(); return null; } if (tok == JsonToken.NUMBER) { String s = in.nextString(); try { if (s.contains(".")) return (T) new GenericValue(Double.parseDouble(s)); else return (T) new GenericValue(Long.parseLong(s)); } catch (NumberFormatException ex) { return (T) new GenericValue(null); } } if (tok == JsonToken.STRING) return (T) new GenericValue(in.nextString()); if (tok == JsonToken.BOOLEAN) return (T) new GenericValue(in.nextBoolean()); if (tok != JsonToken.BEGIN_OBJECT) return null; TypeToken tt = null; GenericValue rv = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (name.equals("clazz")) { try { Class<?> cls = Class.forName(in.nextString()); tt = TypeToken.get(cls); } catch (Exception ex) { // do nothing } } else if (name.equals("data") && tt != null) { TypeAdapter delegate = gson.getAdapter(tt); rv = new GenericValue(delegate.read(in)); } else in.skipValue(); } in.endObject(); return (T) rv; } }; }
From source file:org.bimserver.client.ClientIfcModel.java
License:Open Source License
private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException { EClassifier eClassifier = eStructuralFeature.getEType(); if (eClassifier == EcorePackage.eINSTANCE.getEString()) { return jsonReader.nextString(); } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble() || eClassifier == EcorePackage.eINSTANCE.getEDoubleObject()) { return jsonReader.nextDouble(); } else if (eClassifier == EcorePackage.eINSTANCE.getEFloat() || eClassifier == EcorePackage.eINSTANCE.getEFloatObject()) { return (float) jsonReader.nextDouble(); } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean() || eClassifier == EcorePackage.eINSTANCE.getEBooleanObject()) { return jsonReader.nextBoolean(); } else if (eClassifier == EcorePackage.eINSTANCE.getEInt() || eClassifier == EcorePackage.eINSTANCE.getEIntegerObject()) { return jsonReader.nextInt(); } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) { // TODO DOES THIS EVER HAPPEN?? if (jsonReader.peek() == JsonToken.BOOLEAN) { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(("" + jsonReader.nextBoolean()).toUpperCase()).getInstance(); } else {/*from ww w . j a v a2 s.c om*/ EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance(); } } else if (eClassifier instanceof EClass) { throw new RuntimeException(); } else if (eClassifier == EcorePackage.eINSTANCE.getEByteArray()) { return Base64.decodeBase64(jsonReader.nextString().getBytes(Charsets.UTF_8)); } else if (eClassifier instanceof EEnum) { if (jsonReader.peek() == JsonToken.BOOLEAN) { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(("" + jsonReader.nextBoolean()).toUpperCase()).getInstance(); } else { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance(); } } else { throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName()); } }
From source file:org.bimserver.deserializers.JsonDeserializer.java
License:Open Source License
private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException { EClassifier eClassifier = eStructuralFeature.getEType(); if (eClassifier == EcorePackage.eINSTANCE.getEString()) { return jsonReader.nextString(); } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()) { return jsonReader.nextDouble(); } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()) { return jsonReader.nextBoolean(); } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()) { return jsonReader.nextInt(); } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance(); } else if (eClassifier instanceof EClass) { if (eStructuralFeature.getEType().getName().equals("IfcGloballyUniqueId")) { IfcGloballyUniqueId ifcGloballyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId(); ifcGloballyUniqueId.setWrappedValue(jsonReader.nextString()); return ifcGloballyUniqueId; } else {// w w w . j a v a 2 s.co m throw new RuntimeException(); } } else if (eClassifier instanceof EEnum) { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance(); } else { throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName()); } }
From source file:org.bimserver.emf.SharedJsonDeserializer.java
License:Open Source License
private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException { EClassifier eClassifier = eStructuralFeature.getEType(); if (eClassifier == EcorePackage.eINSTANCE.getEString()) { return jsonReader.nextString(); } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()) { return jsonReader.nextDouble(); } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()) { return jsonReader.nextBoolean(); } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()) { return jsonReader.nextInt(); } else if (eClassifier == EcorePackage.eINSTANCE.getELong()) { return jsonReader.nextLong(); } else if (eClassifier == EcorePackage.eINSTANCE.getEIntegerObject()) { return jsonReader.nextInt(); } else if (eClassifier == EcorePackage.eINSTANCE.getEByteArray()) { return Base64.decodeBase64(jsonReader.nextString()); } else if (eClassifier == EcorePackage.eINSTANCE.getEDate()) { long timestamp = jsonReader.nextLong(); return new Date(timestamp); } else if (eClassifier == EcorePackage.eINSTANCE.getEFloat()) { return (float) jsonReader.nextDouble(); } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance(); } else if (eClassifier instanceof EClass) { if (eStructuralFeature.getEType().getName().equals("IfcGloballyUniqueId")) { IfcGloballyUniqueId ifcGloballyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId(); ifcGloballyUniqueId.setWrappedValue(jsonReader.nextString()); return ifcGloballyUniqueId; } else {// w ww . j a va2 s. co m throw new RuntimeException(); } } else if (eClassifier instanceof EEnum) { EEnum eEnum = (EEnum) eStructuralFeature.getEType(); if (jsonReader.peek() == JsonToken.BOOLEAN) { return eEnum.getEEnumLiteral(jsonReader.nextBoolean() ? "TRUE" : "FALSE").getInstance(); } else { return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance(); } } else { throw new RuntimeException("Unimplemented type " + eStructuralFeature.getEType().getName()); } }