List of usage examples for com.google.gson.stream JsonReader getPath
public String getPath()
From source file:LongDateTypeAdapter.java
License:Apache License
@Override public Date read(JsonReader in) throws IOException { switch (in.peek()) { case NULL:/*from ww w . j a v a 2s. com*/ return null; case STRING: try { return new Date(Long.parseLong(in.nextString())); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } default: throw new JsonSyntaxException("invalid date" + in.getPath()); } }
From source file:BundleTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from ww w . ja v a 2s. 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:com.gilecode.yagson.types.TypeUtils.java
License:Apache License
private static Type readTypeAdvice(JsonReader in) throws IOException { in.beginObject();/*w w w . ja v a 2 s.c om*/ if (!in.hasNext()) { throw new JsonSyntaxException("BEGIN_OBJECT is not expected at path " + in.getPath()); } String name = in.nextName(); if (!name.equals("@type")) { throw new JsonSyntaxException("@type is expected at path " + in.getPath()); } return readTypeAdviceAfterTypeField(in); }
From source file:com.gilecode.yagson.types.TypeUtils.java
License:Apache License
private static boolean consumeValueField(JsonReader in) throws IOException { if (!in.hasNext()) { // no @val means actually null value, e.g. skipped by serialization return false; }/*from w w w .j a v a 2 s. c om*/ String name = in.nextName(); if (!name.equals("@val")) { throw new JsonSyntaxException("Only @type and @val fields are expected at the type advice " + "objects at path " + in.getPath()); } return true; }
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://from ww w .j a va2s . 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:io.grpc.internal.JsonParser.java
License:Apache License
private static Map<String, ?> parseJsonObject(JsonReader jr) throws IOException { jr.beginObject();//from w w w . j a v a 2 s .c om Map<String, Object> obj = new LinkedHashMap<>(); while (jr.hasNext()) { String name = jr.nextName(); Object value = parseRecursive(jr); obj.put(name, value); } checkState(jr.peek() == JsonToken.END_OBJECT, "Bad token: " + jr.getPath()); jr.endObject(); return Collections.unmodifiableMap(obj); }
From source file:io.grpc.internal.JsonParser.java
License:Apache License
private static List<?> parseJsonArray(JsonReader jr) throws IOException { jr.beginArray();//ww w .j a v a2s . c o m List<Object> array = new ArrayList<>(); while (jr.hasNext()) { Object value = parseRecursive(jr); array.add(value); } checkState(jr.peek() == JsonToken.END_ARRAY, "Bad token: " + jr.getPath()); jr.endArray(); return Collections.unmodifiableList(array); }
From source file:org.objectpocket.gson.CustomTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); // @Entity/*from www. j a v a2s . c o m*/ if (type.getRawType().getAnnotation(Entity.class) != null) { return new TypeAdapter<T>() { // SERIALIZE public void write(JsonWriter out, T obj) throws IOException { if (obj != null) { String id = objectPocket.getIdForObject(obj); // normalize if (!objectPocket.isSerializeAsRoot(obj)) { gson.toJson(new ProxyOut(obj.getClass().getTypeName(), id), ProxyOut.class, out); return; } else { objectPocket.setSerializeAsRoot(obj, false); } } // default serialization delegate.write(out, obj); }; // DESERIALIZE @SuppressWarnings("unchecked") @Override public T read(JsonReader in) throws IOException { if (in.getPath().length() > 2) { in.beginObject(); in.nextName(); StringBuilder sb = new StringBuilder(in.nextString()); String id = sb.substring(0, sb.indexOf("@")); in.endObject(); T obj = null; try { obj = (T) ReflectionUtil.instantiateDefaultConstructor(type.getRawType()); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { throw new IOException("Could not instantiate class " + type.getRawType().getName() + "\n" + "Might be that the class has no default constructor!", e); } objectPocket.addIdFromReadObject(obj, id); return obj; } else { T obj = delegate.read(in); return obj; } } }; } // All other else { return delegate; } }