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

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

Introduction

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

Prototype

public JsonReader(Reader in) 

Source Link

Document

Creates a new instance that reads a JSON-encoded stream from in .

Usage

From source file:com.github.lindenb.gatkui.Json2Xml.java

License:Open Source License

public static void main(String[] args) {
    try {/*from   w w w.  ja  v  a 2s . c o  m*/
        Json2Xml app = new Json2Xml();
        Reader r = null;
        JsonReader jr = null;
        if (args.length == 0) {
            LOG.info("reading JSON from stdin");
            r = new InputStreamReader(System.in);
        } else if (args.length == 1) {
            r = new FileReader(new File(args[0]));
        } else {
            System.err.println("Illegal Number of args");
            System.exit(-1);
        }

        jr = new JsonReader(r);
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
        app.w = xof.createXMLStreamWriter(System.out, "UTF-8");
        app.w.setDefaultNamespace(NS);
        app.w.writeStartDocument("UTF-8", "1.0");

        app.parse(null, jr);
        app.w.writeEndDocument();
        app.w.flush();
        jr.close();
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }

}

From source file:com.github.rustdt.tooling.CargoMessageParser.java

License:Open Source License

public CargoMessageParser(Reader reader) {
    this.reader = assertNotNull(reader);
    this.jsonReader = new JsonReader(reader);
    this.jsonReader.setLenient(true);
}

From source file:com.github.sdnwiselab.sdnwise.configuration.Configurator.java

License:Open Source License

/**
 * Parses a file given in input containing a JSON string and returns the
 * corresponding configurator object described in the file.
 *
 * @param fileName the path to the JSON file
 * @return a configurator object//from  w ww . ja  v a 2 s .c o  m
 */
public static final Configurator load(final InputStream fileName) {
    try {
        return (new Gson()).fromJson(new JsonReader(new InputStreamReader(fileName, "UTF-8")),
                Configurator.class);
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(Configurator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.goide.dlv.JsonReaderEx.java

License:Apache License

@NotNull
public JsonReader asGson() {
    JsonToken nextToken = peek();// ww  w .j  a v  a  2 s .  c  om
    switch (nextToken) {
    case BEGIN_ARRAY:
    case BEGIN_OBJECT:
    case STRING:
    case NUMBER:
    case BOOLEAN:
        break;
    default:
        throw createParseError("Cannot create sub reader, next token " + nextToken + " is not value");
    }

    CharSequence sequence = position > 0 ? in.subSequence(position - 1, in.length()) : in;
    return new JsonReader(new CharSequenceReader(sequence));
}

From source file:com.google.api.client.json.gson.GsonFactory.java

License:Apache License

@Override
public JsonParser createJsonParser(Reader reader) {
    return new GsonParser(this, new JsonReader(reader));
}

From source file:com.google.cloud.bigquery.samples.StreamingSample.java

License:Apache License

/**
 * Command line that demonstrates Bigquery streaming.
 *
 * @param args Command line args, should be empty
 * @throws IOException IOexception/*from w  ww .  j  ava 2s . co m*/
 */
// [START main]
public static void main(final String[] args) throws IOException {
    final Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your project id: ");
    String projectId = scanner.nextLine();
    System.out.println("Enter your dataset id: ");
    String datasetId = scanner.nextLine();
    System.out.println("Enter your table id: ");
    String tableId = scanner.nextLine();
    scanner.close();

    System.out.println("Enter JSON to stream to BigQuery: \n" + "Press End-of-stream (CTRL-D) to stop");

    JsonReader fromCli = new JsonReader(new InputStreamReader(System.in));

    Iterator<TableDataInsertAllResponse> responses = run(projectId, datasetId, tableId, fromCli);

    while (responses.hasNext()) {
        System.out.println(responses.next());
    }

    fromCli.close();
}

From source file:com.google.cloud.tools.libraries.CloudLibraries.java

License:Apache License

@VisibleForTesting
List<CloudLibrary> getLibraries() throws IOException {
    try (InputStream inputStream = CloudLibraries.class.getResourceAsStream(librariesJsonPath)) {
        if (inputStream == null) {
            throw new MissingResourceException("Resource not found when loading libraries", LIBRARIES_JSON,
                    librariesJsonPath);/*ww w. j  a  va2  s  .co  m*/
        }

        InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        JsonReader jsonReader = new JsonReader(reader);
        Type listType = new TypeToken<List<CloudLibrary>>() {
        }.getType();
        return new Gson().fromJson(jsonReader, listType);
    }
}

From source file:com.google.devtools.moe.client.project.ProjectConfig.java

License:Apache License

public static ProjectConfig parse(String configText) throws InvalidProject {
    ProjectConfig config = null;/*from   w ww .j a v  a2  s.c  o  m*/
    if (configText != null) {
        try {
            JsonReader configReader = new JsonReader(new StringReader(configText));
            configReader.setLenient(true);
            JsonElement configJsonElement = new JsonParser().parse(configReader);
            if (configJsonElement != null) {
                // Config files often contain JavaScript idioms like comments, single quoted strings,
                // and trailing commas in lists.
                // Check that the JSON parsed from configText is structurally the same as that
                // produced when it is interpreted by GSON in lax mode.
                String normalConfigText = JsonSanitizer.sanitize(configText);
                JsonElement normalConfigJsonElement = new JsonParser().parse(normalConfigText);
                JsonStructureChecker.requireSimilar(configJsonElement, normalConfigJsonElement);

                Gson gson = GsonModule.provideGson(); // TODO(user): Remove this static reference.
                config = gson.fromJson(configText, ProjectConfig.class);
            }
        } catch (JsonParseException e) {
            throw new InvalidProject(e, "Could not parse MOE config: " + e.getMessage());
        }
    }

    if (config == null) {
        throw new InvalidProject("Could not parse MOE config");
    }
    config.validate();
    return config;
}

From source file:com.google.gdt.googleapi.core.ApiDirectoryListingJsonCodec.java

License:Open Source License

public ApiDirectoryListing toApiDirectoryListing(InputStream in, URL baseURL)
        throws UnsupportedEncodingException {
    ApiDirectoryListing retval = null;/*  ww  w. ja  va  2s  . c  om*/
    try {
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        baseURLThreadLocal.set(baseURL);
        retval = gson.fromJson(reader, ApiDirectoryListing.class);
    } finally {
        baseURLThreadLocal.set(null);
    }
    return retval;
}

From source file:com.google.gdt.googleapi.core.ApiDirectoryListingJsonCodec.java

License:Open Source License

public ApiInfoImpl toApiInfo(InputStream in, URL baseURL) throws UnsupportedEncodingException {
    ApiInfoImpl retval = null;//from   ww w . ja v a  2 s  .  com
    try {
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        baseURLThreadLocal.set(baseURL);
        retval = gson.fromJson(reader, ApiInfoImpl.class);
    } finally {
        baseURLThreadLocal.set(null);
    }
    return retval;
}