List of usage examples for com.google.gson.stream JsonReader nextNull
public void nextNull() throws IOException
From source file:com.continuuity.loom.codec.json.LowercaseEnumTypeAdapterFactory.java
License:Apache License
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; }/* www. j a va 2s. co m*/ final Map<String, T> lowercaseToConstant = Maps.newHashMap(); for (T constant : rawType.getEnumConstants()) { lowercaseToConstant.put(toLowercase(constant), constant); } return new TypeAdapter<T>() { public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(toLowercase(value)); } } public T read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } else { String next = reader.nextString(); return lowercaseToConstant.get(next); } } }; }
From source file:com.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java
License:Apache License
@Override public MailChimpObject read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; }// w w w. ja v a2 s .com MailChimpObject result; try { result = constructor.newInstance(); } catch (Exception e) { throw new RuntimeException("Failed to invoke " + constructor + " with no args", e); } in.beginObject(); while (in.hasNext()) { final String key; if (in.peek() == JsonToken.NAME) { key = in.nextName(); } else { key = in.nextString(); } final Object value; Type valueType = result.getReflectiveMappingTypes().get(key); if (valueType != null) { value = gson.getAdapter(TypeToken.get(valueType)).read(in); } else { if (in.peek() == JsonToken.BEGIN_OBJECT) { value = gson.getAdapter(MailChimpObject.class).read(in); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { value = readList(in); } else { value = gson.getAdapter(Object.class).read(in); } } if (result.put(key, value) != null) { throw new JsonSyntaxException("duplicate key: " + key); } } in.endObject(); return result; }
From source file:com.flipkart.batchdemo.adapter.CustomTagDataAdapter.java
License:Open Source License
@Override public CustomTagData read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }/*from ww w .j a va 2 s . c o m*/ if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); Tag tag = null; Long eventId = 0L; JSONObject event = null; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "tag": tag = tagTypeAdapter.read(reader); break; case "eventId": eventId = BatchingTypeAdapters.LONG.read(reader); break; case "event": event = BatchingTypeAdapters.getJSONObjectTypeAdapter(gson).read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); CustomTagData customTagData = new CustomTagData(tag, event); customTagData.setEventId(eventId); return customTagData; }
From source file:com.flipkart.batching.gson.adapters.batch.SizeBatchTypeAdapter.java
License:Open Source License
@Override public SizeBatch<T> read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }// w ww . j a v a 2 s . c o m if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); int maxBatchSize = 0; DataCollection<T> dataCollection = null; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "maxBatchSize": maxBatchSize = BatchingTypeAdapters.INTEGER.read(reader); break; case "dataCollection": dataCollection = typeAdapter.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); return dataCollection == null ? null : new SizeBatch<>(dataCollection.dataCollection, maxBatchSize); }
From source file:com.flipkart.batching.gson.adapters.batch.SizeTimeBatchTypeAdapter.java
License:Open Source License
@Override public SizeTimeBatch<T> read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }/*from ww w . j a v a2s .c o m*/ if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); int maxBatchSize = 0; DataCollection<T> dataCollection = null; long timeout = 0L; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "maxBatchSize": maxBatchSize = BatchingTypeAdapters.INTEGER.read(reader); break; case "dataCollection": dataCollection = typeAdapter.read(reader); break; case "timeOut": timeout = BatchingTypeAdapters.LONG.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); return dataCollection == null ? null : new SizeTimeBatch<>(dataCollection.dataCollection, maxBatchSize, timeout); }
From source file:com.flipkart.batching.gson.adapters.batch.TagBatchTypeAdapter.java
License:Open Source License
@Override public TagBatch<T> read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }// ww w . j a v a 2s . co m if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); Tag tag = null; DataCollection<T> collection = null; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "tag": tag = tagTypeAdapter.read(reader); break; case "dataCollection": collection = typeAdapter.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); return collection == null || tag == null ? null : new TagBatch<>(tag, new BatchImpl<>(collection.dataCollection)); }
From source file:com.flipkart.batching.gson.adapters.batch.TimeBatchTypeAdapter.java
License:Open Source License
@Override public TimeBatch<T> read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }//from w w w . j av a 2 s .com if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); long timeOut = 0L; DataCollection<T> dataCollection = null; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "timeOut": timeOut = BatchingTypeAdapters.LONG.read(reader); break; case "dataCollection": dataCollection = typeAdapter.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); return dataCollection == null ? null : new TimeBatch<>(dataCollection.dataCollection, timeOut); }
From source file:com.flipkart.batching.gson.adapters.BatchImplTypeAdapter.java
License:Open Source License
@Override public BatchImpl<T> read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }/*from w ww .j ava 2 s. c o m*/ if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); DataCollection<T> dataCollection = null; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "dataCollection": dataCollection = typeAdapter.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); return dataCollection == null ? null : new BatchImpl<T>(dataCollection.dataCollection); }
From source file:com.flipkart.batching.gson.adapters.data.EventDataTypeAdapter.java
License:Open Source License
@Override public EventData read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }//w ww . j a va 2s .c om if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); EventData object = new EventData(); while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "eventId": object.setEventId(BatchingTypeAdapters.LONG.read(reader)); break; default: reader.skipValue(); break; } } reader.endObject(); return object; }
From source file:com.flipkart.batching.gson.adapters.data.TagDataTypeAdapter.java
License:Open Source License
@Override public TagData read(JsonReader reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; }//from w w w . ja v a 2 s . c om if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); Tag tag = null; long eventId = 0L; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "tag": tag = tagTypeAdapter.read(reader); break; case "eventId": eventId = BatchingTypeAdapters.LONG.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); TagData tagData = new TagData(tag); tagData.setEventId(eventId); return tagData; }