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

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

Introduction

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

Prototype

public String nextString() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#STRING string value of the next token, consuming it.

Usage

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

License:Apache License

private List<InPort> readApplicationInputs(JsonReader jsonReader) throws IOException, ParserException {
    List<InPort> inPorts = new ArrayList<>();
    JsonToken peek = jsonReader.peek();//from  w w  w . jav  a  2  s.  c om
    PortModel portModel;
    InPort inPort;
    String name;
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            portModel = new PortModel();
            inPort = new InputPortIml(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)) {
                    inPort.setDefaultValue(jsonReader.nextString());
                } else if (name.equals(DESCRIPTION)) {
                    portModel.setDescription(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            inPorts.add(inPort);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException(
                "Error! reading application inputs, expected " + getTokenString(JsonToken.NULL) + " or "
                        + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));
    }

    return inPorts;
}

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 .java 2  s  .c  om
    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.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  www  . jav  a2s  . 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.carbondata.core.metadata.datatype.DataTypeAdapter.java

License:Apache License

@Override
public Object read(JsonReader jsonReader) throws IOException {
    JsonToken token = jsonReader.peek();
    if (token == JsonToken.STRING) {
        return DataTypeUtil.valueOf(jsonReader.nextString());
    } else {//from  ww  w  .  j ava  2  s  .  c o m
        // use original deserializer logic
        return fallBack_original.fromJson(jsonReader, DataType.class);
    }
}

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

License:Apache License

/**
 * Create a Schema using JSON string, for example:
 * [//from  w  ww  . j a va  2  s  .  co 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.fs.http.client.FileStatus.java

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<FileStatus>() {
        @Override/*from   ww w  . j  a v a 2s  . 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 void parseInnerError(final JsonReader reader, final ODataErrorContext errorContext) throws IOException {
    JsonToken token = reader.peek();//  ww w  . j ava2s  . c o  m
    if (token == JsonToken.STRING) {
        // implementation for parse content as provided by JsonErrorDocumentProducer
        String innerError = reader.nextString();
        errorContext.setInnerError(innerError);
    } else if (token == JsonToken.BEGIN_OBJECT) {
        // implementation for OData v2 Section 2.2.8.1.2 JSON Error Response
        // (RFC4627 Section 2.2 -> https://www.ietf.org/rfc/rfc4627.txt))
        // currently partial provided
        errorContext.setInnerError(readJson(reader));
    }
}

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  ww w  . j ava 2s . 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();
}

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

License:Apache License

/**
 * Read the string value from the JsonReader or 'null' if no value is available.
 * //from   w ww. j  av a 2s.  c o  m
 * @param reader to read from
 * @return the string value or 'null'
 * @throws IOException if an exception occurs
 */
private String getValue(final JsonReader reader) throws IOException {
    JsonToken token = reader.peek();
    if (JsonToken.NULL == token) {
        reader.skipValue();
        return null;
    }
    return reader.nextString();
}

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

License:Apache License

private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
        final Object typeMapping, final EntityProviderReadProperties readProperties)
        throws EdmException, EntityProviderException, IOException {
    final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
    Object value = null;//from w  w w . j  av a2 s.  c  o m
    final JsonToken tokenType = reader.peek();
    if (tokenType == JsonToken.NULL) {
        reader.nextNull();
    } else {
        switch (EdmSimpleTypeKind.valueOf(type.getName())) {
        case Boolean:
            if (tokenType == JsonToken.BOOLEAN) {
                value = reader.nextBoolean();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Byte:
        case SByte:
        case Int16:
        case Int32:
            if (tokenType == JsonToken.NUMBER) {
                value = reader.nextInt();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Single:
        case Double:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else if (tokenType == JsonToken.NUMBER) {
                value = reader.nextDouble();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        default:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        }
    }

    final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
    final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets()
            ? entityPropertyInfo.getFacets()
            : null;
    return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}