Class JSONWriter
Convenience JSON writer to complement JSONParser. Two access modes:
-
One-shot:
JSONWriter.toJson(map)/JSONWriter.toJson(map, writer)-- accepts aMap,List,String,Number,Boolean,null, and arbitrarily nested combinations. -
Fluent builder:
JSONWriter.object().put("name", x).put("values", JSONWriter.array().add("a").add("b")).toJson()-- for ad-hoc request bodies where aMapliteral would be noisier than the chain.
Encoded output is strict JSON: no trailing commas, all strings
double-quoted with the standard backslash escapes for ", \, \n,
\r, \t, and control chars < 0x20 emitted as \ + u00xx. No
pretty-printing layer is included; if you need indented output, run
the result through an external formatter at debug time.
For typed mapper-based serialization (DTOs annotated with @Mapped
from the binding framework), use com.codename1.mapping.Mappers#toJson
instead. JSONWriter is for ad-hoc and untyped payloads.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFluent builder for[ ..., ..., ... ].static final classFluent builder for{ "k": v, ... }. -
Method Summary
Modifier and TypeMethodDescriptionstatic JSONWriter.ArrayBuilderarray()Starts a JSON-array builder.static JSONWriter.ObjectBuilderobject()Starts a JSON-object builder.static StringEncodesvalueas JSON and returns the resulting string.static voidtoJson(Object value, OutputStream out) Streamsvalueas JSON intooutusing UTF-8 encoding.static voidStreamsvalueas JSON intowriter.
-
Method Details
-
toJson
-
toJson
Streamsvalueas JSON intowriter. The writer is not closed or flushed by this method -- the caller owns the writer.- Throws:
IOException
-
toJson
Streamsvalueas JSON intooutusing UTF-8 encoding. The stream is flushed but not closed.- Throws:
IOException
-
object
Starts a JSON-object builder. Insertion order is preserved. -
array
Starts a JSON-array builder.
-