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

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

Introduction

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

Prototype

public final void setLenient(boolean lenient) 

Source Link

Document

Configure this writer to relax its syntax rules.

Usage

From source file:com.github.javaplugs.mybatis.JsonElementTypeHandler.java

License:Open Source License

@Override
public void setNonNullParameter(PreparedStatement ps, int i, JsonElement parameter, JdbcType jdbcType)
        throws SQLException {
    try {//  www .j a v  a 2  s  .  c o  m
        StringWriter sw = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(sw);
        jsonWriter.setLenient(false);
        Streams.write(parameter, jsonWriter);
        ps.setString(i, sw.toString());
    } catch (IOException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

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

License:Apache License

GsonGenerator(GsonFactory factory, JsonWriter writer) {
    this.factory = factory;
    this.writer = writer;
    // lenient to allow top-level values of any type
    writer.setLenient(true);
}

From source file:csv2json.CSV2JSONConverter.java

License:Apache License

/**
 * It takes a CSV file and convert it to JSON. First line of the CSV file is
 * the column header./*www.  j ava2 s .  c o m*/
 * 
 * @param csvInputFile
 *            - CSV input file.
 * @param jsonOutputFile
 *            - Output JSON file. Output directory must be existing and have
 *            write access to it.
 * @throws Exception
 */
public void convert(final String csvInputFile, final String jsonOutputFile) throws Exception {
    if (csvInputFile != null && jsonOutputFile != null) {

        PrintWriter writer = null;
        JsonWriter jsonWriter = null;
        int rowCount = 1;

        try (CSVReader reader = new CSVReader(new FileReader(csvInputFile))) {

            writer = new PrintWriter(jsonOutputFile);
            jsonWriter = new JsonWriter(writer);
            jsonWriter.setIndent("    ");
            jsonWriter.setHtmlSafe(true);
            jsonWriter.setLenient(true);

            boolean readFirstLine = false;
            // To store each line of the file.
            String[] eachLine = reader.readNext();
            // To store column header.
            String[] columnHeader = null;

            jsonWriter.beginArray();
            while (eachLine != null) {

                if (eachLine.length > 0) {
                    if (!readFirstLine) {
                        //
                        columnHeader = eachLine;
                        readFirstLine = true;
                    } else {
                        if (eachLine.length == columnHeader.length) {
                            // Convert each row to JSON object.
                            jsonWriter.beginObject();

                            for (int columnIndex = 0; columnIndex < eachLine.length; columnIndex++) {
                                // converting each column
                                jsonWriter.name(columnHeader[columnIndex]).value(eachLine[columnIndex]);
                                // end of converting each column
                            }

                            jsonWriter.endObject();
                            // End of converting each row.
                        }

                    }
                }
                eachLine = reader.readNext();
            }
            jsonWriter.endArray();

        } catch (FileNotFoundException e) {
            throw new Exception(e.getMessage(), e);
        } finally {

            if (jsonWriter != null) {
                jsonWriter.flush();
                jsonWriter.close();
            }
            if (writer != null) {
                writer.flush();
                writer.close();
            }

        }

    } else {
        throw new IllegalArgumentException("CSV input file, JSON output file name cannot be null.");
    }
}

From source file:de.odysseus.staxon.json.stream.gson.GsonStreamFactory.java

License:Apache License

@Override
public JsonStreamTarget createJsonStreamTarget(Writer writer, boolean pretty) {
    JsonWriter jsonWriter = new JsonWriter(new FilterWriter(writer) {
        @Override//from  w w  w.  ja  v a  2  s. c  o m
        public void close() throws IOException {
            flush(); // avoid closing underlying stream
        }
    });
    jsonWriter.setLenient(false);
    jsonWriter.setIndent(pretty ? "\t" : "");
    return new GsonStreamTarget(jsonWriter);
}

From source file:gson_ext.Encoder.java

License:Apache License

@JRubyMethod(required = 1, optional = 1)
public IRubyObject encode(ThreadContext context, IRubyObject[] args) {
    Ruby ruby = context.getRuntime();//  ww  w . ja va  2 s. co m
    Writer out = null;

    if (args.length < 2 || args[1].isNil()) {
        out = new StringWriter();
    } else {
        IRubyObject io = args[1];
        if ((io instanceof RubyIO) || (io instanceof StringIO)) {
            IRubyObject stream = IOJavaAddons.AnyIO.any_to_outputstream(context, io);
            out = new OutputStreamWriter((OutputStream) stream.toJava(OutputStream.class));
        } else {
            throw ruby.newArgumentError("Unsupported source. This method accepts IO");
        }
    }

    JsonWriter writer = new JsonWriter(out);
    writer.setLenient(this.lenient);
    writer.setHtmlSafe(this.htmlSafe);
    writer.setIndent(this.indent);
    writer.setSerializeNulls(this.serializeNulls);
    try {
        encodeValue(writer, context, args[0]);
    } catch (Exception ex) {
        throw EncodeError.newEncodeError(ruby, ex.getMessage());
    }
    if (out instanceof StringWriter) {
        return ruby.newString(out.toString());
    } else {
        return context.nil;
    }
}

From source file:guru.qas.martini.jmeter.sampler.DefaultMartiniResultMarshaller.java

License:Apache License

protected JsonWriter getJsonWriter(StringWriter writer) throws IOException {
    JsonWriter jsonWriter = gson.newJsonWriter(writer);
    jsonWriter.setHtmlSafe(true);/*from  ww w  . j a va 2  s.c o m*/
    jsonWriter.setLenient(true);
    jsonWriter.setSerializeNulls(true);
    return jsonWriter;
}

From source file:it.polimi.meteocal.ejb.HandleAuthGoogleImpl.java

License:Open Source License

/**
 * Method that updates the google token in the DB using the refresh token
 *
 * @param credential the credential for access the google API
 * @param tokenResponse the http response with the token
 * @param user the user in MeteoCal/*w w w.  j  a  v  a 2  s .  c  o  m*/
 * @see Credential
 * @see TokenResponse
 */
protected static void setGoogleToken(Credential credential, TokenResponse tokenResponse, User user) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("MeteoCalEJB");
    EntityManager em = emf.createEntityManager();
    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        String tokenGoogle = tokenResponse.toString();
        LOGGER.log(Level.INFO, tokenGoogle);
        JsonParser parser = new JsonParser();
        JsonObject newToken = parser.parse(tokenGoogle).getAsJsonObject();
        JsonObject oldToken = parser.parse(user.getGoogleToken()).getAsJsonObject();
        newToken.add("refresh_token", oldToken.get("refresh_token"));
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter);
        jsonWriter.setLenient(true);
        Streams.write(newToken, jsonWriter);
        tokenGoogle = stringWriter.toString();

        LOGGER.log(Level.INFO, tokenGoogle);

        if (tokenGoogle.contains("refresh_token")) {
            LOGGER.log(Level.INFO, "GoogleToken updated");
            user.setGoogleToken(tokenGoogle);
            em.merge(user);
            em.joinTransaction();
            em.flush();
        }
    } catch (GeneralSecurityException | IOException e) {
        LOGGER.log(Level.ERROR, e, e);
    }
    em.close();
    emf.close();
}

From source file:net.oneandone.stool.setup.Transform.java

License:Apache License

public static String toString(JsonObject obj) {
    try {/*from  w  w  w  .j ava 2 s  . c o m*/
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter);
        jsonWriter.setIndent("  ");
        jsonWriter.setLenient(true);
        Streams.write(obj, jsonWriter);
        return stringWriter.toString();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

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

License:Open Source License

public static void prettyPrintJSON(JsonElement e) throws IOException {
    JsonWriter writer = new JsonWriter(new OutputStreamWriter(System.out, "UTF-8"));
    writer.setIndent("    ");
    writer.setHtmlSafe(true);//from w  ww .  java 2s .c o  m
    writer.setLenient(true);
    Streams.write(e, writer);
    writer.flush();
    System.out.println();
}

From source file:org.danbrough.mega.MegaCrypto.java

License:Open Source License

public String toPrettyString(JsonElement o) {

    StringWriter out = new StringWriter();
    JsonWriter writer = new JsonWriter(out);
    writer.setIndent(" ");
    writer.setLenient(true);
    try {//from   w w w . ja va 2  s.  c o m
        Streams.write(o, writer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toString();
}