List of usage examples for com.google.gson.stream JsonToken BEGIN_OBJECT
JsonToken BEGIN_OBJECT
To view the source code for com.google.gson.stream JsonToken BEGIN_OBJECT.
Click Source Link
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 ww . 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.emf.SharedJsonDeserializer.java
License:Open Source License
@SuppressWarnings("rawtypes") public IfcModelInterface read(InputStream in, IfcModelInterface model, boolean checkWaitingList) throws DeserializeException { if (model.getPackageMetaData().getSchemaDefinition() == null) { throw new DeserializeException("No SchemaDefinition available"); }/* w w w . j a v a2s .c o m*/ WaitingList<Long> waitingList = new WaitingList<Long>(); final boolean log = false; if (log) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { IOUtils.copy(in, baos); File file = new File("debug.json"); System.out.println(file.getAbsolutePath()); FileUtils.writeByteArrayToFile(file, baos.toByteArray()); } catch (IOException e) { e.printStackTrace(); } in = new ByteArrayInputStream(baos.toByteArray()); } JsonReader jsonReader = new JsonReader(new InputStreamReader(in, Charsets.UTF_8)); int nrObjects = 0; try { JsonToken peek = jsonReader.peek(); if (peek != null && peek == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); peek = jsonReader.peek(); while (peek == JsonToken.NAME) { String nextName = jsonReader.nextName(); if (nextName.equals("objects")) { jsonReader.beginArray(); while (jsonReader.hasNext()) { nrObjects++; processObject(model, waitingList, jsonReader, null); } jsonReader.endArray(); } else if (nextName.equals("header")) { IfcHeader ifcHeader = (IfcHeader) processObject(model, waitingList, jsonReader, StorePackage.eINSTANCE.getIfcHeader()); model.getModelMetaData().setIfcHeader(ifcHeader); } peek = jsonReader.peek(); } jsonReader.endObject(); } } catch (IOException e) { LOGGER.error("", e); } catch (IfcModelInterfaceException e) { LOGGER.error("", e); } finally { LOGGER.info("# Objects: " + nrObjects); try { jsonReader.close(); } catch (IOException e) { LOGGER.error("", e); } } boolean checkUnique = false; if (checkUnique) { for (IdEObject idEObject : model.getValues()) { for (EStructuralFeature eStructuralFeature : idEObject.eClass().getEAllStructuralFeatures()) { Object value = idEObject.eGet(eStructuralFeature); if (eStructuralFeature instanceof EReference) { if (eStructuralFeature.isMany()) { List list = (List) value; if (eStructuralFeature.isUnique()) { Set<Object> t = new HashSet<>(); for (Object v : list) { if (t.contains(v)) { // LOGGER.error("NOT UNIQUE " + idEObject.eClass().getName() + "." + eStructuralFeature.getName()); } t.add(v); } } } } } } } if (checkWaitingList && waitingList.size() > 0) { try { waitingList.dumpIfNotEmpty(); } catch (BimServerClientException e) { e.printStackTrace(); } throw new DeserializeException("Waitinglist should be empty (" + waitingList.size() + ")"); } return model; }
From source file:org.dice_research.topicmodeling.io.json.AbstractDocumentJsonReader.java
License:Open Source License
public Document readDocument(JsonReader reader) { try {//from w ww .ja v a 2 s . co m // Check that an object begins if ((!reader.hasNext()) && (!JsonToken.BEGIN_OBJECT.equals(reader.peek()))) { return null; } } catch (IOException e) { LOGGER.warn("Got an IOException when trying to check whether a JSON object begins. Moving on.", e); } try { return gson.fromJson(reader, Document.class); } catch (Exception e) { LOGGER.error("Couldn't read document from JSON. Returning null.", e); return null; } }
From source file:org.eclipse.skalli.core.rest.JSONRestReader.java
License:Open Source License
@Override public boolean isValue() throws IOException { if (state == STATE_FINAL) { return false; }/*from ww w.j a va 2 s . co m*/ if (isKey("value")) { //$NON-NLS-1$ return true; } JsonToken next = json.peek(); return lookAhead == null && (next == JsonToken.STRING || next == JsonToken.NUMBER || next == JsonToken.BOOLEAN || next == JsonToken.BEGIN_ARRAY || next == JsonToken.BEGIN_OBJECT || next == JsonToken.NULL); }
From source file:org.eclipse.skalli.core.rest.JSONRestReader.java
License:Open Source License
@Override public boolean isObject() throws IOException { if (state == STATE_FINAL) { return false; }/*from w ww . j a v a2 s . co m*/ return json.peek() == JsonToken.BEGIN_OBJECT; }
From source file:org.geotools.data.arcgisrest.GeoJSONParser.java
License:Open Source License
/** * Checks whether there is another feature to read * //ww w.j a va 2s . c om * @return true if there is another featuere to read, false otherwise */ @Override public boolean hasNext() { try { if (this.inFeatureCollection == true && this.reader.peek() == JsonToken.BEGIN_OBJECT) { return true; } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } return false; }
From source file:org.immutables.gson.stream.JsonParserReader.java
License:Apache License
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: return JsonToken.STRING; default: // Not semantically equivalent return JsonToken.NULL; }// w ww . j av a 2 s . c o m }
From source file:org.immutables.mongo.bson4gson.BsonReader.java
License:Apache License
private static JsonToken toGsonToken(BsonType type) { switch (type) { case END_OF_DOCUMENT: return JsonToken.END_DOCUMENT; case DOCUMENT: return JsonToken.BEGIN_OBJECT; case ARRAY:/*from ww w .ja va 2s .c o m*/ 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: return JsonToken.NULL; default: // not really sure what to do with this type return JsonToken.NULL; } }
From source file:org.jetbrains.io.JsonReaderEx.java
License:Apache License
/** * Returns the type of the next token without consuming it. *///from w w w . j a v a2 s . c o m public JsonToken peek() { 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(); } }
From source file:org.kairosdb.core.http.rest.json.DataPointsParser.java
License:Apache License
public ValidationErrors parse() throws IOException, DatastoreException { long start = System.currentTimeMillis(); ValidationErrors validationErrors = new ValidationErrors(); JsonReader reader = new JsonReader(inputStream); try {//from ww w. j a v a 2 s . c o m int metricCount = 0; if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) { try { reader.beginArray(); while (reader.hasNext()) { NewMetric metric = parseMetric(reader); validateAndAddDataPoints(metric, validationErrors, metricCount); metricCount++; } } catch (EOFException e) { validationErrors.addErrorMessage("Invalid json. No content due to end of input."); } reader.endArray(); } else if (reader.peek().equals(JsonToken.BEGIN_OBJECT)) { NewMetric metric = parseMetric(reader); validateAndAddDataPoints(metric, validationErrors, 0); } else validationErrors.addErrorMessage("Invalid start of json."); } catch (EOFException e) { validationErrors.addErrorMessage("Invalid json. No content due to end of input."); } finally { reader.close(); } ingestTime = (int) (System.currentTimeMillis() - start); return validationErrors; }