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

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

Introduction

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

Prototype

public final void setHtmlSafe(boolean htmlSafe) 

Source Link

Document

Configure this writer to emit JSON that's safe for direct inclusion in HTML and XML documents.

Usage

From source file:com.simiacryptus.mindseye.lang.Layer.java

License:Apache License

/**
 * Write zip.//w  w  w .ja v  a  2s. com
 *
 * @param out       the out
 * @param precision the precision
 */
default void writeZip(@Nonnull ZipOutputStream out, SerialPrecision precision) {
    try {
        @Nonnull
        HashMap<CharSequence, byte[]> resources = new HashMap<>();
        JsonObject json = getJson(resources, precision);
        out.putNextEntry(new ZipEntry("model.json"));
        @Nonnull
        JsonWriter writer = new JsonWriter(new OutputStreamWriter(out));
        writer.setIndent("  ");
        writer.setHtmlSafe(true);
        writer.setSerializeNulls(false);
        new GsonBuilder().setPrettyPrinting().create().toJson(json, writer);
        writer.flush();
        out.closeEntry();
        resources.forEach((name, data) -> {
            try {
                out.putNextEntry(new ZipEntry(String.valueOf(name)));
                IOUtils.write(data, out);
                out.flush();
                out.closeEntry();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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.//from  w w w . j  a va  2 s .  co  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:gson_ext.Encoder.java

License:Apache License

@JRubyMethod(required = 1, optional = 1)
public IRubyObject encode(ThreadContext context, IRubyObject[] args) {
    Ruby ruby = context.getRuntime();/*from  w  w  w  .ja  v  a  2s  .  c  o  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);
    jsonWriter.setLenient(true);/*from   www  . j  av  a 2 s . c  om*/
    jsonWriter.setSerializeNulls(true);
    return jsonWriter;
}

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);
    writer.setLenient(true);/*from w ww  .ja v  a  2s .  c o  m*/
    Streams.write(e, writer);
    writer.flush();
    System.out.println();
}

From source file:org.gradle.api.publish.internal.ModuleMetadataFileGenerator.java

License:Apache License

public void generateTo(PublicationInternal publication, Collection<? extends PublicationInternal> publications,
        Writer writer) throws IOException {
    // Collect a map from component to coordinates. This might be better to move to the component or some publications model
    Map<SoftwareComponent, ComponentData> coordinates = new HashMap<SoftwareComponent, ComponentData>();
    collectCoordinates(publications, coordinates);

    // Collect a map from component to its owning component. This might be better to move to the component or some publications model
    Map<SoftwareComponent, SoftwareComponent> owners = new HashMap<SoftwareComponent, SoftwareComponent>();
    collectOwners(publications, owners);

    // Write the output
    JsonWriter jsonWriter = new JsonWriter(writer);
    jsonWriter.setHtmlSafe(false);
    jsonWriter.setIndent("  ");
    writeComponentWithVariants(publication, publication.getComponent(), coordinates, owners, jsonWriter);
    jsonWriter.flush();//from  w  w w. j a  va  2 s  .  c om
    writer.append('\n');
}

From source file:org.jboss.weld.logging.Json.java

License:Apache License

static void writeJsonElementToFile(JsonElement element, File outputFile) throws IOException {
    try (Writer writer = Files.newBufferedWriter(outputFile.toPath(), Charset.forName("UTF-8"))) {
        JsonWriter jsonWriter = new JsonWriter(writer);
        jsonWriter.setHtmlSafe(true);
        Streams.write(element, jsonWriter);
    }/*from  ww w.ja v  a  2s.c  om*/
}

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

public static String toJSONArray(NVEntity[] nves, boolean indent, boolean printNull, Base64Type b64Type)
        throws IOException {
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(sw);
    writer.setSerializeNulls(true);//from   w  w w  .  ja  v a 2  s  .  co m
    writer.setHtmlSafe(true);

    if (indent)
        writer.setIndent("  ");
    else
        writer.setIndent("");
    writer.beginArray();
    for (NVEntity nve : nves) {
        if (nve != null) {
            toJSON(writer, nve.getClass(), nve, printNull, true, b64Type);
        }
    }
    writer.endArray();
    writer.close();
    return sw.toString();
}

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

public static String toJSON(NVEntity nve, boolean indent, boolean printNull, boolean printClassType,
        Base64Type b64Type) throws IOException {
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(sw);
    writer.setSerializeNulls(true);//from  w  w w  . j a va2  s  . com
    writer.setHtmlSafe(true);

    if (indent)
        writer.setIndent("  ");
    else
        writer.setIndent("");

    toJSON(writer, nve.getClass(), nve, printNull, printClassType, b64Type);

    writer.close();

    return sw.toString();
}

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

public static String toJSONWrapper(String wrapName, NVEntity nve, boolean indent, boolean printNull,
        boolean printClassType, Base64Type b64Type) throws IOException {
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(sw);
    writer.setSerializeNulls(true);//from w w w .ja  va 2s .  c  o  m
    writer.setHtmlSafe(true);

    if (indent)
        writer.setIndent("  ");
    else
        writer.setIndent("");

    writer.beginObject();
    writer.name(wrapName);
    toJSON(writer, nve.getClass(), nve, printNull, printClassType, b64Type);
    writer.endObject();
    writer.close();

    return sw.toString();
}