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

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

Introduction

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

Prototype

public final void setSerializeNulls(boolean serializeNulls) 

Source Link

Document

Sets whether object members are serialized when their value is null.

Usage

From source file:com.flipkart.android.proteus.demo.converter.GsonRequestBodyConverter.java

License:Apache License

@Override
public RequestBody convert(T value) throws IOException {
    TypeAdapter<T> adapter = getAdapter();
    Buffer buffer = new Buffer();
    Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
    JsonWriter jsonWriter = gson.newJsonWriter(writer);
    jsonWriter.setSerializeNulls(true);
    adapter.write(jsonWriter, value);//  w w  w  .ja v  a2 s  . co m
    jsonWriter.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}

From source file:com.getperka.flatpack.Packer.java

License:Apache License

/**
 * Write the given entity into a {@link Writer}.
 * /*from  w ww .  j a v  a2 s .com*/
 * @param entity the entity to write
 * @param out the destination output which will be closed by this method
 */
public void pack(FlatPackEntity<?> entity, Writer out) throws IOException {
    out = ioObserver.observe(out);
    JsonWriter json = new JsonWriter(out);
    json.setSerializeNulls(false);
    if (prettyPrint) {
        json.setIndent("  ");
    }

    packScope.enter().withEntity(entity).withJsonWriter(json);
    try {
        pack(entity);
    } finally {
        packScope.exit();
    }
}

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

License:Apache License

/**
 * Write zip.//from  w  ww.  ja va 2s .  c  o  m
 *
 * @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:com.tsc9526.monalisa.orm.datatable.DataTable.java

License:Open Source License

public String toJson() {
    StringWriter buffer = new StringWriter();
    JsonWriter w = new JsonWriter(buffer);
    w.setSerializeNulls(true);

    JsonHelper.writeJson(w, this, true);

    return buffer.toString();
}

From source file:com.tsc9526.monalisa.orm.datatable.Page.java

License:Open Source License

public String toJson() {
    try {/* w  w w.  j ava 2  s  . co  m*/
        StringWriter buffer = new StringWriter();
        JsonWriter w = new JsonWriter(buffer);
        w.setSerializeNulls(true);
        w.beginObject();

        w.name("pageNo").value(pageNo);
        w.name("pageSize").value(pageSize);
        w.name("totalPage").value(totalPage);
        w.name("totalRow").value(totalRow);

        w.name("list");
        JsonHelper.writeJson(w, list, false);

        w.endObject();
        w.close();

        return buffer.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.tsc9526.monalisa.tools.datatable.DataTable.java

License:Open Source License

public String toJson() {
    StringWriter buffer = new StringWriter();
    JsonWriter w = new JsonWriter(buffer);
    w.setSerializeNulls(true);

    MelpJson.writeJson(w, this, true);

    return buffer.toString();
}

From source file:com.tsc9526.monalisa.tools.datatable.Page.java

License:Open Source License

public String toJson() {
    try {//from   w  ww.  j  a  va 2  s  .c  o m
        StringWriter buffer = new StringWriter();
        JsonWriter w = new JsonWriter(buffer);
        w.setSerializeNulls(true);
        w.beginObject();

        w.name("page").value(page);
        w.name("total").value(total);
        w.name("size").value(size);
        w.name("records").value(records);
        w.name("rows");
        MelpJson.writeJson(w, rows, false);

        w.endObject();
        w.close();

        return buffer.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.TitleTypeAdapterFactory.java

private TypeAdapter<Title> customizeMyClassAdapter(Gson gson, TypeToken<Title> type) {
    final TypeAdapter<Title> delegate = gson.getDelegateAdapter(this, type);
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
    return new TypeAdapter<Title>() {
        @Override/*from   w w w. ja va2  s .  co m*/
        public void write(JsonWriter out, Title value) throws IOException {
            // null for text is significant, else there will be
            // "Chart Title" as default
            if (value != null && value.getText() == null) {
                boolean serializeNulls = out.getSerializeNulls();
                out.setSerializeNulls(true);
                out.beginObject();
                out.name("text");
                out.nullValue();
                out.endObject();
                out.setSerializeNulls(serializeNulls);
            } else {
                elementAdapter.write(out, delegate.toJsonTree(value));
            }
        }

        // This is never used
        @Override
        public Title read(JsonReader in) throws IOException {
            JsonElement tree = elementAdapter.read(in);
            return delegate.fromJsonTree(tree);
        }
    };
}

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 .  java2 s .  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);//from  w  w w.  j  av  a 2 s .co  m
    jsonWriter.setLenient(true);
    jsonWriter.setSerializeNulls(true);
    return jsonWriter;
}