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

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

Introduction

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

Prototype

public void endObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current object.

Usage

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

License:Apache License

private List<OutPort> readApplicationOutputs(JsonReader jsonReader) throws IOException, ParserException {
    List<OutPort> outPorts = new ArrayList<>();
    PortModel portModel;//from  w  w w  .  ja v a2s .  co m
    OutPort outPort;
    String name;
    JsonToken peek = jsonReader.peek();
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            portModel = new PortModel();
            outPort = new OutPortImpl(portModel);
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    portModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    portModel.setPortId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    jsonReader.skipValue();
                } else if (name.equals(DEFAULT_VALUE)) {
                    jsonReader.skipValue(); // can output has default values?
                } else if (name.equals(DESCRIPTION)) {
                    portModel.setDescription(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            outPorts.add(outPort);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException(
                "Error! reading application outputs, expected " + getTokenString(JsonToken.NULL) + " or "
                        + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));

    }
    return outPorts;
}

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

License:Apache License

private void readPosition(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();//from  w  ww  .jav a2 s  . c  om
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            // skip position data.
            jsonReader.nextName();
            jsonReader.skipValue();
        }
        jsonReader.endObject();
    } else {
        jsonReader.skipValue();
    }
}

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  va  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;/* w  w w  .jav a2 s.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 .ja  va2  s  .c o  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 ww w  .j  av  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 a 2 s  .c  o m
 *
 * {"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//from   www .j  av a 2  s.co  m
        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 www  .  j av  a  2 s.c  o m
        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 parseError(final JsonReader reader) throws IOException, EntityProviderException {
    ODataErrorContext errorContext = new ODataErrorContext();
    String currentName;//from   w  w w  . j a v a  2s . co  m
    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;
}