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

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

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readProperties(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();/*  ww w  .  j a v a  2 s .c  o  m*/
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            // TODO: Read and use proprety values
            String name = jsonReader.nextName();
            jsonReader.skipValue();
        }
        jsonReader.endObject();
    } else {
        jsonReader.skipValue();
    }

}

From source file:org.apache.ambari.view.hive.resources.uploads.parsers.json.JSONIterator.java

License:Apache License

private LinkedHashMap<String, String> readNextObject(JsonReader reader)
        throws IOException, EndOfDocumentException {
    LinkedHashMap<String, String> row = new LinkedHashMap<>();
    boolean objectStarted = false;
    boolean shouldBeName = false;
    String currentName = null;//from  w w w. j a v  a 2s  .  c  o m

    while (true) {
        JsonToken token = reader.peek();
        switch (token) {
        case BEGIN_ARRAY:
            throw new IllegalArgumentException("Row data cannot have an array.");
        case END_ARRAY:
            throw new EndOfDocumentException("End of Json Array document.");
        case BEGIN_OBJECT:
            if (objectStarted == true) {
                throw new IllegalArgumentException("Nested objects not supported.");
            }
            if (shouldBeName == true) {
                throw new IllegalArgumentException("name expected, got begin_object");
            }
            objectStarted = true;
            shouldBeName = true;
            reader.beginObject();
            break;
        case END_OBJECT:
            if (shouldBeName == false) {
                throw new IllegalArgumentException("value expected, got end_object");
            }
            reader.endObject();
            return row;
        case NAME:
            if (shouldBeName == false) {
                throw new IllegalArgumentException("name not expected at this point.");
            }
            shouldBeName = false;
            currentName = reader.nextName();
            break;
        case NUMBER:
        case STRING:
            if (shouldBeName == true) {
                throw new IllegalArgumentException("value not expected at this point.");
            }
            String n = reader.nextString();
            row.put(currentName, n);
            shouldBeName = true;
            break;
        case BOOLEAN:
            if (shouldBeName == true) {
                throw new IllegalArgumentException("value not expected at this point.");
            }
            String b = String.valueOf(reader.nextBoolean());
            row.put(currentName, b);
            shouldBeName = true;
            break;
        case NULL:
            if (shouldBeName == true) {
                throw new IllegalArgumentException("value not expected at this point.");
            }
            reader.nextNull();
            row.put(currentName, "");
            shouldBeName = true;
            break;
        case END_DOCUMENT:
            return row;

        default:
            throw new IllegalArgumentException(
                    "Illegal token detected inside json: token : " + token.toString());
        }
    }
}

From source file:org.apache.axis2.json.gson.rpc.JsonUtils.java

License:Apache License

public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation,
        Class[] paramClasses, int paramCount)
        throws InvocationTargetException, IllegalAccessException, IOException {

    Object[] methodParam = new Object[paramCount];
    Gson gson = new Gson();
    String[] argNames = new String[paramCount];

    if (!jsonReader.isLenient()) {
        jsonReader.setLenient(true);/*from ww  w .j a v a  2  s . co m*/
    }
    jsonReader.beginObject();
    jsonReader.nextName(); // get message name from input json stream
    jsonReader.beginArray();

    int i = 0;
    for (Class paramType : paramClasses) {
        jsonReader.beginObject();
        argNames[i] = jsonReader.nextName();
        methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it
        jsonReader.endObject();
        i++;
    }

    jsonReader.endArray();
    jsonReader.endObject();

    return operation.invoke(service, methodParam);

}

From source file:org.apache.carbondata.sdk.file.Schema.java

License:Apache License

/**
 * Create a Schema using JSON string, for example:
 * [/*from  www.  j a v a 2s  .  c o m*/
 *   {"name":"string"},
 *   {"age":"int"}
 * ]
 * @param json specified as string
 * @return Schema
 */
public static Schema parseJson(String json) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Field.class, new TypeAdapter<Field>() {
        @Override
        public void write(JsonWriter out, Field field) throws IOException {
            // noop
        }

        @Override
        public Field read(JsonReader in) throws IOException {
            in.beginObject();
            Field field = new Field(in.nextName(), in.nextString());
            in.endObject();
            return field;
        }
    });

    Field[] fields = gsonBuilder.create().fromJson(json, Field[].class);
    return new Schema(fields);
}

From source file:org.apache.hadoop.dynamodb.importformat.ImportInputFormat.java

License:Open Source License

/**
 * An example manifest file looks like/*  w  w  w .  j  a  v a2 s . c om*/
 *
 * {"name":"DynamoDB-export","version":3, "entries":[
 * {"url":"s3://path/to/object/92dd1414-a049-4c68-88fb-a23acd44907e","mandatory":true},
 * {"url":"s3://path/to/object/ba3f3535-7aa1-4f97-a530-e72938bf4b76","mandatory":true} ]}
 */
// @formatter:on
private List<InputSplit> parseManifest(FileSystem fs, Path manifestPath, JobConf job) throws IOException {
    List<InputSplit> splits = null;

    FSDataInputStream fp = fs.open(manifestPath);
    JsonReader reader = new JsonReader(new InputStreamReader(fp, Charsets.UTF_8));

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
        case VERSION_JSON_KEY:
            job.set(DynamoDBConstants.EXPORT_FORMAT_VERSION, String.valueOf(reader.nextInt()));
            break;
        case ENTRIES_JSON_KEY:
            splits = readEntries(reader, job);
            break;
        default:
            log.info("Skipping a JSON key in the manifest file: " + name);
            reader.skipValue();
            break;
        }
    }
    reader.endObject();

    if (splits == null) {
        return Collections.emptyList();
    }
    return splits;
}

From source file:org.apache.hadoop.fs.http.client.ContentSummary.java

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<ContentSummary>() {
        @Override// www .  j  av a 2s  . c  om
        public void write(JsonWriter out, ContentSummary value) throws IOException {
            /* not implemented */
        }

        @Override
        public ContentSummary read(JsonReader in) throws IOException {
            ContentSummary instance = null;
            in.setLenient(true);

            if (in.peek() == JsonToken.BEGIN_OBJECT) {
                in.beginObject();

                if (in.nextName().equalsIgnoreCase("ContentSummary")) {
                    String name;
                    in.beginObject();
                    instance = new ContentSummary();

                    while (in.hasNext()) {
                        name = in.nextName();

                        if (name.equalsIgnoreCase("directoryCount")) {
                            instance.directoryCount = in.nextInt();
                        } else if (name.equalsIgnoreCase("fileCount")) {
                            instance.fileCount = in.nextInt();
                        } else if (name.equalsIgnoreCase("length")) {
                            instance.length = in.nextInt();
                        } else if (name.equalsIgnoreCase("quota")) {
                            instance.quota = in.nextInt();
                        } else if (name.equalsIgnoreCase("spaceConsumed")) {
                            instance.spaceConsumed = in.nextInt();
                        } else if (name.equalsIgnoreCase("spaceQuota")) {
                            instance.spaceQuota = in.nextInt();
                        }
                    }

                    in.endObject();
                }

                in.endObject();
            }
            return instance;
        }
    };
}

From source file:org.apache.hadoop.fs.http.client.FileStatus.java

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<FileStatus>() {
        @Override//from   w  w  w.java  2s  .com
        public void write(JsonWriter out, FileStatus value) throws IOException {
            /* not implemented */
        }

        @Override
        public FileStatus read(JsonReader in) throws IOException {
            FileStatus instance = null;
            in.setLenient(true);

            if (in.peek() == JsonToken.BEGIN_OBJECT) {
                in.beginObject();

                if (in.nextName().equalsIgnoreCase("FileStatus")) {
                    String name;
                    in.beginObject();
                    instance = new FileStatus();

                    while (in.hasNext()) {
                        name = in.nextName();

                        if (name.equalsIgnoreCase("accessTime")) {
                            instance.accessTime = in.nextLong();
                        } else if (name.equalsIgnoreCase("blockSize")) {
                            instance.blockSize = in.nextInt();
                        } else if (name.equalsIgnoreCase("length")) {
                            instance.length = in.nextLong();
                        } else if (name.equalsIgnoreCase("modificationTime")) {
                            instance.modTime = in.nextLong();
                        } else if (name.equalsIgnoreCase("replication")) {
                            instance.replication = in.nextInt();
                        } else if (name.equalsIgnoreCase("group")) {
                            instance.group = in.nextString();
                        } else if (name.equalsIgnoreCase("owner")) {
                            instance.owner = in.nextString();
                        } else if (name.equalsIgnoreCase("pathSuffix")) {
                            instance.suffix = in.nextString();
                        } else if (name.equalsIgnoreCase("permission")) {
                            instance.permission = in.nextString();
                        } else if (name.equalsIgnoreCase("type")) {
                            instance.type = FileType.valueOf(in.nextString());
                        }
                    }

                    in.endObject();
                }

                in.endObject();
            }
            return instance;
        }
    };
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private ODataErrorContext parseJson(final JsonReader reader) throws IOException, EntityProviderException {
    ODataErrorContext errorContext;//from   w w  w.j  av  a2  s  .c  om

    if (reader.hasNext()) {
        reader.beginObject();
        String currentName = reader.nextName();
        if (FormatJson.ERROR.equals(currentName)) {
            errorContext = parseError(reader);
        } else {
            throw new EntityProviderException(EntityProviderException.INVALID_STATE
                    .addContent("Invalid object with name '" + currentName + "' found."));
        }
    } else {
        throw new EntityProviderException(
                EntityProviderException.INVALID_STATE.addContent("No content to parse found."));
    }

    errorContext.setContentType(ContentType.APPLICATION_JSON.toContentTypeString());
    return errorContext;
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private ODataErrorContext parseError(final JsonReader reader) throws IOException, EntityProviderException {
    ODataErrorContext errorContext = new ODataErrorContext();
    String currentName;/*from  w  w  w.  j ava2  s  .  c  om*/
    reader.beginObject();
    boolean messageFound = false;
    boolean codeFound = false;

    while (reader.hasNext()) {
        currentName = reader.nextName();
        if (FormatJson.CODE.equals(currentName)) {
            codeFound = true;
            errorContext.setErrorCode(getValue(reader));
        } else if (FormatJson.MESSAGE.equals(currentName)) {
            messageFound = true;
            parseMessage(reader, errorContext);
        } else if (FormatJson.INNER_ERROR.equals(currentName)) {
            parseInnerError(reader, errorContext);
        } else {
            throw new EntityProviderException(EntityProviderException.INVALID_STATE
                    .addContent("Invalid name '" + currentName + "' found."));
        }
    }

    if (!codeFound) {
        throw new EntityProviderException(
                EntityProviderException.MISSING_PROPERTY.addContent("Mandatory 'code' property not found.'"));
    }
    if (!messageFound) {
        throw new EntityProviderException(EntityProviderException.MISSING_PROPERTY
                .addContent("Mandatory 'message' property not found.'"));
    }

    reader.endObject();
    return errorContext;
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private String readJson(final JsonReader reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        if (token == JsonToken.NAME) {
            if (sb.length() > 0) {
                sb.append(",");
            }//from  w w w. j  a  v  a2s .  co  m
            String name = reader.nextName();
            sb.append("\"").append(name).append("\"").append(":");
        } else if (token == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
            sb.append("{").append(readJson(reader)).append("}");
            reader.endObject();
        } else if (token == JsonToken.BEGIN_ARRAY) {
            reader.beginArray();
            sb.append("[").append(readJson(reader)).append("]");
            reader.endArray();
        } else {
            sb.append("\"").append(reader.nextString()).append("\"");
        }
    }

    return sb.toString();
}