I've a few questions about using json. I can see that there are corresponding modules in the attached library lib_v1.0.2, but the examples do not use them. Is it possible to give two simple examples: serializing an object into a json string and deserializing json into the same object: for example object's creation via constructor.json jsonString. If possible, in addition to serializing/deserializing ordinary fields: strings, int, bool, explain how to serialize/deserialize & hash maps & lists if object contains them.
Asked
Active
Viewed 110 times
2 Answers
2
Update: the encoder now takes a converter
block which is invoked for objects it doesn't know how to serialize. The converter
is supposed to convert that object into something the encoder understands.
Currently, the JSON library only supports a hardcoded list of types:
encode obj:
if obj is string: encode_string_ obj
else if obj is num: encode_number_ obj
else if identical obj true: encode_true_
else if identical obj false: encode_false_
else if identical obj null: encode_null_
else if obj is Map: encode_map_ obj
else if obj is List: encode_list_ obj
else: throw "INVALID_JSON_OBJECT"
This means that it can't serialize/deserialize objects of other (user-defined) types.
I have filed a feature request here: https://github.com/toitware/public/issues/6

Florian Loitsch
- 7,698
- 25
- 30
0
I haven't yet tested the deserialization of complex json objects, but apparently Toit supports serialization/deserialization of containers, includes nested ones. For example a list:
import encoding.json as json
main :
list := [1,2,"covid-19",25,[11,12,"toit",13,14],{"fortran":[77,90]}]
a1 := list[0]
log("a1->$a1")
a2 := list[1]
log("a2->$a2")
a3 := list[2]
log("a3->$a3")
a4 := list[3]
log("a4->$a4")
a5 := list[4]
log("a5->$a5")
a6 := list[5]
log("a5->$a6")
jsonObjList := json.encode list
log("jsonObjList($list)")
listClone := json.decode jsonObjList
log("listClone->($list)")
aa1 := listClone[0]
log("aa1->$aa1")
aa2 := listClone[1]
log("aa2->$aa2")
aa3 := listClone[2]
log("aa3->$aa3")
aa4 := listClone[3]
log("aa4->$aa4")
aa5 := listClone[4]
log("aa5->$aa5")
aa6 := listClone[5]
log("aa5->$aa6")
jsonObjListText := jsonObjList.to_string
log("jsonObjListText-> $jsonObjListText")
listClone2 := json.decode jsonObjListText.to_byte_array
log("listClone2->($listClone2)")
aaa1 := listClone2[0]
log("aaa1->$aaa1")
aaa2 := listClone2[1]
log("aaa2->$aaa2")
aaa3 := listClone2[2]
log("aaa3->$aaa3")
aaa4 := listClone2[3]
log("aaa4->$aaa4")
aaa5 := listClone2[4]
log("aaa5->$aaa5")
aaa6 := listClone2[5]
log("aaa6->$aaa6")
Output:
michael_k@michaelk:~/toit_apps/json$ toit execute test_json.toit
a1->1
a2->2
a3->covid-19
a4->25
a5->[11, 12, toit, 13, 14]
a5->{fortran: [77, 90]}
jsonObjList([1, 2, covid-19, 25, [11, 12, toit, 13, 14], {fortran: [77, 90]}])
listClone->([1, 2, covid-19, 25, [11, 12, toit, 13, 14], {fortran: [77, 90]}])
aa1->1
aa2->2
aa3->covid-19
aa4->25
aa5->[11, 12, toit, 13, 14]
aa5->{fortran: [77, 90]}
jsonObjListText-> [1,2,"covid-19",25,[11,12,"toit",13,14],{"fortran":[77,90]}]
listClone2->([1, 2, covid-19, 25, [11, 12, toit, 13, 14], {fortran: [77, 90]}])
aaa1->1
aaa2->2
aaa3->covid-19
aaa4->25
aaa5->[11, 12, toit, 13, 14]
aaa6->{fortran: [77, 90]}
michael_k@michaelk:~/toit_apps/json$

Florian Loitsch
- 7,698
- 25
- 30

Michael Kanzieper
- 709
- 1
- 10
- 20