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:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Collection<Object> parseArray(final JsonReader jsonReader) throws IOException {
    assert null != jsonReader : "DEBUG: The json Reader is null";

    Collection<Object> l = new ArrayList<>();
    JsonToken tok;//  ww w  .  j  av a2s . c  o  m

    //
    while (jsonReader.hasNext()) {

        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            l.add(parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            l.add(parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            l.add(jsonReader.nextString());
            break;
        case BOOLEAN:
            l.add(jsonReader.nextBoolean());
            break;
        case NUMBER:
            l.add(jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            l.add(null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return l;
}

From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Map<String, Object> parseObject(final JsonReader jsonReader) throws IOException {

    Map<String, Object> map = new HashMap<>();
    String tempKey;//  w  ww .  java2s  . c om
    JsonToken tok;
    //
    while (jsonReader.hasNext()) {

        // since we want to restore a map we assue a key/value pair
        tempKey = jsonReader.nextName();
        //
        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            map.put(tempKey, parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            map.put(tempKey, parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            map.put(tempKey, jsonReader.nextString());
            break;
        case BOOLEAN:
            map.put(tempKey, jsonReader.nextBoolean());
            break;
        case NUMBER:
            map.put(tempKey, jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            map.put(tempKey, null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return map;
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private JsonObject next(final JsonParser parser, final JsonReader reader, final String sourceName)
        throws IOException {
    while (reader.hasNext() && reader.peek() != JsonToken.END_DOCUMENT) {
        try {/*from w  ww  .ja va2s  .c  om*/
            final JsonElement element = parser.parse(reader);
            if (element != null && element != JsonNull.INSTANCE) {
                return element.getAsJsonObject();
            }
        } catch (Exception e) {
            log.error(sourceName + " - error parsing json", e);
            return null;
        }
    }
    return null;
}

From source file:guru.qas.martini.report.DefaultTraceabilityMatrix.java

License:Apache License

@Override
public void createReport(JsonReader reader, OutputStream outputStream) throws IOException {
    checkNotNull(reader, "null JsonReader");
    checkNotNull(outputStream, "null OutputStream");

    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("Results");
    addHeader(sheet);//w  ww.  j a  v a  2 s  .c o m

    State state = new DefaultState();
    while (reader.hasNext()) {
        JsonToken peek = reader.peek();

        JsonObject object;
        switch (peek) {
        case BEGIN_ARRAY:
            reader.beginArray();
            continue;
        case BEGIN_OBJECT:
            object = gson.fromJson(reader, JsonObject.class);
            break;
        case END_ARRAY:
            reader.endArray();
            continue;
        case END_DOCUMENT:
            reader.skipValue();
            continue;
        default:
            JsonElement element = gson.fromJson(reader, JsonElement.class);
            LOGGER.warn("skipping unhandled element {}", element);
            continue;
        }

        switch (JsonObjectType.evaluate(object)) {
        case SUITE:
            JsonObject suite = SUITE.get(object);
            state.addSuite(suite);
            break;
        case FEATURE:
            JsonObject feature = FEATURE.get(object);
            state.addFeature(feature);
            break;
        case RESULT:
            JsonObject result = RESULT.get(object);
            addResult(state, sheet, result);
            break;
        default:
            LOGGER.warn("skipping unrecognized JsonObject: {}", object);
        }
    }

    state.updateResults();
    resizeColumns(sheet);

    Sheet suiteSheet = workbook.createSheet("Suites");
    state.updateSuites(suiteSheet);

    workbook.write(outputStream);
    outputStream.flush();
}

From source file:io.datakernel.datagraph.server.GsonClassTypeAdapter.java

License:Apache License

@Override
public Class<?> read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from  w ww .  j  a  v a2s .com*/
        return null;
    }
    try {
        String className = in.nextString();
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    }
}

From source file:io.datakernel.datagraph.server.GsonInetSocketAddressAdapter.java

License:Apache License

@Override
public InetSocketAddress read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*  w w  w . ja  v  a  2  s. c  o m*/
        return null;
    }
    try {
        Iterator<String> split = Splitter.on(':').split(reader.nextString()).iterator();
        InetAddress hostname = InetAddresses.forString(split.next());
        int port = Integer.parseInt(split.next());
        checkArgument(!split.hasNext());
        return new InetSocketAddress(hostname, port);
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:io.datakernel.datagraph.server.GsonStreamIdAdapter.java

License:Apache License

@Override
public StreamId read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from w  w w  .j av a 2 s  .  c  o m*/
        return null;
    }
    return new StreamId(reader.nextLong());
}

From source file:io.grpc.internal.JsonParser.java

License:Apache License

private static Object parseRecursive(JsonReader jr) throws IOException {
    checkState(jr.hasNext(), "unexpected end of JSON");
    switch (jr.peek()) {
    case BEGIN_ARRAY:
        return parseJsonArray(jr);
    case BEGIN_OBJECT:
        return parseJsonObject(jr);
    case STRING://  w  w w  .  j a v  a 2 s  . c om
        return jr.nextString();
    case NUMBER:
        return jr.nextDouble();
    case BOOLEAN:
        return jr.nextBoolean();
    case NULL:
        return parseJsonNull(jr);
    default:
        throw new IllegalStateException("Bad token: " + jr.getPath());
    }
}

From source file:io.grpc.internal.JsonParser.java

License:Apache License

private static Map<String, ?> parseJsonObject(JsonReader jr) throws IOException {
    jr.beginObject();//  w w w. j  av a2s .  c o m
    Map<String, Object> obj = new LinkedHashMap<>();
    while (jr.hasNext()) {
        String name = jr.nextName();
        Object value = parseRecursive(jr);
        obj.put(name, value);
    }
    checkState(jr.peek() == JsonToken.END_OBJECT, "Bad token: " + jr.getPath());
    jr.endObject();
    return Collections.unmodifiableMap(obj);
}

From source file:io.grpc.internal.JsonParser.java

License:Apache License

private static List<?> parseJsonArray(JsonReader jr) throws IOException {
    jr.beginArray();//from   w  w w  . j  a  va  2s  . co m
    List<Object> array = new ArrayList<>();
    while (jr.hasNext()) {
        Object value = parseRecursive(jr);
        array.add(value);
    }
    checkState(jr.peek() == JsonToken.END_ARRAY, "Bad token: " + jr.getPath());
    jr.endArray();
    return Collections.unmodifiableList(array);
}