List of usage examples for com.google.gson.stream JsonReader nextString
public String nextString() throws IOException
From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java
License:Open Source License
private MeetingPayload extractPayload(JsonReader in) throws IOException, NumberFormatException, IllegalStateException, JsonParseException { NginxClojureRT.log.debug(TAG + "TypeAdapter extracting Payload..."); MeetingPayload meetingPayload = new MeetingPayload(); if (in.peek() == JsonToken.NULL) { in.nextNull();//from w w w . j a v a 2s . co m throw new JsonParseException("null Payload"); } in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "aps": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "badge": meetingPayload.getAps().setBadge(in.nextLong()); break; case "sound": meetingPayload.getAps().setSound(in.nextString()); break; case "alert": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "title": meetingPayload.getAps().getAlert().setTitle(in.nextString()); break; case "body": meetingPayload.getAps().getAlert().setBody(in.nextString()); break; case "action-loc-key": meetingPayload.getAps().getAlert().setActionLocKey(in.nextString()); break; } } in.endObject(); break; } } in.endObject(); break; case "dapi": meetingPayload.setDapi(in.nextString()); break; case "acme1": meetingPayload.setAcme1(in.nextString()); break; case "acme2": meetingPayload.setAcme2(in.nextLong()); break; case "acme3": meetingPayload.setAcme3(in.nextLong()); break; case "acme4": NginxClojureRT.log.info(TAG + "TypeAdapter Reader is reading acme4..."); meetingPayload.setAcme4(in.nextLong()); break; case "acme5": meetingPayload.setAcme5(in.nextLong()); break; case "acme6": meetingPayload.setAcme6(in.nextLong()); break; case "acme7": ArrayList<String> attendees = new ArrayList<>(); in.beginArray(); while (in.hasNext()) { attendees.add(in.nextString()); } in.endArray(); meetingPayload.setAcme7(attendees); break; case "acme8": meetingPayload.setAcme8(in.nextString()); break; } } in.endObject(); return meetingPayload; }
From source file:com.cardinity.json.UtcDateTypeAdapter.java
License:Apache License
@Override public Date read(JsonReader in) throws IOException { try {// ww w .j a va 2s. c o m switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); // Instead of using iso8601Format.parse(value), we use Jackson's date parsing // This is because Android doesn't support XXX because it is JDK 1.6 return parse(date, new ParsePosition(0)); } } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:com.cinchapi.concourse.util.Convert.java
License:Apache License
/** * Convert the next JSON object in the {@code reader} to a mapping that * associates each key with the Java objects that represent the * corresponding values.// ww w . j a va 2s .c o m * * <p> * This method has the same rules and limitations as * {@link #jsonToJava(String)}. It simply uses a {@link JsonReader} to * handle reading an array of objects. * </p> * <p> * <strong>This method DOES NOT {@link JsonReader#close()} the * {@code reader}.</strong> * </p> * * @param reader the {@link JsonReader} that contains a stream of JSON * @return the JSON data in the form of a {@link Multimap} from keys to * values */ private static Multimap<String, Object> jsonToJava(JsonReader reader) { Multimap<String, Object> data = HashMultimap.create(); try { reader.beginObject(); JsonToken peek0; while ((peek0 = reader.peek()) != JsonToken.END_OBJECT) { String key = reader.nextName(); peek0 = reader.peek(); if (peek0 == JsonToken.BEGIN_ARRAY) { // If we have an array, add the elements individually. If // there are any duplicates in the array, they will be // filtered out by virtue of the fact that a HashMultimap // does not store dupes. reader.beginArray(); JsonToken peek = reader.peek(); do { Object value; if (peek == JsonToken.BOOLEAN) { value = reader.nextBoolean(); } else if (peek == JsonToken.NUMBER) { value = stringToJava(reader.nextString()); } else if (peek == JsonToken.STRING) { String orig = reader.nextString(); value = stringToJava(orig); if (orig.isEmpty()) { value = orig; } // If the token looks like a string, it MUST be // converted to a Java string unless it is a // masquerading double or an instance of Thrift // translatable class that has a special string // representation (i.e. Tag, Link) else if (orig.charAt(orig.length() - 1) != 'D' && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) { value = value.toString(); } } else if (peek == JsonToken.NULL) { reader.skipValue(); continue; } else { throw new JsonParseException("Cannot parse nested object or array within an array"); } data.put(key, value); } while ((peek = reader.peek()) != JsonToken.END_ARRAY); reader.endArray(); } else { Object value; if (peek0 == JsonToken.BOOLEAN) { value = reader.nextBoolean(); } else if (peek0 == JsonToken.NUMBER) { value = stringToJava(reader.nextString()); } else if (peek0 == JsonToken.STRING) { String orig = reader.nextString(); value = stringToJava(orig); if (orig.isEmpty()) { value = orig; } // If the token looks like a string, it MUST be // converted to a Java string unless it is a // masquerading double or an instance of Thrift // translatable class that has a special string // representation (i.e. Tag, Link) else if (orig.charAt(orig.length() - 1) != 'D' && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) { value = value.toString(); } } else if (peek0 == JsonToken.NULL) { reader.skipValue(); continue; } else { throw new JsonParseException("Cannot parse nested object to value"); } data.put(key, value); } } reader.endObject(); return data; } catch (IOException | IllegalStateException e) { throw new JsonParseException(e.getMessage()); } }
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; }//from ww w. java 2s .c om 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.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
@Override public T read(final JsonReader in) throws IOException { String jobName = ""; String cron = ""; int shardingTotalCount = 0; String shardingItemParameters = ""; String jobParameter = ""; boolean failover = false; boolean misfire = failover; String description = ""; JobProperties jobProperties = new JobProperties(); JobEventConfiguration[] jobEventConfigs = null; JobType jobType = null;/*from w ww . ja v a2 s . c om*/ String jobClass = ""; DataflowJobConfiguration.DataflowType dataflowType = null; boolean streamingProcess = false; int concurrentDataProcessThreadCount = 0; String scriptCommandLine = ""; Map<String, Object> customizedValueMap = new HashMap<>(32, 1); in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case "jobName": jobName = in.nextString(); break; case "cron": cron = in.nextString(); break; case "shardingTotalCount": shardingTotalCount = in.nextInt(); break; case "shardingItemParameters": shardingItemParameters = in.nextString(); break; case "jobParameter": jobParameter = in.nextString(); break; case "failover": failover = in.nextBoolean(); break; case "misfire": misfire = in.nextBoolean(); break; case "description": description = in.nextString(); break; case "jobProperties": jobProperties = getJobProperties(in); break; case "jobEventConfigs": jobEventConfigs = getJobEventConfigs(in); break; case "jobType": jobType = JobType.valueOf(in.nextString()); break; case "jobClass": jobClass = in.nextString(); break; case "dataflowType": dataflowType = DataflowJobConfiguration.DataflowType.valueOf(in.nextString()); break; case "streamingProcess": streamingProcess = in.nextBoolean(); break; case "concurrentDataProcessThreadCount": concurrentDataProcessThreadCount = in.nextInt(); break; case "scriptCommandLine": scriptCommandLine = in.nextString(); break; default: addToCustomizedValueMap(jsonName, in, customizedValueMap); break; } } in.endObject(); JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount, shardingItemParameters, jobParameter, failover, misfire, description, jobProperties, jobEventConfigs); JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, dataflowType, streamingProcess, concurrentDataProcessThreadCount, scriptCommandLine); return getJobRootConfiguration(typeConfig, customizedValueMap); }
From source file:com.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
private JobProperties getJobProperties(final JsonReader in) throws IOException { JobProperties result = new JobProperties(); in.beginObject();//from w ww .java 2 s . c o m while (in.hasNext()) { switch (in.nextName()) { case "job_exception_handler": result.put(JobProperties.JobPropertiesEnum.JOB_EXCEPTION_HANDLER.getKey(), in.nextString()); break; case "executor_service_handler": result.put(JobProperties.JobPropertiesEnum.EXECUTOR_SERVICE_HANDLER.getKey(), in.nextString()); break; default: break; } } in.endObject(); return result; }
From source file:com.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
private JobEventConfiguration[] getJobEventConfigs(final JsonReader in) throws IOException { List<JobEventConfiguration> result = new ArrayList<>(2); in.beginObject();// w ww. j a v a2s . co m while (in.hasNext()) { String name = in.nextName(); switch (name) { case "log": in.beginObject(); result.add(new JobLogEventConfiguration()); in.endObject(); break; case "rdb": String url = ""; String username = ""; String password = ""; String driverClassName = ""; String logLevel = ""; in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "url": url = in.nextString(); break; case "username": username = in.nextString(); break; case "password": password = in.nextString(); break; case "driverClassName": driverClassName = in.nextString(); break; case "logLevel": logLevel = in.nextString(); break; default: break; } } in.endObject(); result.add(new JobRdbEventConfiguration(driverClassName, url, username, password, LogLevel.valueOf(logLevel.toUpperCase()))); break; default: break; } } in.endObject(); return Iterables.toArray(result, JobEventConfiguration.class); }
From source file:com.dangdang.ddframe.job.util.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
@Override public T read(final JsonReader in) throws IOException { String jobName = ""; String cron = ""; int shardingTotalCount = 0; String shardingItemParameters = ""; String jobParameter = ""; boolean failover = false; boolean misfire = failover; String description = ""; JobProperties jobProperties = new JobProperties(); JobEventConfiguration[] jobEventConfigs = null; JobType jobType = null;/* w w w . j a v a 2 s. c o m*/ String jobClass = ""; boolean streamingProcess = false; String scriptCommandLine = ""; Map<String, Object> customizedValueMap = new HashMap<>(32, 1); in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case "jobName": jobName = in.nextString(); break; case "cron": cron = in.nextString(); break; case "shardingTotalCount": shardingTotalCount = in.nextInt(); break; case "shardingItemParameters": shardingItemParameters = in.nextString(); break; case "jobParameter": jobParameter = in.nextString(); break; case "failover": failover = in.nextBoolean(); break; case "misfire": misfire = in.nextBoolean(); break; case "description": description = in.nextString(); break; case "jobProperties": jobProperties = getJobProperties(in); break; case "jobEventConfigs": jobEventConfigs = getJobEventConfigs(in); break; case "jobType": jobType = JobType.valueOf(in.nextString()); break; case "jobClass": jobClass = in.nextString(); break; case "streamingProcess": streamingProcess = in.nextBoolean(); break; case "scriptCommandLine": scriptCommandLine = in.nextString(); break; default: addToCustomizedValueMap(jsonName, in, customizedValueMap); break; } } in.endObject(); JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount, shardingItemParameters, jobParameter, failover, misfire, description, jobProperties, jobEventConfigs); JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, streamingProcess, scriptCommandLine); return getJobRootConfiguration(typeConfig, customizedValueMap); }
From source file:com.dangdang.ddframe.job.util.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
private JobEventConfiguration[] getJobEventConfigs(final JsonReader in) throws IOException { List<JobEventConfiguration> result = new ArrayList<>(2); in.beginObject();/*w ww.java 2 s . c o m*/ while (in.hasNext()) { String name = in.nextName(); switch (name) { case "log": in.beginObject(); result.add(new JobEventLogConfiguration()); in.endObject(); break; case "rdb": String url = ""; String username = ""; String password = ""; String driverClassName = ""; String logLevel = ""; in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "url": url = in.nextString(); break; case "username": username = in.nextString(); break; case "password": password = in.nextString(); break; case "driverClassName": driverClassName = in.nextString(); break; case "logLevel": logLevel = in.nextString(); break; default: break; } } in.endObject(); result.add(new JobEventRdbConfiguration(driverClassName, url, username, password, LogLevel.valueOf(logLevel.toUpperCase()))); break; default: break; } } in.endObject(); return Iterables.toArray(result, JobEventConfiguration.class); }
From source file:com.dangdang.ddframe.job.util.json.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
@Override public T read(final JsonReader in) throws IOException { String jobName = ""; String cron = ""; int shardingTotalCount = 0; String shardingItemParameters = ""; String jobParameter = ""; boolean failover = false; boolean misfire = failover; String description = ""; JobProperties jobProperties = new JobProperties(); JobType jobType = null;/*from w ww .j a va 2 s . c o m*/ String jobClass = ""; boolean streamingProcess = false; String scriptCommandLine = ""; Map<String, Object> customizedValueMap = new HashMap<>(32, 1); in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case "jobName": jobName = in.nextString(); break; case "cron": cron = in.nextString(); break; case "shardingTotalCount": shardingTotalCount = in.nextInt(); break; case "shardingItemParameters": shardingItemParameters = in.nextString(); break; case "jobParameter": jobParameter = in.nextString(); break; case "failover": failover = in.nextBoolean(); break; case "misfire": misfire = in.nextBoolean(); break; case "description": description = in.nextString(); break; case "jobProperties": jobProperties = getJobProperties(in); break; case "jobType": jobType = JobType.valueOf(in.nextString()); break; case "jobClass": jobClass = in.nextString(); break; case "streamingProcess": streamingProcess = in.nextBoolean(); break; case "scriptCommandLine": scriptCommandLine = in.nextString(); break; default: addToCustomizedValueMap(jsonName, in, customizedValueMap); break; } } in.endObject(); JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount, shardingItemParameters, jobParameter, failover, misfire, description, jobProperties); JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, streamingProcess, scriptCommandLine); return getJobRootConfiguration(typeConfig, customizedValueMap); }