@Override public void toJson(JsonWriter w) { //{ catalog : '', name : '' w.startObject() .propertyOptional("catalog", catalog) .property("name", name); //tables : [ {},{} ] w.propertyJsonable("tables",tables); if(sequences.length > 0){ w.propertyJsonable("sequences",sequences); } w.endObject(); } }
protected void writeDefaultSecurity(WriteContext context, ApiMetadata m, JsonWriter w) { if(null == context.defaultSecurity){ return; } w.property(SECURITY, () -> { w.startArray(); w.startObject(); w.property(context.defaultSecurity, () -> { w.array(m.getPermissions(), (p) -> w.value(p.getValue())); }); w.endObject(); w.endArray(); }); }
@Override public void toJson(JsonWriter writer) { writer.startObject(); writer.property("name", name) .key("columns").array(columnNames); writer.endObject(); }
@Override public void toJson(JsonWriter writer) { writer.startObject(); writer.property("oldSequence", oldSequence) .property("newSequence", newSequence); writer.endObject(); } }
protected void writeModelWithinObject(WriteContext context, ApiMetadata m, JsonWriter w, MApiModel model) { if(!model.hasBaseModel()) { w.property(TYPE, OBJECT); } if(model.isEntity()) { w.property(X_ENTITY, true); } writeExtension(w, model.getExtension()); w.propertyOptional(TITLE, model.getTitle()); w.propertyOptional(SUMMARY, model.getSummary()); w.propertyOptional(DESCRIPTION, model.getDescription()); if(!model.hasBaseModel()) { writeModelProperties(context, m, w, model); }else{ w.property(ALL_OF, () -> { w.startArray(); //item1 : the base model. w.startObject() .property(REF, ref(model.getBaseName())) .endObject(); w.separator(); //item2 : self w.startObject() .property(TYPE, OBJECT); writeModelProperties(context, m, w, model); w.endObject(); w.endArray(); }); } }
@Override public void toJson(JsonWriter writer) { writer.startObject(); writeName(writer); writer.propertyOptional("minValue", minValue) .propertyOptional("maxValue", maxValue) .propertyOptional("increment",increment) .propertyOptional("start",start) .propertyOptional("cache",cache) .propertyOptional("cycle",cycle); writer.endObject(); } }
protected void writeModel(WriteContext context, ApiMetadata m, JsonWriter w, MApiModel model) { w.startObject(); writeModelWithinObject(context, m, w, model); w.endObject(); }
/** * Writes a property optional. That means it will not writes the property if the value is null. */ default JsonWriter propertyOptional(String key, Float v) { if(null != v){ property(key, v); } return this; }
@Override public void toJson(JsonWriter writer) { writer.startObject(); for(SchemaChange change : this){ writer.key(getChangeName(change)); change.toJson(writer); } writer.endObject(); }
protected void writeName(JsonWriter writer) { writer.propertyOptional("catalog", catalog) .propertyOptional("schema", schema) .property("name", name); if(quoted) { writer.property("quoted", true); } } }
/** * 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(); }
/** * Encodes the value to json string. */ public static String encode(Object value){ return createWriter().value(value).toString(); }
@Override public JsonWriter property(String key, Map v) { return key(key).map(v); }
/** * Writes an array use writer function. */ default <T> JsonWriter array(T[] a, Consumer<T> itemWriter) { startArray(); if(null != a) { for(int i=0;i<a.length;i++) { T item = a[i]; if(i > 0) { separator(); } itemWriter.accept(item); } } endArray(); return this; }
@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); }); } } } }
@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); } } } }
return this; } else { return key(key).value(cs.toString()); return this; } else { return key(key).array(a); return this; } else { return key(key).array(it); return this; } else { return key(key).objectArray(v);
/** * Writes empty array : <code>[]</code>. */ default JsonWriter emptyArray() { return startArray().endArray(); }