Skip to content

Commit

Permalink
Some more neojson
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed Jun 18, 2015
1 parent 73609e5 commit 3742b47
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions NeoJSON/NeoJSON.pier
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ While reading:
-JSON maps become instances of mapClass, ==Dictionary== by default;
-JSON lists become instances of listClass, ==Array== by default.

This example creates a Pharo dictionary (with ==x== and ==y== keys):

[[[language=smalltalk
NeoJSONReader fromString: ' { "x" : 1, "y" : 2 } '.
]]]

The following example creates a Pharo array from a JSON expression:

[[[language=smalltalk
Expand All @@ -99,31 +93,47 @@ This expression can be decomposed to better control the reading process:
next.
]]]

The above expression is equivalent to the previous one except that an ordered collection will be used in place of an array.
The above expression is equivalent to the previous one except that a Pharo ordered collection will be used in place of an array.

There is also an option to convert all map keys to symbols, which is off by default.

@@authorToDo DC: example of how to specify options
The next example creates a Pharo dictionary (with =='x'== and =='y'== keys):

[[[language=smalltalk
NeoJSONReader fromString: ' { "x" : 1, "y" : 2 } '.
]]]

To automatically convert keys to symbols, pass ==true== to ==propertyNamesAsSymbols:== like this:

[[[language=smalltalk
(NeoJSONReader on: ' { "x" : 1, "y" : 2 } ' readStream)
propertyNamesAsSymbols: true;
next
]]]

The result of this expression is a dictionary with ==#x== and ==#y== as keys.

!!!Writing to JSON

While writing:

-Dictionary and SmallDictionary become maps
-all other Collection classes become lists
-all other Objects are rejected
-instances of ==Dictionary== and ==SmallDictionary== become maps;
-all other collections become lists;
-all other non-primitive objects are rejected.

Here are some examples writing in generic mode:

[[[
[[[language=smalltalk
NeoJSONWriter toString: #(1 2 3).

NeoJSONWriter toString: { Float pi. true. false. 'string' }.
NeoJSONWriter toString: { #a -> '1' . #b -> '2' } asDictionary.
]]]

Above expressions return a compact string (/i.e./, with neither indentation nor new lines). To get a nicely formatted output, use ==toStringPretty:== like this:

NeoJSONWriter toStringPretty: (Dictionary new at: #x put: 1; at: #y put: 2; yourself).
[[[language=smalltalk
NeoJSONWriter toStringPretty: #(1 2 3).
]]]

NeoJSONWriter can output either in a compact format (the default) or in a pretty printed format.

SD: examples how to do that.
Expand Down

0 comments on commit 3742b47

Please sign in to comment.