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:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs a {@link Schema} from the "key":"schema" pair.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param key The json property name that need to match.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} object representing the schema of the json input.
 * @throws IOException When fails to construct a valid schema from the input.
 *//* www.  ja  v  a  2 s.co  m*/
private Schema readInnerSchema(JsonReader reader, String key, Map<String, Schema> knownRecords)
        throws IOException {
    if (!key.equals(reader.nextName())) {
        throw new IOException("Property \"" + key + "\" missing.");
    }
    return read(reader, knownRecords);
}

From source file:co.cask.common.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Reads json value and convert it into {@link Schema} object.
 *
 * @param reader Source of json//  w w w  . j  a va 2  s.  c o  m
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} reflecting the json.
 * @throws java.io.IOException Any error during reading.
 */
private Schema read(JsonReader reader, Set<String> knownRecords) throws IOException {
    JsonToken token = reader.peek();
    switch (token) {
    case NULL:
        return null;
    case STRING: {
        // Simple type or know record type
        String name = reader.nextString();
        if (knownRecords.contains(name)) {
            return Schema.recordOf(name);
        }
        return Schema.of(Schema.Type.valueOf(name.toUpperCase()));
    }
    case BEGIN_ARRAY:
        // Union type
        return readUnion(reader, knownRecords);
    case BEGIN_OBJECT: {
        reader.beginObject();
        String name = reader.nextName();
        if (!"type".equals(name)) {
            throw new IOException("Property \"type\" missing.");
        }
        Schema.Type schemaType = Schema.Type.valueOf(reader.nextString().toUpperCase());

        Schema schema;
        switch (schemaType) {
        case ENUM:
            schema = readEnum(reader);
            break;
        case ARRAY:
            schema = readArray(reader, knownRecords);
            break;
        case MAP:
            schema = readMap(reader, knownRecords);
            break;
        case RECORD:
            schema = readRecord(reader, knownRecords);
            break;
        default:
            schema = Schema.of(schemaType);
        }
        reader.endObject();
        return schema;
    }
    }
    throw new IOException("Malformed schema input.");
}

From source file:co.cask.common.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#ENUM ENUM} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @return A {@link Schema} of type {@link Schema.Type#ENUM ENUM}.
 * @throws java.io.IOException When fails to construct a valid schema from the input.
 *///from  w ww  . j ava 2s .  co m
private Schema readEnum(JsonReader reader) throws IOException {
    if (!"symbols".equals(reader.nextName())) {
        throw new IOException("Property \"symbols\" missing for enum.");
    }
    ImmutableList.Builder<String> enumValues = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        enumValues.add(reader.nextString());
    }
    reader.endArray();
    return Schema.enumWith(enumValues.build());
}

From source file:co.cask.common.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#RECORD RECORD} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} of type {@link Schema.Type#RECORD RECORD}.
 * @throws java.io.IOException When fails to construct a valid schema from the input.
 */// w  w  w.j  a  v a2 s . com
private Schema readRecord(JsonReader reader, Set<String> knownRecords) throws IOException {
    if (!"name".equals(reader.nextName())) {
        throw new IOException("Property \"name\" missing for record.");
    }

    String recordName = reader.nextString();

    // Read in fields schemas
    if (!"fields".equals(reader.nextName())) {
        throw new IOException("Property \"fields\" missing for record.");
    }

    knownRecords.add(recordName);

    ImmutableList.Builder<Schema.Field> fieldBuilder = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        reader.beginObject();
        if (!"name".equals(reader.nextName())) {
            throw new IOException("Property \"name\" missing for record field.");
        }
        String fieldName = reader.nextString();
        fieldBuilder.add(Schema.Field.of(fieldName, readInnerSchema(reader, "type", knownRecords)));
        reader.endObject();
    }
    reader.endArray();
    return Schema.recordOf(recordName, fieldBuilder.build());
}

From source file:co.cask.common.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs a {@link Schema} from the "key":"schema" pair.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param key The json property name that need to match.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} object representing the schema of the json input.
 * @throws java.io.IOException When fails to construct a valid schema from the input.
 *//*from   w w w. ja v  a 2  s .com*/
private Schema readInnerSchema(JsonReader reader, String key, Set<String> knownRecords) throws IOException {
    if (!key.equals(reader.nextName())) {
        throw new IOException("Property \"" + key + "\" missing.");
    }
    return read(reader, knownRecords);
}

From source file:co.moonmonkeylabs.flowmortarexampleapp.common.flow.GsonParceler.java

License:Apache License

private Object decode(String json) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(json));

    try {/*from  w w w.  j  a va2 s. co m*/
        reader.beginObject();

        Class<?> type = Class.forName(reader.nextName());
        return gson.fromJson(reader, type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        reader.close();
    }
}

From source file:com.aelitis.azureus.util.ObjectTypeAdapterLong.java

License:Apache License

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();//ww  w  .ja  v a2 s  .  c o  m
    switch (token) {
    case BEGIN_ARRAY:
        List<Object> list = new ArrayList<Object>();
        in.beginArray();
        while (in.hasNext()) {
            list.add(read(in));
        }
        in.endArray();
        return list;

    case BEGIN_OBJECT:
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        in.beginObject();
        while (in.hasNext()) {
            map.put(in.nextName(), read(in));
        }
        in.endObject();
        return map;

    case STRING:
        return in.nextString();

    case NUMBER: {
        String value = in.nextString();
        if (value.indexOf('.') >= 0) {
            return Double.parseDouble(value);
        } else {
            return Long.parseLong(value);
        }
    }

    case BOOLEAN:
        return in.nextBoolean();

    case NULL:
        in.nextNull();
        return null;

    default:
        throw new IllegalStateException();
    }
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private List<FuxiJob> loadJobsFromStream(InputStream in) throws ODPSConsoleException {
    boolean debug = true;
    ArrayList<FuxiJob> jobs = new ArrayList<FuxiJob>();
    if (debug) {/* w w  w.  ja va 2  s. co  m*/
        try {
            JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("mapReduce")) {
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String nameInMapReduce = reader.nextName();
                        if (nameInMapReduce.equals("jobs")) {
                            reader.beginArray();
                            while (reader.hasNext()) {
                                jobs.add(getFuxiJobFromJson(reader));
                            }
                            reader.endArray();
                        } else if (nameInMapReduce.equals("jsonSummary")) {
                            getInfoFromJsonSummary(jobs, reader.nextString());
                        } else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ODPSConsoleException("Bad json format");
        }
    }

    return jobs;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiJob getFuxiJobFromJson(JsonReader reader) throws IOException {
    FuxiJob job = new FuxiJob();

    reader.beginObject();//from   w  w  w  . j a  v  a  2s.  c o  m
    while (reader.hasNext()) {
        String nameInJob = reader.nextName();
        if (nameInJob.equals("name")) {
            job.name = reader.nextString();
        } else if (nameInJob.equals("tasks")) {
            reader.beginArray();
            job.tasks = new ArrayList<FuxiTask>();
            while (reader.hasNext()) {
                job.tasks.add(getFuxiTaskFromJson(reader));
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return job;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiTask getFuxiTaskFromJson(JsonReader reader) throws IOException {
    FuxiTask task = new FuxiTask();

    reader.beginObject();/*from   w  ww  .j  a v a2s  .  c o m*/
    while (reader.hasNext()) {
        String nameInTask = reader.nextName();
        if (nameInTask.equals("name")) {
            task.name = reader.nextString();
        } else if (nameInTask.equals("instances")) {
            task.instances = new ArrayList<FuxiInstance>();
            task.upTasks = new ArrayList<FuxiTask>();
            task.downTasks = new ArrayList<FuxiTask>();
            reader.beginArray();
            while (reader.hasNext()) {
                task.instances.add(getFuxiInstanceFromJson(reader));
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return task;
}