Currently, Pharo provides two main frameworks to handle the JSON format.
This page briefly present these two frameworks and expose their differences to help users to choose the one fitting their needs.
STONJSON is the built-in JSON parser available in default Pharo images. It is part of the STON package and its development takes place in Pharo's github repository.
- From
String
:
STONJSON fromString: '{ "foo" : 42.0 }' "a Dictionary('foo'->42.0 )"
- From
Stream
:
readStream := '{ "foo" : 42.0 }' readStream.
STONJSON fromStream: readStream "a Dictionary('foo'->42.0 )"
- Read from JSON file:
'/path/to/file.json' asFileReference
readStreamDo: [ :readStream |
STONJSON fromStream: readStream ] "a Dictionary('foo'->42.0 )"
- Let
jsonObject
be defined as:
jsonObject := Dictionary new
at: 'foo' put: 42.0;
yourself.
- To generate a JSON
String
:
STONJSON toString: jsonObject "'{""foo"":42.0}'"
- To generate JSON on a
Stream
:
STONJSON put: jsonObject onStream: writeStream
- To write a JSON file:
'/path/to/file.json' asFileReference
writeStreamDo: [ :writeStream |
STONJSON put: jsonObject onStream: writeStream ]
To pretty print JSON, either use STONJSON>>#toStringPretty:
or STONJSON>>#put:onStreamPretty:
.
NeoJSON is actually maintained by Sven Van Caekenberghe on github. This section shows some quick examples but there is a great documentation made by Sven and a chapter in Enterprise Pharo (chapter 8).
To install NeoJSON, simply execute the following code snippet in a playground:
Metacello new
repository: 'github://svenvc/NeoJSON/repository';
baseline: 'NeoJSON';
load
- From
String
:
NeoJSONReader fromString: '{ "foo" : 42.0 }' "a Dictionary('foo'->42.0 )"
- From
Stream
:
readStream := '{ "foo" : 42.0 }' readStream.
(NeoJSONReader on: readStream) next
- Read from JSON file:
'/path/to/file.json' asFileReference
readStreamDo: [ :readStream |
(NeoJSONReader on: readStream) next ] "a Dictionary('foo'->42.0 )"
Let jsonObject
be defined as:
jsonObject := Dictionary new
at: 'foo' put: 42.0;
yourself.
- To generate a JSON
String
:
NeoJSONWriter toString: jsonObject "'{""foo"":42.0}'"
Pretty:
NeoJSONWriter toStringPretty: jsonObject
- To generate JSON on a
Stream
:
(NeoJSONWriter on: writeStream)
nextPut: jsonObject
Pretty:
(NeoJSONWriter on: writeStream)
prettyPrint: true;
nextPut: jsonObject.
- To write a JSON file:
'/path/to/file.json' asFileReference
writeStreamDo: [ :writeStream |
(NeoJSONWriter on: writeStream)
nextPut: jsonObject ]
Pretty:
'/path/to/file.json' asFileReference
writeStreamDo: [ :writeStream |
(NeoJSONWriter on: writeStream)
prettyPrint: true;
nextPut: jsonObject ]
Property | STONJSON | NeoJSON |
---|---|---|
Parse JSON | ✅ | ✅ |
Generate JSON | ✅ | ✅ |
Pretty-printing | ✅ | ✅ |
Built-in | ✅ | ❌ |
Facilities to map JSON objects to Pharo objects | ❌ | ✅ |
Facilities to query JSON objects | ❌ | ✅ * |
*See this post from Sven on the mailing list to know how to use this feature implemented in
NeoJSONObject
.
JSON Schema allows to describes the structure of JSON objects. It is similar to XML Schema but for JSON.
A yet incomplete (but working in its scope) implementation of JSON Schema is provided by Zweidenker on github.