STON (Smalltalk Object Notation)

repository·master·Indexed 41 days ago

https://github.com/svenvc/ston

A JSON-inspired data interchange format for Smalltalk objects. STON supports symbols, class tags, and circular references, and is a superset of JSON. It provides a high-level API via the STON class for serializing and materializing objects using methods like #toString: and #fromString:, with native support for types such as DateAndTime, Point, and Duration.

Tokens
11K
Snippets
29
Records
59
Agent score
99%

What's inside STON

  1. Overview of STONWritePrettyPrinterReadTests

    master
    The STONWritePrettyPrinterReadTests suite is designed to verify the integrity of the STON (Smalltalk Object Notation) serialization process. Specifically, it tests the round-trip capability of 'pretty printed' serialized data: it ensures that objects serialized using a pretty-printing format can be correctly materialized (read back into Smalltalk objects) without loss of data or structure.
  2. What is Smalltalk Object Notation (STON)?

    master

    Smalltalk Object Notation (STON) is a lightweight, text-based, human-readable data interchange format designed for class-based object-oriented languages like Smalltalk. It is based on JSON but extends it to support domain-level object serialization for persistence or network transport.

    Key differences from JSON include:

    • Symbols: Added as a primitive value type.
    • Class Tags: Object values can be preceded by an optional class tag.
    • References: Supports shared and circular object structures to represent complex object graphs.
    • JSON Compatibility: STON implementations are designed to be backwards compatible with standard JSON.
  3. What is STON (Smalltalk Object Notation)?

    master

    STON is a lightweight, text-based, human-readable data interchange format designed for class-based object-oriented languages like Smalltalk. It is used to serialize domain-level objects for persistence or network transport.

    Key features include:

    • Class Tags: Allows specifying the type of an object (e.g., DoomUser).
    • Object References: Supports arbitrary object graphs.
    • Compact Representations: Uses specialized formats for common value objects like ByteArray (hex strings), URL, and DateAndTime (RFC/ISO forms).
    • Primitive Support: Comprehensive support for numbers, strings, symbols, booleans, and nil.
  4. Use STONJSON as a JSON parser and writer

    master

    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.