List of usage examples for com.google.gson.stream JsonReader nextDouble
public double nextDouble() throws IOException
From source file:BundleTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w ww .j av a 2 s. c o m*/ public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { if (!Bundle.class.isAssignableFrom(type.getRawType())) { return null; } return (TypeAdapter<T>) new TypeAdapter<Bundle>() { @Override public void write(JsonWriter out, Bundle bundle) throws IOException { if (bundle == null) { out.nullValue(); return; } out.beginObject(); for (String key : bundle.keySet()) { out.name(key); Object value = bundle.get(key); if (value == null) { out.nullValue(); } else { gson.toJson(value, value.getClass(), out); } } out.endObject(); } @Override public Bundle read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; case BEGIN_OBJECT: return toBundle(readObject(in)); default: throw new IOException("expecting object: " + in.getPath()); } } private Bundle toBundle(List<Pair<String, Object>> values) throws IOException { Bundle bundle = new Bundle(); for (Pair<String, Object> entry : values) { String key = entry.first; Object value = entry.second; if (value instanceof String) { bundle.putString(key, (String) value); } else if (value instanceof Integer) { bundle.putInt(key, ((Integer) value).intValue()); } else if (value instanceof Long) { bundle.putLong(key, ((Long) value).longValue()); } else if (value instanceof Double) { bundle.putDouble(key, ((Double) value).doubleValue()); } else if (value instanceof Parcelable) { bundle.putParcelable(key, (Parcelable) value); } else if (value instanceof List) { List<Pair<String, Object>> objectValues = (List<Pair<String, Object>>) value; Bundle subBundle = toBundle(objectValues); bundle.putParcelable(key, subBundle); } else { throw new IOException("Unparcelable key, value: " + key + ", " + value); } } return bundle; } private List<Pair<String, Object>> readObject(JsonReader in) throws IOException { List<Pair<String, Object>> object = new ArrayList<Pair<String, Object>>(); in.beginObject(); while (in.peek() != JsonToken.END_OBJECT) { switch (in.peek()) { case NAME: String name = in.nextName(); Object value = readValue(in); object.add(new Pair<String, Object>(name, value)); break; case END_OBJECT: break; default: throw new IOException("expecting object: " + in.getPath()); } } in.endObject(); return object; } private Object readValue(JsonReader in) throws IOException { switch (in.peek()) { case BEGIN_ARRAY: return readArray(in); case BEGIN_OBJECT: return readObject(in); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; case NUMBER: return readNumber(in); case STRING: return in.nextString(); default: throw new IOException("expecting value: " + in.getPath()); } } private Object readNumber(JsonReader in) throws IOException { double doubleValue = in.nextDouble(); if (doubleValue - Math.ceil(doubleValue) == 0) { long longValue = (long) doubleValue; if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) { return (int) longValue; } return longValue; } return doubleValue; } @SuppressWarnings("rawtypes") private List readArray(JsonReader in) throws IOException { List list = new ArrayList(); in.beginArray(); while (in.peek() != JsonToken.END_ARRAY) { Object element = readValue(in); list.add(element); } in.endArray(); return list; } }; }
From source file:cn.ieclipse.af.demo.sample.volley.adapter.IntAdapter.java
License:Apache License
@Override public Number read(JsonReader in) throws IOException { int num = 0;//from w w w . j a v a 2 s .co m // 0 if (in.peek() == JsonToken.NULL) { in.nextNull(); num = 0; } else { try { double input = in.nextDouble();//?double?? num = (int) input;//int } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } return num; }
From source file:co.cask.cdap.format.StructuredRecordStringConverter.java
License:Apache License
private static Object readJson(JsonReader reader, Schema schema) throws IOException { switch (schema.getType()) { case NULL:/*from w w w .ja v a2 s. com*/ reader.nextNull(); return null; case BOOLEAN: return reader.nextBoolean(); case INT: return reader.nextInt(); case LONG: return reader.nextLong(); case FLOAT: // Force down cast return (float) reader.nextDouble(); case DOUBLE: return reader.nextDouble(); case BYTES: return readBytes(reader); case STRING: return reader.nextString(); case ENUM: // Currently there is no standard container to represent enum type return reader.nextString(); case ARRAY: return readArray(reader, schema.getComponentSchema()); case MAP: return readMap(reader, schema.getMapSchema()); case RECORD: return readRecord(reader, schema); case UNION: return readUnion(reader, schema); } throw new IOException("Unsupported schema: " + schema); }
From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public static <T> int readJsnArr(JsonReader reader, String tableName, Class<T> clazz) throws IOException, JSONException, Exception { JSONObject o = new JSONObject(); JsonToken peek = reader.peek();/* w w w.j ava 2 s . c o m*/ String n = null; reader.beginArray(); int j = 0; int i = 0; while (reader.hasNext()) { peek = reader.peek(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (reader.peek() == JsonToken.END_OBJECT) { reader.endObject(); } o = new JSONObject(); while (reader.hasNext()) { peek = reader.peek(); if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.BOOLEAN) { try { o.put(n, reader.nextBoolean()); } catch (JSONException e) { e.printStackTrace(); } } else if (peek == JsonToken.STRING) { try { o.put(n, reader.nextString()); } catch (JSONException e) { e.printStackTrace(); } } else if (peek == JsonToken.NUMBER) { try { o.put(n, reader.nextDouble()); } catch (JSONException e) { e.printStackTrace(); } } } reader.endObject(); if (o.has("key")) { i = i + 1; j = j + 1; if (j % 100 == 0) { j = 2; } saveEntityFromJson(o, tableName, clazz, i); if (i % 10 == 0) { //notifyUser(context.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". " + context.getString(R.string.hint_run_background), (int)(Math.round(j))); } } } reader.endArray(); return i; }
From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java
License:Apache License
private Positions parseSinglePosition(JsonReader in) throws IOException { Positions parsed;/*from w ww.ja va 2 s .c o m*/ double lon = in.nextDouble(); double lat = in.nextDouble(); double alt = 0.0; if (in.hasNext()) { alt = in.nextDouble(); } // Skip eventual altitude or other dimensions while (in.peek() != JsonToken.END_ARRAY) { in.skipValue(); } parsed = new SinglePosition(Coordinates.of(lon, lat, alt)); return parsed; }
From source file:com.github.kevinsawicki.halligan.Resource.java
License:Open Source License
/** * Parse resource property/*ww w . j av a 2 s . c o m*/ * * @param reader * @param name * @throws IOException */ protected void parseProperty(final JsonReader reader, final String name) throws IOException { JsonToken next = reader.peek(); switch (next) { case BEGIN_OBJECT: properties.put(name, gson.getGson().fromJson(reader, Map.class)); break; case STRING: properties.put(name, reader.nextString()); break; case NUMBER: properties.put(name, reader.nextDouble()); break; case NULL: properties.put(name, null); reader.nextNull(); break; case BOOLEAN: properties.put(name, reader.nextBoolean()); break; default: throw new IOException("Unrecognized property value token: " + next); } }
From source file:com.github.lindenb.gatkui.Json2Xml.java
License:Open Source License
private void parse(String label, JsonReader r) throws Exception { if (!r.hasNext()) return;/*from ww w . ja v a 2s. com*/ JsonToken token = r.peek(); switch (token) { case NAME: break; case BEGIN_OBJECT: { r.beginObject(); parseObject(label, r); break; } case END_OBJECT: { break; } case BEGIN_ARRAY: { r.beginArray(); parseArray(label, r); break; } case END_ARRAY: { break; } case NULL: { r.nextNull(); w.writeEmptyElement(NS, "null"); if (label != null) w.writeAttribute("name", label); break; } case STRING: { w.writeStartElement(NS, "string"); if (label != null) w.writeAttribute("name", label); w.writeCharacters(r.nextString()); w.writeEndElement(); break; } case NUMBER: { w.writeStartElement(NS, "number"); if (label != null) w.writeAttribute("name", label); String s; try { s = String.valueOf(r.nextLong()); } catch (Exception err) { s = String.valueOf(r.nextDouble()); } w.writeCharacters(s); w.writeEndElement(); break; } case BOOLEAN: { w.writeStartElement(NS, "boolean"); if (label != null) w.writeAttribute("name", label); w.writeCharacters(String.valueOf(r.nextBoolean())); w.writeEndElement(); break; } case END_DOCUMENT: { break; } default: throw new IllegalStateException(token.name()); } }
From source file:com.google.maps.internal.GeolocationResponseAdapter.java
License:Open Source License
@Override public GeolocationApi.Response read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();//from ww w.ja v a2 s . c om return null; } GeolocationApi.Response response = new GeolocationApi.Response(); LatLngAdapter latLngAdapter = new LatLngAdapter(); reader.beginObject(); // opening { while (reader.hasNext()) { String name = reader.nextName(); // two different objects could be returned a success object containing "location" and "accuracy" // keys or an error object containing an "error" key if (name.equals("location")) { // we already have a parser for the LatLng object so lets use that response.location = latLngAdapter.read(reader); } else if (name.equals("accuracy")) { response.accuracy = reader.nextDouble(); } else if (name.equals("error")) { reader.beginObject(); // the error key leads to another object... while (reader.hasNext()) { String errName = reader.nextName(); // ...with keys "errors", "code" and "message" if (errName.equals("code")) { response.code = reader.nextInt(); } else if (errName.equals("message")) { response.message = reader.nextString(); } else if (errName.equals("errors")) { reader.beginArray(); // its plural because its an array of errors... while (reader.hasNext()) { reader.beginObject();// ...and each error array element is an object... while (reader.hasNext()) { errName = reader.nextName(); // ...with keys "reason", "domain", "debugInfo", "location", "locationType", and "message" (again) if (errName.equals("reason")) { response.reason = reader.nextString(); } else if (errName.equals("domain")) { response.domain = reader.nextString(); } else if (errName.equals("debugInfo")) { response.debugInfo = reader.nextString(); } else if (errName.equals("message")) { // have this already reader.nextString(); } else if (errName.equals("location")) { reader.nextString(); } else if (errName.equals("locationType")) { reader.nextString(); } } reader.endObject(); } reader.endArray(); } } reader.endObject(); // closing } } } reader.endObject(); return response; }
From source file:com.google.maps.internal.LatLngAdapter.java
License:Open Source License
/** * Reads in a JSON object and try to create a LatLng in one of the following formats. * * <pre>{//from w w w.ja v a 2s . c o m * "lat" : -33.8353684, * "lng" : 140.8527069 * } * * { * "latitude": -33.865257570508334, * "longitude": 151.19287000481452 * }</pre> */ @Override public LatLng read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } double lat = 0; double lng = 0; boolean hasLat = false; boolean hasLng = false; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("lat".equals(name) || "latitude".equals(name)) { lat = reader.nextDouble(); hasLat = true; } else if ("lng".equals(name) || "longitude".equals(name)) { lng = reader.nextDouble(); hasLng = true; } } reader.endObject(); if (hasLat && hasLng) { return new LatLng(lat, lng); } else { return null; } }
From source file:com.ibm.og.client.ApacheClient.java
License:Open Source License
private Gson createGson() { return new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .setLongSerializationPolicy(LongSerializationPolicy.STRING) .registerTypeAdapter(Double.class, new TypeAdapter<Double>() { @Override/*from ww w . j a v a2s .c o m*/ public void write(final JsonWriter out, final Double value) throws IOException { // round decimals to 2 places out.value(new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue()); } @Override public Double read(final JsonReader in) throws IOException { return in.nextDouble(); } }.nullSafe()).create(); }