STONJSON is a class-side facade designed to provide simple JSON parsing and writing capabilities. It is a superset of JSON that maintains backwards compatibility for parsing and can be configured for compatibility while writing.
Key Features
- Superset of JSON: Supports class information (for types other than Arrays and Dictionaries).
- Reference Handling: Designed to handle shared and circular references (though writing is restricted).
- Smalltalk-friendly Syntax: Supports Symbols (
#), single-quoted Strings, and nil instead of null. - Special Types: Native support for
Date, Time, DataAndTime, ByteArray, and Point.
Parsing JSON
To parse a JSON string or stream into Smalltalk objects (typically Array and Dictionary), use:
#fromString:: Parses a string.#fromStream:: Parses from a stream.
Writing JSON
To convert Smalltalk objects into JSON strings or streams, use:
#toString: Converts to a JSON string.#toStringPretty:: Converts to a formatted/pretty-printed JSON string.#put:onStream:: Writes the object to a stream.#put:onStreamPretty:: Writes a formatted/pretty-printed object to a stream.
Important Limitation: When writing, you can only write Array and Dictionary objects. Attempting to write objects with shared or circular references will result in an exception.
"Parsing examples"
STONJSON fromString: '[1,-1,3.141592653589793,true,"JSON"]'.
STONJSON fromString: '{"foo":1,"bar":2,"sub":{"a":true,"b":false},"flags":[1,8,32]}'.
"Writing examples"
STONJSON toString: { 1. -1. Float pi. true. 'JSON' }.
STONJSON toStringPretty: { #foo->1. #bar->2 } asDictionary.