Skip to content

Latest commit

 

History

History
162 lines (133 loc) · 4.8 KB

File metadata and controls

162 lines (133 loc) · 4.8 KB

JSON support in Pharo

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

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.

Parse JSON

  • 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 )"

Generate JSON

  • 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

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).

Install

To install NeoJSON, simply execute the following code snippet in a playground:

Metacello new
	repository: 'github://svenvc/NeoJSON/repository';
	baseline: 'NeoJSON';
	load

Parse JSON

  • 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 )"

Generate JSON

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 ]

STONJSON v.s. NeoJSON

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

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.