eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'JSONReader':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: JSONReader


Inheritance:

   Object
   |
   +--Stream
      |
      +--PeekableStream
         |
         +--JSONReader

Package:
stx:goodies/communication
Category:
Net-Communication-JSON
Version:
rev: 1.18 date: 2018/05/09 17:37:43
user: cg
file: JSONReader.st directory: goodies/communication
module: stx stc-classLibrary: communication

Description:


I read data structures (currently build of OrderedCollection and Dictionary)
from and to JSON (Java Script Object Notation). 
Writing is done with the #toJSON: class-method (note: it will behave badly with circular data structures).
Reading via the #fromJSON: class-method.

Notice: 
    By default (and standard), 
    Date, Time and Timestamps are written as iso8601 strings,
    and read initially as strings (thus, they must be converted explicitely back if required).

    The non-standard (but supported by some browsers) format is 'new Date(...)' which is generated
    optionally, if the useISODateFormat flag is turned off in the JSONPrinter.
    However, we cannot read back this format.
    As per ECMA,
        JSON.stringify({'now': new Date()}) 
    should generate
        {''now'':''2013-10-21T13:28:06.419Z''}
    which is exactly what JSONPrinter now generates by default
        JSONPrinter encode:(Structure with: #now -> (Timestamp now)).

Another Note:
    Can now also encode arbitrary Smalltalk objects; however, the reader will always decode them 
    as dictionaries:
        JSONPrinter encode:(Point x:10 y:20).
        JSONReader decode:(JSONPrinter encode:(Point x:10 y:20)).

Author:
    Public Domain Code from Robin Redeker published in lists.gnu.org
    adapted to ST/X by Claus Gittinger


Class protocol:

encoding & decoding
o  decode: stringOrStream
decode the JSON string (as received from JavaScript) to an object.
Same as fromJSON; added to make this encoder more protocol-conform
to other encoders.

o  encode: anObject
return a JSON string which represents the object (can be sent to JavaScript).
Same as toJSON; added to make this encoder more protocol-conform
to other encoders.

o  fromJSON: stringOrStream
decode the JSON string (as received from JavaScript) to an object.

o  toJSON: anObject
return a JSON string which represents the object (can be sent to JavaScript).

instance creation
o  on: aStream


Instance protocol:

private
o  next
I'm returning the next non-whitespace character

o  nextJSONArray
I decode JSON arrays from self and will return an Array for them.

o  nextJSONDict
I decode JSON objects from self and will return a Dictionary containing all the key/value pairs.

o  nextJSONNumber
I'm extracting a number in JSON format from self and return Integer or Float depending on the input.

o  nextJSONString
I'm extracting a JSON string from self and return it as String.

o  peek
I'm peeking for the next non-whitespace character and will drop all whitespace in front of it

o  stream: aStream

reading
o  nextJSONObject
I decode a json self to a value, which will be one of:
nil, true, false, OrderedCollection, Dictionary, String or Number
(I will return Integer or Float depending on the input).


Examples:


     |o1 s o2|
     o1 := #('hello' 123 nil true false (1 'two' 3.0) -4 -2.123e-5).
     s := JSONReader toJSON:o1.
     o2 := JSONReader fromJSON:s   
     |o1 s o2|
     o1 := Dictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3.0 'four' 'vier').
     s := JSONReader toJSON:o1.
     o2 := JSONReader fromJSON:s   
     |o1 s o2|
     o1 := Dictionary withKeysAndValues:#('one two three' 123).
     s := JSONReader toJSON:o1.
     o2 := JSONReader fromJSON:s   
     |o1 s o2|
     o1 := { Date today . Time now . Timestamp now }.
     s := JSONReader encode:o1.
     o2 := JSONReader decode:s   
     |o1 s o2|
     o1 := { Point x:10 y:20 }.
     s := JSONPrinter new useISODateFormat:false; encode:o1.
     o2 := JSONReader decode:s
     |o1 s o2|
     o1 := { Date today . Time now . Timestamp now }.
     s := JSONPrinter new useISODateFormat:false; encode:o1.
     o2 := JSONReader decode:s -- fails as non-JSON  


ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Mon, 19 Nov 2018 22:08:42 GMT