# Class JSONWriter

java.lang.Object
com.codename1.io.JSONWriter

public final class JSONWriter extends Object

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 Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final class`
  `JSONWriter.ArrayBuilder`
  Fluent builder for `[ ..., ..., ... ]`.
  `static final class`
  `JSONWriter.ObjectBuilder`
  Fluent builder for `{ "k": v, ... }`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static JSONWriter.ArrayBuilder`
  `array()`
  Starts a JSON-array builder.
  `static JSONWriter.ObjectBuilder`
  `object()`
  Starts a JSON-object builder.
  `static String`
  `toJson(Object value)`
  Encodes `value` as JSON and returns the resulting string.
  `static void`
  `toJson(Object value, OutputStream out)`
  Streams `value` as JSON into `out` using UTF-8 encoding.
  `static void`
  `toJson(Object value, java.io.Writer writer)`
  Streams `value` as JSON into `writer`.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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, java.io.Writer writer) throws java.io.IOException
    Streams `value` as JSON into `writer`. The writer is not closed or flushed by this method -- the caller owns the writer.
    Throws:
      `java.io.IOException`

  - ### toJson

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

  - ### 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.