json - Amber and localstorage, asJSON? -
i want store amber (on-line ide) orderedcollection in localstorage of web browser , later retrieve it.
creating test data object
| coll hcoll | coll := orderedcollection new. coll add: 'abc'. coll add: 'xon'. hcoll := hashedcollection new. hcoll at: 'en' put: 'english'. hcoll at: 'fr' put: 'french'. hcoll at: 'ge' put: 'german'. coll add: hcoll.
storing test data object in localstorage
localstorage key-value store in browser. values have strings.
localstorage setitem: 'coll' value: coll asjsonstring. "we set coll nil indicate going retrieve localstorage" coll := nil.
getting stored value
a printit of following
localstorage getitem: 'coll'
gives
'["abc","xon",{"en":"english","fr":"french","ge":"german"}]'
this json string.
how orderedcollection coll?
use json parser built browser
json parse: (localstorage getitem: 'coll')
the result of printit is
an array ('abc' 'xon' [object object])
and
(json parse: (localstorage getitem: 'coll')) class
is
array
the third element of array
((json parse: (localstorage getitem: 'coll')) at: 3) class
is a
jsobjectproxy
question
how smalltalk representation arbitrary json object (containing javascript arrays , objects, orderedcollections , hashedcollections, dictionaries in smalltalk)?
note
json built on 2 structures:
- a collection of name/value pairs. in various languages, realized object, dictionary, hash table, or associative array.
- an ordered list of values. in many languages, realized array, list, or sequence.
a printit of
smalltalkimage current readjsobject: (json parse: (localstorage getitem: 'coll'))
gives back
array ('abc' 'xon' dictionary ('en' -> 'english' , 'fr' -> 'french' , 'ge' -> 'german'))
comment
(json parse: (localstorage getitem: 'coll'))
gives jsproxyobject converted amber objects method #readjsobject: . method redirects call underlying javascript method.
Comments
Post a Comment