pod
json
Overview
The json API provides basic serialization between Fantom and Javascript Object Notation (JSON). Refer to json.org for more information about JSON.
Type Mapping
JSON types are mapped to Fantom types as follows:
JSON Fantom ------ -------- object Str:Obj? array Obj?[] string Str number Int or Float true Bool false Bool null null
In addition when writing Fantom objects the following is supported:
- Simples are written as a JSON string
- Serializables are written as JSON objects with each non-static, non-transient field being written as a key/val pair
Writing simples or serializables do not "roundtrip". For example if you write a Date
it is written and will be read back as a normal string.
Writing Json
Writing is accomplised via write
which writes a given map to an OutStream
. To write to standard out:
map := ["key":"value", "intKey":123] Json.write(map, Env.cur.out)
You can also use the writeToStr
as a convenience to write to an in-memory string.
Reading Json
Reading JSON is accomplised via read
which takes raw JSON from a stream and produces of the core JSON types. For example:
str := """{"k1":"v1", "k2":3.4159, "k3":[1,2,3], "k4": {"m1":true, "m2":null}}""" Str:Obj? data := Json.read(str.in) data["k1"] => v1