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

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

Introduction

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

Prototype

public final void setLenient(boolean lenient) 

Source Link

Document

Configure this parser to be liberal in what it accepts.

Usage

From source file:net.malisis.ddb.json.BlockPackJsonReader.java

License:Open Source License

public static BlockPack readPack(File file) {
    BlockPack.Type type;/*from w w w.  ja v  a 2s . co  m*/
    String name;
    ZipFile zipFile = null;

    if (file.isDirectory()) {
        name = file.getName();
        type = BlockPack.Type.FOLDER;
    } else if (file.getName().endsWith(".zip")) {
        name = file.getName().substring(0, file.getName().length() - 4);
        type = BlockPack.Type.ZIP;
        try {
            zipFile = new ZipFile(file);
        } catch (IOException e) {
            DDB.log.error("Could not read zip file {} :\n", file.getName(), e);
        }
    } else {
        DDB.log.error("Skipping {}, not a valid DDB pack file.", file.getName());
        return null;
    }

    BlockPack pack = new BlockPack(type, name, zipFile);

    InputStream inputStream;
    try {
        inputStream = pack.getInputStream(name + ".json");
    } catch (IOException e) {
        DDB.log.error("Skipping {}, couldn't read {}.json : {}", file.getName(), name, e.getMessage());
        return null;
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(BlockPack.class, new BlockPackDeserializer(pack));
    //gsonBuilder.registerTypeAdapter(DDBRecipe.class, new DDBRecipe.DDBRecipeDeserializer());
    Gson gson = gsonBuilder.create();

    try (Reader reader = new InputStreamReader(inputStream, "UTF-8")) {
        JsonReader jsonReader = new JsonReader(reader);
        jsonReader.setLenient(true);
        gson.fromJson(jsonReader, BlockPack.class);
    } catch (IOException | JsonSyntaxException e) {
        DDB.log.error("Failed to read {}.json : {}", name, e.getMessage());
        return null;
    }

    return pack;
}

From source file:net.sf.javaocr.demos.android.recognizer.Recognizer.java

License:Apache License

/**
 * read cluster data  from storage//ww  w.  j a va2  s.  co  m
 */
private void readClusterData() {
    Log.d(LOG_TAG, "start reading cluster data");
    try {
        InputStream inputStream = getResources().openRawResource(R.raw.freespaces);
        InputStreamReader reader = new InputStreamReader(inputStream);
        JsonReader jreader = new JsonReader(reader);
        jreader.setLenient(true);

        freeSpacesMatcher.setContainers(JSONUnmarshaller.unmarshallArray(jreader, FreeSpacesContainer.class));

        reader.close();

        inputStream = getResources().openRawResource(R.raw.moments);
        reader = new InputStreamReader(inputStream);
        jreader = new JsonReader(reader);
        jreader.setLenient(true);

        metricMatcher
                .setContainers(JSONUnmarshaller.unmarshallArray(jreader, MahalanobisClusterContainer.class));

    } catch (Exception e) {
        e.printStackTrace();
    }
    loadReady = true;
}

From source file:org.apache.axis2.json.gson.rpc.JsonUtils.java

License:Apache License

public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation,
        Class[] paramClasses, int paramCount)
        throws InvocationTargetException, IllegalAccessException, IOException {

    Object[] methodParam = new Object[paramCount];
    Gson gson = new Gson();
    String[] argNames = new String[paramCount];

    if (!jsonReader.isLenient()) {
        jsonReader.setLenient(true);
    }//from ww  w.j av  a  2  s. c  o  m
    jsonReader.beginObject();
    jsonReader.nextName(); // get message name from input json stream
    jsonReader.beginArray();

    int i = 0;
    for (Class paramType : paramClasses) {
        jsonReader.beginObject();
        argNames[i] = jsonReader.nextName();
        methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it
        jsonReader.endObject();
        i++;
    }

    jsonReader.endArray();
    jsonReader.endObject();

    return operation.invoke(service, methodParam);

}

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

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<ContentSummary>() {
        @Override/*from  www  . java  2s.co 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//from   www. j a  va  2 s .  c  om
        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.zeppelin.helium.HeliumLocalRegistry.java

License:Apache License

private HeliumPackage readPackageInfo(File f) {
    try {/*w  w  w  .ja  v  a 2  s . c o m*/
        JsonReader reader = new JsonReader(new StringReader(FileUtils.readFileToString(f)));
        reader.setLenient(true);

        return gson.fromJson(reader, HeliumPackage.class);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

From source file:org.apache.zeppelin.notebook.Notebook.java

License:Apache License

/**
 * import JSON as a new note./*from  w w  w.  j  av  a  2  s. c  o m*/
 *
 * @param sourceJson - the note JSON to import
 * @param noteName   - the name of the new note
 * @return notebook ID
 * @throws IOException
 */
public Note importNote(String sourceJson, String noteName, AuthenticationInfo subject) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();

    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    JsonReader reader = new JsonReader(new StringReader(sourceJson));
    reader.setLenient(true);
    Note newNote;
    try {
        Note oldNote = gson.fromJson(reader, Note.class);
        newNote = createNote(subject);
        if (noteName != null)
            newNote.setName(noteName);
        else
            newNote.setName(oldNote.getName());
        List<Paragraph> paragraphs = oldNote.getParagraphs();
        for (Paragraph p : paragraphs) {
            newNote.addCloneParagraph(p);
        }

        newNote.persist(subject);
    } catch (IOException e) {
        logger.error(e.toString(), e);
        throw e;
    }

    return newNote;
}

From source file:org.bimserver.collada.OpenGLTransmissionFormatSerializer.java

License:Open Source License

private JsonElement readFileAsJSON(File expectedOutputFile) {
    String result;/* www. j  a v a  2s.  co  m*/
    try {
        result = FileUtils.readFileToString(expectedOutputFile);
    } catch (IOException e) {
        // Something bad happened, but send something readable to the next step anyway.
        result = "{}";
        e.printStackTrace();
    }
    // Parse the existing file.
    JsonParser parser = new JsonParser();
    JsonReader reader = new JsonReader(new StringReader(result));
    reader.setLenient(true);
    JsonElement root = parser.parse(reader);
    // Send root back.
    return root;
}

From source file:org.commoncrawl.util.EC2MetadataTransferUtil.java

License:Open Source License

public static void main(String[] args) throws IOException {
    final String bucket = args[0];
    String paths = args[1];/*from w ww  .java2s  . c om*/

    System.out.println("Paths are:" + paths);
    JsonParser parser = new JsonParser();
    JsonReader reader = new JsonReader(new StringReader(paths));
    reader.setLenient(true);
    final JsonArray array = parser.parse(reader).getAsJsonArray();
    int pathCount = array.size();

    EC2MetadataTransferUtil util = new EC2MetadataTransferUtil(bucket, array);
}

From source file:org.commoncrawl.util.S3BulkTransferUtil.java

License:Open Source License

public static void main(String[] args) throws IOException {

    CommandLineParser parser = new GnuParser();

    try {/*from w w w.  ja va 2 s . c  om*/
        // parse the command line arguments
        CommandLine cmdLine = parser.parse(options, args);

        String s3AccessKey = cmdLine.getOptionValue("awsKey");
        String s3Secret = cmdLine.getOptionValue("awsSecret");
        String s3Bucket = cmdLine.getOptionValue("bucket");
        Path hdfsOutputPath = new Path(cmdLine.getOptionValue("outputPath"));
        JsonArray paths = new JsonArray();

        if (cmdLine.hasOption("path")) {
            String values[] = cmdLine.getOptionValues("path");
            for (String value : values) {
                paths.add(new JsonPrimitive(value));
            }
        }
        if (cmdLine.hasOption("paths")) {
            JsonParser jsonParser = new JsonParser();
            JsonReader reader = new JsonReader(new StringReader(cmdLine.getOptionValue("paths")));
            reader.setLenient(true);
            JsonArray array = jsonParser.parse(reader).getAsJsonArray();
            if (array != null) {
                paths.addAll(array);
            }
        }

        if (paths.size() == 0) {
            throw new IOException("No Input Paths Specified!");
        }

        LOG.info("Bucket:" + s3Bucket + " Target Paths:" + paths.toString());

        S3BulkTransferUtil util = new S3BulkTransferUtil(s3Bucket, s3AccessKey, s3Secret, paths,
                hdfsOutputPath);
    } catch (Exception e) {
        LOG.error(CCStringUtils.stringifyException(e));
        printUsage();
    }
}