/** * Creates a {@link JsonWriter} use {@link StringBuilder} as output. */ public static JsonWriter createWriter(JsonSettings settings) { return writer().setSettings(settings).create(); }
default JsonWriterCreator maxIgnore() { setIgnoreNull(true); setIgnoreEmptyString(true); setIgnoreEmptyArray(true); return setIgnoreFalse(true); }
protected JsonWriter createJsonWriter(Appendable out) { JsonWriterCreator writer = JSON.writer(out); if(null != propertyNamingStyle) { writer.setNamingStyle(propertyNamingStyle); } return writer.create(); }
default void toJson(Appendable out) { toJson(JSON.writer(out).setIgnoreNull(true).create()); }
@Override protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try (OutputStream os = outputMessage.getBody()){ try (OutputStreamWriter writer = new OutputStreamWriter(os, getCharset(outputMessage))) { JsonWriter jsonWriter = JSON.writer(writer).create(); if(o == null) { jsonWriter.null_(); return; } if(o.getClass().isArray()) { Object[] a = (Object[])o; jsonWriter.startArray(); for(Object item : a) { ((JsonStringable)item).toJson(jsonWriter); } jsonWriter.endArray(); }else { ((JsonStringable)o).toJson(jsonWriter); } } } }
@Override public void write(Object o, Type type, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { if(type instanceof Class) { writeInternal(o, outputMessage); }else { try (OutputStream os = outputMessage.getBody()){ try (OutputStreamWriter writer = new OutputStreamWriter(os, getCharset(outputMessage))) { JsonWriter jsonWriter = JSON.writer(writer).create(); if(null == o) { jsonWriter.startArray().endArray(); return; } Collection c = (Collection)o; jsonWriter.array(c, item -> { ((JsonStringable)item).toJson(jsonWriter); }); } } } }
/** * Creates a {@link JsonWriter} use the given {@link Appendable} as output. */ public static JsonWriter createWriter(Appendable out, JsonSettings settings) { return writer(out).setSettings(settings).create(); }
default void toJson(Appendable out, JsonSettings settings){ if(settings == null){ toJson(out); return; } JsonWriter writer = JSON.writer(out) .setSettings(settings) .create(); toJson(writer); }