Merge: Nit objects to plain JSON
authorJean Privat <jean@pryen.org>
Sat, 23 May 2015 01:01:11 +0000 (21:01 -0400)
committerJean Privat <jean@pryen.org>
Sat, 23 May 2015 01:01:11 +0000 (21:01 -0400)
commita05897f89603d0dc2880aa287b966bb6e0f4a39f
treee699490c61132ec61cadbfe2e4b684885bb48476
parentaa935cdfd61e7d1efec06a8fb55ed8752d4d776b
parente2f01b3a764ed03f499f6424180ae7340700de2c
Merge: Nit objects to plain JSON

Adds an option to write Nit objects to plain JSON in `JsonSerializer`, and the shortcut `Serializable::to_plain_json`. This format lacks the meta-data necessary for the full deserialization but it is easier to read for non-Nit programs and humans.

You can see an example in the doc of `json::serialization` and here, taken from the tests. The following class uses "complex" Nit data structures and serialize them to the closest available structure in JSON.

~~~nit
class G
auto_serializable

var hs = new HashSet[Int]
var s = new ArraySet[String]
var hm = new HashMap[String, Int]
var am = new ArrayMap[String, String]

init
do
hs.add -1
hs.add 0
s.add "one"
s.add "two"
hm["one"] = 1
hm["two"] = 2
am["three"] = "3"
am["four"] = "4"
end
end
var g = new G
g.to_plain_json # ...
~~~
~~~json
{"hs": [-1, 0], "s": ["one", "two"], "hm": {"one": 1, "two": 2}, "am": {"three": "3", "four": "4"}}
~~~

Of course, when writing to a file or in a socket, it is better to use `JsonSerializer` as the `to_plain_json` version uses a string.

Pull-Request: #1360
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>