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

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

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

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();
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();/*w ww  .  j a  v a2  s  . c o  m*/
    } 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  ww w .  j  a  va  2  s . co  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.ambari.view.hive.resources.uploads.parsers.json.JSONIterator.java

License:Apache License

public JSONIterator(JsonReader reader) throws IOException {
    this.reader = reader;
    // test the start of array
    JsonToken jt = reader.peek();
    if (jt != JsonToken.BEGIN_ARRAY) {
        throw new IllegalArgumentException("Expected the whole document to contain a single JsonArray.");
    }/*from  w  w w. j  a v a 2 s  .c  o m*/

    reader.beginArray(); // read the start of array
    try {
        nextObject = readNextObject(this.reader);
    } catch (EndOfDocumentException e) {
    }
}

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  w  w  w.  j  a  va  2s . c  o  m*/
        // use original deserializer logic
        return fallBack_original.fromJson(jsonReader, DataType.class);
    }
}

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

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<ContentSummary>() {
        @Override//  ww  w .  j a  va 2  s. c  o 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// w w  w . j av a2 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.jclouds.oneandone.rest.util.ServerFirewallPolicyAdapter.java

License:Apache License

@Override
public List<T> read(JsonReader reader) throws IOException {
    List<ServerFirewallPolicy> list = new ArrayList<ServerFirewallPolicy>();
    if (reader.peek() == JsonToken.BEGIN_OBJECT) {
        Type mapType = new TypeToken<Map<String, Object>>() {
        }.getType();/* w w  w  .  j a  va 2s.c o m*/
        Map<String, String> jsonMap = gson.fromJson(reader, mapType);
        ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name"));
        list.add(inning);

    } else if (reader.peek() == JsonToken.BEGIN_ARRAY) {

        reader.beginArray();
        while (reader.hasNext()) {
            Type mapType = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, String> jsonMap = gson.fromJson(reader, mapType);
            ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name"));
            list.add(inning);
        }
        reader.endArray();

    } else {
        reader.skipValue();
    }
    return (List<T>) list;
}

From source file:org.apache.jclouds.oneandone.rest.util.SnapshotAdapter.java

License:Apache License

@Override
public List<T> read(JsonReader reader) throws IOException {
    List<Snapshot> list = new ArrayList<Snapshot>();
    if (reader.peek() == JsonToken.BEGIN_OBJECT) {
        Type mapType = new TypeToken<Map<String, Object>>() {
        }.getType();//w  w w.  jav  a2 s  . c  om
        Map<String, String> jsonMap = gson.fromJson(reader, mapType);
        Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"),
                jsonMap.get("deletion_date"));
        list.add(inning);
    } else if (reader.peek() == JsonToken.BEGIN_ARRAY) {

        reader.beginArray();
        while (reader.hasNext()) {
            Type mapType = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, String> jsonMap = gson.fromJson(reader, mapType);
            Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"),
                    jsonMap.get("deletion_date"));
            list.add(inning);
        }
        reader.endArray();
    } else {
        reader.skipValue();
    }
    return (List<T>) list;
}

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();
    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  w w  w. j  ava  2  s .  c om*/
}

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 .ja  v  a 2 s  .  c o 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();
}