public final class JSONWriter
- Object
- 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 types
class JSONWriter.ObjectBuilder | Fluent builder for { "k": v, ... }. |
class JSONWriter.ArrayBuilder | Fluent builder for [ ..., ..., ... ]. |
Methods
Inherited methods
Method details
toJson
public static String toJson(Object value)value as JSON and returns the resulting string. Accepts
Map, List, String, Number, Boolean, null. Maps with
non-String keys are encoded using String.valueOf(key).toJson
public static void toJson(Object value, Writer writer)
throws IOExceptionvalue as JSON into writer. The writer is not closed
or flushed by this method – the caller owns the writer.Throws
toJson
public static void toJson(Object value, OutputStream out)
throws IOExceptionvalue as JSON into out using UTF-8 encoding. The stream
is flushed but not closed.Throws
object
public static JSONWriter.ObjectBuilder object()array
public static JSONWriter.ArrayBuilder array()