/** * Writes an array. */ default JsonWriter arrayJsonable(JsonStringable... array) { for(JsonStringable o : array){ o.toJson(this); } return this; } }
default String toJson() { StringBuilder out = new StringBuilder(); toJson(out); return out.toString(); }
/** * Writes a property, the value will be wrote by the given {@link JsonStringable} object. */ default JsonWriter property(String key, JsonStringable o) { key(key); if(null == o){ null_(); }else{ o.toJson(this); } return this; }
/** * Writes an array property. */ default JsonWriter propertyJsonable(String key, Iterable<? extends JsonStringable> iterable) { key(key).startArray(); if(null != iterable) { Iterator<? extends JsonStringable> iterator = iterable.iterator(); while (iterator.hasNext()){ JsonStringable o = iterator.next(); o.toJson(this); if(iterator.hasNext()){ this.separator(); } } } return endArray(); }
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); } } } }
protected JsonWriter bean(Object bean, JsonType type) { if (null == bean) { return null_(); } else if (bean instanceof JsonStringable) { ((JsonStringable) bean).toJson(this); return this; } else { startObject(); properties(bean, false, type); endObject(); return this; } }
/** * Writes an array property. */ default JsonWriter propertyJsonable(String key, JsonStringable... array) { key(key).startArray(); if(null != array) { int i = 0; for(JsonStringable o : array){ o.toJson(this); if(i < array.length - 1) { this.separator(); } i++; } } return endArray(); }
@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); }); } } } }
return objectArray(v); } else if (v instanceof JsonStringable) { ((JsonStringable) v).toJson(this); return this; } else if (v instanceof Map) {