java.lang.Object
com.darkcollective.relix.json.JsonWriter
A minimal, dependency-free streaming writer for JSON text.
The writer is a fluent builder. Callers open and close containers with
beginObject()/endObject() and
beginArray()/endArray(), label object members with
name(String), and emit scalars with the value overloads.
Output is compact (no insignificant whitespace) and every string — both names
and string values — is escaped per the JSON specification.
Structural guarantees
The writer fails fast with anIllegalStateException on misuse, so a
buggy serializer cannot silently produce malformed JSON:
name(String)is only valid inside an object, and must precede each member value (no two names in a row, no name without a value).- A value inside an object must be preceded by a
name(String). endObject()/endArray()must match the currently open container, and an object may not be closed with a dangling name.- At most one value may be written at the top level.
Example
String json = new JsonWriter()
.beginObject()
.name("op").value("Projection")
.name("schema").beginArray()
.beginObject().name("name").value("id").name("type").value("N").endObject()
.endArray()
.name("children").beginArray().endArray()
.endObject()
.toJson();
// {"op":"Projection","schema":[{"name":"id","type":"N"}],"children":[]}
Instances are not thread-safe and are intended for single-pass use.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionBegins a JSON array ([]).Begins a JSON object ({}).endArray()Ends the current JSON array.Ends the current JSON object.Writes an object member name.Writes an explicit JSONnullvalue.toJson()Returns the completed JSON document.toString()Returns the JSON written so far, without checking for completeness.value(boolean value) Writes a boolean value.value(double value) Writes a floating-point number value.value(long value) Writes an integral number value.Writes a string value, or JSONnullifvalueis null.
-
Constructor Details
-
JsonWriter
public JsonWriter()Creates an empty writer.
-
-
Method Details
-
beginObject
Begins a JSON object ({}).- Returns:
- this writer, for chaining
- Throws:
IllegalStateException- if a value is not permitted at this position
-
endObject
Ends the current JSON object.- Returns:
- this writer, for chaining
- Throws:
IllegalStateException- if no object is open, or a name was written without a following value
-
beginArray
Begins a JSON array ([]).- Returns:
- this writer, for chaining
- Throws:
IllegalStateException- if a value is not permitted at this position
-
endArray
Ends the current JSON array.- Returns:
- this writer, for chaining
- Throws:
IllegalStateException- if no array is open
-
name
Writes an object member name. Must be followed by exactly one value.- Parameters:
name- the member name; must not be null- Returns:
- this writer, for chaining
- Throws:
IllegalStateException- if not currently inside an object, or a name was already written without a value
-
value
Writes a string value, or JSONnullifvalueis null.- Parameters:
value- the string, or null for JSON null- Returns:
- this writer, for chaining
-
value
Writes an integral number value.- Parameters:
value- the number- Returns:
- this writer, for chaining
-
value
Writes a floating-point number value.- Parameters:
value- the number; must be finite- Returns:
- this writer, for chaining
- Throws:
IllegalArgumentException- ifvalueis NaN or infinite (JSON has no representation for these)
-
value
Writes a boolean value.- Parameters:
value- the boolean- Returns:
- this writer, for chaining
-
nullValue
Writes an explicit JSONnullvalue.- Returns:
- this writer, for chaining
-
toJson
Returns the completed JSON document.- Returns:
- the JSON text
- Throws:
IllegalStateException- if any object/array is still open, or no value has been written
-
toString
Returns the JSON written so far, without checking for completeness. Useful for diagnostics; prefertoJson()for finished output.
-