public final class JSONWriter

  1. Object
  2. JSONWriter

Convenience JSON writer to complement JSONParser. Two access modes:

  1. One-shot: JSONWriter.toJson(map) / JSONWriter.toJson(map, writer) – accepts a Map, List, String, Number, Boolean, null, and arbitrarily nested combinations.

  2. Fluent builder: JSONWriter.object().put("name", x).put("values", JSONWriter.array().add("a").add("b")).toJson() – for ad-hoc request bodies where a Map literal 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.ObjectBuilderFluent builder for { "k": v, ... }.
class JSONWriter.ArrayBuilderFluent builder for [ ..., ..., ... ].

Methods

public static String toJson(Object value)Encodes value as JSON and returns the resulting string.
public static void toJson(Object value, Writer writer) throws IOExceptionStreams value as JSON into writer.
public static void toJson(Object value, OutputStream out) throws IOExceptionStreams value as JSON into out using UTF-8 encoding.
public static JSONWriter.ObjectBuilder object()Starts a JSON-object builder.
public static JSONWriter.ArrayBuilder array()Starts a JSON-array builder.

Inherited methods

Method details

toJson

public static String toJson(Object value)
Encodes 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 IOException
Streams value as JSON into writer. The writer is not closed or flushed by this method – the caller owns the writer.

toJson

public static void toJson(Object value, OutputStream out) throws IOException
Streams value as JSON into out using UTF-8 encoding. The stream is flushed but not closed.

object

public static JSONWriter.ObjectBuilder object()
Starts a JSON-object builder. Insertion order is preserved.

array

public static JSONWriter.ArrayBuilder array()
Starts a JSON-array builder.