Example usage for com.google.gson.stream JsonReader nextName

List of usage examples for com.google.gson.stream JsonReader nextName

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextName.

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

From source file:com.atlauncher.adapter.ColorTypeAdapter.java

License:Open Source License

@Override
public Color read(JsonReader reader) throws IOException {
    reader.beginObject();/*from   w w w. j av a2 s  . co  m*/
    String next = reader.nextName();
    if (!next.equalsIgnoreCase("value")) {
        throw new JsonParseException("Key " + next + " isnt a valid key");
    }
    String hex = reader.nextString();
    reader.endObject();
    int[] rgb = toRGB(clamp(hex.substring(1)));
    return new Color(rgb[0], rgb[1], rgb[2]);
}

From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java

License:Open Source License

@Override
public Receipient read(JsonReader in)
        throws IOException, IllegalStateException, JsonParseException, NumberFormatException {
    NginxClojureRT.log.debug(TAG + " invoked...");
    Receipient receipient = new Receipient();
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from  ww  w  .  j a  va  2  s . c  o m*/
        return null;
    }

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "apns_token":
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
            } else
                receipient.setApns_Token(in.nextString());
            break;
        case "fcm_token":
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
            } else
                receipient.setFcm_Token(in.nextString());
            break;
        case "payload":
            receipient.setPayload(extractPayload(in));
            break;
        }
    }
    in.endObject();

    NginxClojureRT.log.debug(TAG + "Deserializing and adding receipient of "
            + (receipient.getApns_Token() == null ? "fcm--" + receipient.getFcm_Token()
                    : "apns--" + receipient.getApns_Token()));
    return receipient;
}

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();/*  www  . j a va 2  s  .  com*/
        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.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.//from  w w w.java 2  s.co  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.confighub.core.utils.Utils.java

License:Open Source License

private static void skipToken(final JsonReader reader) throws IOException {
    final JsonToken token = reader.peek();
    switch (token) {
    case BEGIN_ARRAY:
        reader.beginArray();//from   w  w  w.java 2  s. co  m
        break;
    case END_ARRAY:
        reader.endArray();
        break;
    case BEGIN_OBJECT:
        reader.beginObject();
        break;
    case END_OBJECT:
        reader.endObject();
        break;
    case NAME:
        reader.nextName();
        break;
    case STRING:
    case NUMBER:
    case BOOLEAN:
    case NULL:
        reader.skipValue();
        break;
    case END_DOCUMENT:
    default:
        throw new AssertionError(token);
    }
}

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;/* w w  w.  j  av a 2 s .  c  o  m*/
    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();/*  w  w  w  .  j  ava  2s . co  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();//from w  ww.ja v a 2s  .  c  o 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;/*from   w  w  w . ja  v  a2  s .  co  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();//from  w  ww  . j a v a2 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);
}