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

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

Introduction

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

Prototype

public JsonWriter jsonValue(String value) throws IOException 

Source Link

Document

Writes value directly to the writer without quoting or escaping.

Usage

From source file:com.cinchapi.concourse.importer.util.Importables.java

License:Apache License

/**
 * Intelligently write the appropriate JSON representation for {@code value}
 * to {@code out}./*ww  w.j  a v a 2  s  . c o  m*/
 * 
 * @param out the {@link JsonWriter} to use for writing
 * @param value the value to write
 * @throws IOException
 */
@VisibleForTesting
protected static void writeJsonValue(JsonWriter out, String value) throws IOException {
    Object parsed;
    if ((parsed = Strings.tryParseNumberStrict(value)) != null) {
        out.value((Number) parsed);
    } else if ((parsed = Strings.tryParseBoolean(value)) != null) {
        out.value((boolean) parsed);
    } else {
        value = Strings.ensureWithinQuotes(value);
        value = Strings.escapeInner(value, value.charAt(0), '\n');
        out.jsonValue(value);
    }
}

From source file:org.diorite.config.serialization.JsonStringSerializerImpl.java

License:Open Source License

@SuppressWarnings("resource")
@Override/* w  w w .  j a v a 2 s  .c o m*/
public void write(JsonWriter jsonWriter, T t) throws IOException {
    jsonWriter.jsonValue(this.stringSerializer.serialize(t));
}

From source file:org.lanternpowered.server.data.persistence.json.JsonDataFormat.java

License:MIT License

public static void write(JsonWriter writer, @Nullable Object value) throws IOException {
    if (value == null) {
        writer.nullValue();//from  ww  w . jav  a  2  s .  co m
    } else if (value instanceof Boolean) {
        writer.value((Boolean) value);
    } else if (value instanceof Number) {
        if (value instanceof Double) {
            String dbl = Double.toString((Double) value);
            if (dbl.indexOf('.') == -1) {
                dbl += DOUBLE_SUFFIX_UNTYPED;
            }
            // Writes a raw json value, without quotes
            writer.jsonValue(dbl);
        } else if (value instanceof Float) {
            writer.value(value + FLOAT_SUFFIX);
        } else if (value instanceof Long) {
            writer.value(value + LONG_SUFFIX);
        } else if (value instanceof Byte) {
            writer.value(value + BYTE_SUFFIX);
        } else if (value instanceof Short) {
            writer.value(value + SHORT_SUFFIX);
        } else {
            writer.value((Number) value);
        }
    } else if (value instanceof String) {
        writer.value((String) value);
    } else if (value instanceof Iterable) {
        writeArray(writer, (Iterable<?>) value);
    } else if (value instanceof Map) {
        writeMap(writer, (Map<?, ?>) value);
    } else if (value instanceof DataSerializable) {
        writeView(writer, ((DataSerializable) value).toContainer());
    } else if (value instanceof DataView) {
        writeView(writer, (DataView) value);
    } else {
        throw new IllegalArgumentException("Unable to translate object to JSON: " + value);
    }
}