const classafJson::JsonWriter
sys::Obj afJson::JsonWriter
@
Js
(Service) - Writes Fantom objects to JSON, optionally performing pretty printing.
Pretty printing takes more processing than basic printing, but helps debugging.
Note Fantom entities MUST be converted to standard lists and maps BEFORE being written by JsonWriter
.
- convertHook
virtual Obj? convertHook(Obj? val)
A simple override hook to alter values before they are written.
By default this just returns the given value.
- make
new make(Obj? options := null)
Creates a
JsonWriter
with the default pretty printing options.options
may be a map of values or justtrue
to enable pretty printing with defaults. Ifnull
then the default is to NOT pretty print.writer := JsonWriter() writer := JsonWriter(true) writer := JsonWriter(["prettyPrint":true, "indent":" "])
- options
Options used for writing.
Defaults to:
[ "prettyPrint" : false, "indent" : "\t", "maxWidth" : 80, "escapeUnicode" : true, ]
If enabled (default) then
escapeUnicode
will escape all characters over 0x7F using\uXXXX
notation.- writeJson
Str writeJson(Obj? obj, Obj? options := null)
Convenience for serialising the given Fantom object to JSON.
options
may be a map of values or justtrue
to enable pretty printing with defaults. Ifnull
then it defaults to the class default.json := jsonWriter.writeJson(jsonObj) json := jsonWriter.writeJson(jsonObj, true) json := jsonWriter.writeJson(jsonObj, ["prettyPrint":true, "indent":" "])
- writeJsonToStream
OutStream writeJsonToStream(Obj? obj, OutStream out, Obj? options := null)
Write the given object as JSON to this stream. The obj must be one of the following:
null
Bool
Num
Str
Str:Obj?
Obj?[]
options
may be a map of values or justtrue
to enable pretty printing with defaults.jsonWriter.writeJsonToStream(jsonObj, out) jsonWriter.writeJsonToStream(jsonObj, out, true) jsonWriter.writeJsonToStream(jsonObj, out, ["prettyPrint":true, "indent":" "])
The
OutStream
is NOT closed, but is returned.