Example usage for com.google.gson JsonStreamParser JsonStreamParser

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

Introduction

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

Prototype

public JsonStreamParser(Reader reader) 

Source Link

Usage

From source file:net.motel.json.parser.JsonParser.java

public List<Motel> getMotels() {
    List<Motel> motels = null;

    Reader reader = new InputStreamReader(getDummyJsonData());

    JsonStreamParser parser = new JsonStreamParser(reader);

    //Puts the JsonArray into a JsonElement Stream
    Stream<JsonElement> jsonStream = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(parser, Spliterator.ORDERED), false);

    motels = jsonStream.map((arr) -> { //map the JsonElement to a JsonArray
        return arr.getAsJsonArray();
    }).flatMap((smallArr) -> { // map the contents of the JsonArray into a Stream
        return StreamSupport.stream(smallArr.spliterator(), false);
    }).map((element) -> { //Map array contents to a Motel class
        return gson.fromJson(element, Motel.class);
    }).collect(toList()); //Collect all mapped Motel classes into a list.

    return motels;
}

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(".");
        }/*from   w  ww. j  a  v  a2s.c om*/
        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;//from   w w w  .  j  a  va  2  s  .c  o  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();//from  ww  w  .  j  a  v  a2  s  .  co  m
        emit(r);
        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 w  w  w .ja va2 s  .  c o  m
 * 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  w w w. ja  v a  2  s.  c  om
    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  . ja  va2  s .c om*/
        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.bench.convert.json.JsonReader.java

License:Apache License

public JsonReader(Path path, TypeDescription schema, Configuration conf, CompressionKind compressionKind)
        throws IOException {
    this.schema = schema;
    FileSystem fs = path.getFileSystem(conf);
    InputStream input = compressionKind.read(fs.open(path));
    parser = new JsonStreamParser(new InputStreamReader(input, StandardCharsets.UTF_8));
    if (schema.getCategory() != TypeDescription.Category.STRUCT) {
        throw new IllegalArgumentException("Root must be struct - " + schema);
    }/*  w w  w . ja va 2 s  .  co  m*/
    List<TypeDescription> fieldTypes = schema.getChildren();
    converters = new JsonConverter[fieldTypes.size()];
    for (int c = 0; c < converters.length; ++c) {
        converters[c] = createConverter(fieldTypes.get(c));
    }
}

From source file:org.apache.orc.tools.convert.JsonReader.java

License:Apache License

public JsonReader(Path path, TypeDescription schema, Configuration conf) throws IOException {
    this.schema = schema;
    FileSystem fs = path.getFileSystem(conf);
    totalSize = fs.getFileStatus(path).getLen();
    rawStream = fs.open(path);/*from   ww  w.j a v  a2  s.  c  o  m*/
    String name = path.getName();
    int lastDot = name.lastIndexOf(".");
    InputStream input = rawStream;
    if (lastDot >= 0) {
        if (".gz".equals(name.substring(lastDot))) {
            input = new GZIPInputStream(rawStream);
        }
    }
    parser = new JsonStreamParser(new InputStreamReader(input, StandardCharsets.UTF_8));
    if (schema.getCategory() != TypeDescription.Category.STRUCT) {
        throw new IllegalArgumentException("Root must be struct - " + schema);
    }
    List<TypeDescription> fieldTypes = schema.getChildren();
    converters = new JsonConverter[fieldTypes.size()];
    for (int c = 0; c < converters.length; ++c) {
        converters[c] = createConverter(fieldTypes.get(c));
    }
}

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  ww  w .  j  a va2s.c  om*/
    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()));
    }
}