I want to save an Adered ( on-line IDE ) OrderedCollection in a local web browser repository and get it later.
Creating a 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.
Saving the test data object to localStorage
localStorage is the keystore in the browser. Values ββmust be strings.
localStorage setItem: 'coll' value: coll asJSONString. "We set coll to nil to indicate that we are going to retrieve it back from the localStorage" coll := nil.
Return stored value
a printIt next
localStorage getItem: 'coll'
gives
'["abc","xon",{"en":"English","fr":"French","ge":"German"}]'
This is a JSON string.
How do I return an OrderedCollection column?
Use browser parser JSON
JSON parse: (localStorage getItem: 'coll')
PrintIt result
an Array ('abc' 'xon' [object Object])
and
(JSON parse: (localStorage getItem: 'coll')) class
is an
Array
The third element of the array
((JSON parse: (localStorage getItem: 'coll')) at: 3) class
is an
JSObjectProxy
Question
How do I get a Smalltalk view for an arbitrary JSON object (containing JavaScript arrays and objects, ordered characters and hashes, dictionaries in Smalltalk)?
Note
http://www.json.org
JSON is built on two structures:
- A collection of name / value pairs. In different languages, this is implemented as an object, a dictionary, a hash table, or an associative array.
- An ordered list of values. In many languages, this is implemented as an array, list, or sequence.
source share