Example usage for com.google.gson JsonStreamParser next

List of usage examples for com.google.gson JsonStreamParser next

Introduction

In this page you can find the example usage for com.google.gson JsonStreamParser next.

Prototype

public JsonElement next() throws JsonParseException 

Source Link

Document

Returns the next available JsonElement on the reader.

Usage

From source file:com.facebook.buck.apple.XctoolOutputParsing.java

License:Apache License

/**
 * Decodes a stream of JSON objects as produced by {@code xctool -reporter json-stream}
 * and invokes the callbacks in {@code eventCallback} with each event in the stream.
 *//*from  w w w  . j a v a  2s.c o m*/
public static void streamOutputFromReader(Reader reader, XctoolEventCallback eventCallback) {
    Gson gson = new Gson();
    JsonStreamParser streamParser = new JsonStreamParser(reader);
    while (streamParser.hasNext()) {
        try {
            dispatchEventCallback(gson, streamParser.next(), eventCallback);
        } catch (JsonParseException e) {
            LOG.warn(e, "Couldn't parse xctool JSON stream");
        }
    }
}

From source file:com.flipkart.polyguice.config.JsonConfiguration.java

License:Apache License

private void load(Reader in) {
    configTab = new HashMap<>();
    JsonStreamParser parser = new JsonStreamParser(in);
    JsonElement root = null;/*w ww. j  ava  2 s. co  m*/
    if (parser.hasNext()) {
        root = parser.next();
    }
    if (root != null && root.isJsonObject()) {
        flatten(null, root);
    }
    LOGGER.debug("json configuration loaded: {}", configTab);
}

From source file:jGraphprocessingsystem.JsonDataFormat.java

public ArrayList<User> getData(String directory) {
    ArrayList<User> listUser = new ArrayList<>();
    try {/*from   www.  j a v  a  2 s.  co m*/
        JsonStreamParser parser = new JsonStreamParser(new FileReader(directory));
        while (parser.hasNext()) {
            User user = gson.fromJson(parser.next(), User.class);
            listUser.add(user);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(JsonDataFormat.class.getName()).log(Level.SEVERE, null, ex);
    }
    return listUser;
}

From source file:net.wouterdanes.docker.remoteapi.ImagesService.java

License:Apache License

private static void parseStreamToDisplayImageDownloadStatus(final InputStream inputStream) {
    InputStreamReader isr = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(isr);

    JsonStreamParser parser = new JsonStreamParser(reader);

    while (parser.hasNext()) {
        JsonElement element = parser.next();
        JsonObject object = element.getAsJsonObject();
        if (object.has("status")) {
            System.out.print(".");
        }/*w ww . j  a  va2s .  c  o m*/
        if (object.has("error")) {
            System.err.println("ERROR: " + object.get("error").getAsString());
        }
    }
    System.out.println("");
}

From source file:net.wouterdanes.docker.remoteapi.MiscService.java

License:Apache License

private static String parseSteamForImageId(final InputStream inputStream) {
    InputStreamReader isr = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(isr);

    JsonStreamParser parser = new JsonStreamParser(reader);

    String imageId = null;//ww w.  j  av a 2  s  .  co m

    while (parser.hasNext()) {
        JsonElement element = parser.next();
        JsonObject object = element.getAsJsonObject();
        if (object.has("stream")) {
            String text = object.get("stream").getAsString();
            System.out.print(text);
            Matcher matcher = BUILD_IMAGE_ID_EXTRACTION_PATTERN.matcher(text);
            if (matcher.matches()) {
                imageId = matcher.group(1);
            }
        }
        if (object.has("status")) {
            System.out.println(object.get("status").getAsString());
        }
        if (object.has("error")) {
            System.err.println("ERROR: " + object.get("error").getAsString());
        }
    }
    return imageId;
}

From source file:org.apache.drill.plan.physical.operators.ScanJson.java

License:Apache License

@Override
public Object call() throws IOException {
    int count = 0;
    Reader in = input.getInput();
    JsonStreamParser jp = new JsonStreamParser(in);
    while (jp.hasNext()) {
        JsonElement r = jp.next();
        emit(r);//from   w ww . j a  v a2s . c  om
        count++;
    }
    in.close();
    // TODO what should the parent record be at the top-level?
    finishBatch(null);

    return count;
}

From source file:org.apache.edgent.connectors.http.HttpResponders.java

License:Apache License

/**
 * A HTTP response handler for {@code application/json}.
 * //from ww w .  java  2 s . c om
 * For each HTTP response a JSON object is produced that contains:
 * <UL>
 * <LI> {@code request} - the original input tuple that lead to the request  </LI>
 * <LI> {@code response} - JSON object containing information about the response
 * <UL>
 *    <LI> {@code status} - Status code for the response as an integer</LI>
 *    <LI> {@code entity} - JSON response entity if one exists </LI>
 * </UL>
 * </LI>
 * </UL>
 * 
 * @return Function that will process the {@code application/json} responses.
 */
public static BiFunction<JsonObject, CloseableHttpResponse, JsonObject> json() {
    return (request, resp) -> {
        JsonObject out = new JsonObject();
        out.add("request", request);
        JsonObject response = new JsonObject();
        out.add("response", response);
        response.addProperty("status", resp.getStatusLine().getStatusCode());
        JsonStreamParser reader;
        try {
            reader = new JsonStreamParser(
                    new InputStreamReader(resp.getEntity().getContent(), StandardCharsets.UTF_8));
            if (reader.hasNext())
                response.add("entity", reader.next());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return out;

    };
}

From source file:org.apache.hadoop.hive.json.JsonSchemaFinder.java

License:Apache License

public static void main(String[] args) throws Exception {
    HiveType result = null;//from www  .j av  a  2 s .co m
    int count = 0;
    for (String filename : args) {
        System.out.println("Reading " + filename);
        System.out.flush();
        java.io.Reader reader;
        if (filename.endsWith(".gz")) {
            reader = new InputStreamReader(new GZIPInputStream(new FileInputStream(filename)));
        } else {
            reader = new FileReader(filename);
        }
        JsonStreamParser parser = new JsonStreamParser(reader);
        while (parser.hasNext()) {
            count += 1;
            JsonElement item = parser.next();
            HiveType type = pickType(item);
            result = mergeType(result, type);
        }
    }
    System.out.println(count + " records read");
    System.out.println();
    printTopType(System.out, (StructType) result);
}

From source file:org.apache.hadoop.hive.json.JsonShredder.java

License:Apache License

public static void main(String[] args) throws Exception {
    int count = 0;
    JsonShredder shredder = new JsonShredder();
    for (String filename : args) {
        System.out.println("Reading " + filename);
        System.out.flush();//from w  w w.j  a v  a 2 s  . com
        java.io.Reader reader;
        if (filename.endsWith(".gz")) {
            reader = new InputStreamReader(new GZIPInputStream(new FileInputStream(filename)));
        } else {
            reader = new FileReader(filename);
        }
        JsonStreamParser parser = new JsonStreamParser(reader);
        while (parser.hasNext()) {
            count += 1;
            JsonElement item = parser.next();
            shredder.shredObject("root", item);
        }
    }
    shredder.close();
    System.out.println(count + " records read");
    System.out.println();
}

From source file:org.apache.orc.tools.json.JsonSchemaFinder.java

License:Apache License

public void addFile(String filename) throws IOException {
    java.io.Reader reader;/*from   w ww  . ja v  a  2  s  .  co m*/
    FileInputStream inputStream = new FileInputStream(filename);
    if (filename.endsWith(".gz")) {
        reader = new InputStreamReader(new GZIPInputStream(inputStream), StandardCharsets.UTF_8);
    } else {
        reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
    }
    JsonStreamParser parser = new JsonStreamParser(reader);
    while (parser.hasNext()) {
        records += 1;
        mergedType = mergeType(mergedType, pickType(parser.next()));
    }
}