eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'JSONPrinter':

Home

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

Class: JSONPrinter


Inheritance:

   Object
   |
   +--Visitor
      |
      +--AspectVisitor
         |
         +--ObjectCoder
            |
            +--JSONPrinter

Package:
stx:goodies/communication
Category:
Net-Communication-JSON
Version:
rev: 1.33 date: 2019/08/17 22:21:10
user: cg
file: JSONPrinter.st directory: goodies/communication
module: stx stc-classLibrary: communication

Description:


I encode a limited subset of Smalltalk object types into a JSON representation.

Allowed are:
    strings,
    numbers
    booleans,
    nil,
    Arrays and OrderedCollections,
    Dictionaries,
    Date & Time (careful - see >> useISODateFormat)

Everything else is not representable. 
I do not handle recursive data structures.

Notice:
    take a look at the other (alternative) JSON framework (Json),
    which can also be used to handle JSON data, and offers a more flexible
    object mapping mechanism to decode into real objects.
    
Author:
    Claus Gittinger
    Loosely Based upon Public Domain Code from Robin Redeker published in lists.gnu.org


Class protocol:

API - encoding
o  encode: anObject
return a JSON string which represents the object (can be sent to JavaScript).

o  encode: anObject on: aStream
append a compact JSON string which represents the object onto aStream
(can be sent to JavaScript).

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

o  encodePrettyPrinted: anObject on: aStream
append a prettyPrinted (indented) JSON string which represents the object
onto aStream (can be sent to JavaScript).

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

constants
o  typeInfoFormatNone
if set via typeInfoFormat:, slots are encoded in standard JSON (no type info)

o  typeInfoFormatSTX
if set via typeInfoFormat:,
slots are encoded as:
{ '@type': , <classNameString> , 'value': <encoding> }

o  typeInfoFormatSTX2
if set via typeInfoFormat:,
object are store with an additional slot, as:
{ '@type': , <classNameString> ,
...
... regular slots ...
...
}

instance creation
o  new
return an initialized instance


Instance protocol:

accessing
o  skipNil
if true, nil entries are skipped (default is false)

o  useISODateFormat
returns the date format generated.
If true (the default), a string containing the ISO format
is generated for Date, Time and Timestamp.
if false, a non-standard (but supported by some browsers) format
is generated (eg. 'new Date(y,m,d)').

usage example(s):

JSONReader decode:(
         JSONPrinter new 
            encode:{ Date today . Time now . Timestamp now . 10 seconds }
     )    

     JSONPrinter new 
        useISODateFormat:false;
        encode:{ Date today . Time now . Timestamp now . 10 seconds }.  

encoding / decoding
o  encode: anObject

o  encode: anObject on: aStream

initialization
o  initialize
(comment from inherited method)
just to ignore initialize to objects which do not need it

o  makeSlotEncoderFor: typeInfoFormat
JSONTypeEncoder newEncoderFor:nil
JSONTypeEncoder newEncoderFor:#stx

o  prettyPrint: aBoolean
if false (the default), the generated JSON is compact;
if true, lines are indented to be more readable

usage example(s):

     JSONPrinter new 
        encode:( Dictionary new
                    at:'hello' put:'world';
                    at:'foo' put:123;
                    at:'bar' put:(Dictionary new
                                    at:'one' put:1;
                                    at:'two' put:2;
                                    at:'three' put:#('aaa' 'bbb' 'ccc');
                                    yourself);
                    at:'baz' put:999.99;
                    yourself )
        on:Transcript.            

     JSONPrinter new 
        prettyPrint:true;
        encode:( Dictionary new
                    at:'hello' put:'world';
                    at:'foo' put:123;
                    at:'bar' put:(Dictionary new
                                    at:'one' put:1;
                                    at:'two' put:2;
                                    at:'three' put:#('aaa' 'bbb' 'ccc');
                                    yourself);
                    at:'baz' put:999.99;
                    yourself )
        on:Transcript.            

o  skipNil: aBoolean
if true, nil entries are skipped (default is false)

o  typeInfoFormat: formatSymbolOrNil
specify the format of the type info to be store with the slots.
For now, the formats supported are:
nil (default): no type info (standard JSON)
#stx : encode as { '@type': , <classNameString> , 'value': <encoding> }
more formats may be implemented in the future.

usage example(s):

     JSONPrinter new 
        encode:{ Date today . Time now . Timestamp now }.

     JSONPrinter new 
        typeInfoFormat:(JSONPrinter typeInfoFormatSTX);
        encode:{ Date today . Time now . Timestamp now }. 

o  useISODateFormat: aBoolean
if true (the default), a string containing the ISO format
is generated for Date, Time and Timestamp.
if false, a non-standard (but supported by some browsers) format
is generated (eg. 'new Date(y,m,d)').

usage example(s):

     JSONPrinter new 
        encode:{ Date today . Time now . Timestamp now }.

     JSONPrinter new 
        useISODateFormat:false;
        encode:{ Date today . Time now . TimeStamp now }.

visiting
o  visitBoolean: aBoolean with: indent
(comment from inherited method)
visit a Boolean - fallback here is to call visitObject

o  visitDate: aDate with: indent
months start at 0

o  visitDictionary: aDictionary with: indent
(comment from inherited method)
visit a Dictionary

o  visitFloat: aFloat with: indent
(comment from inherited method)
visit a Float

o  visitInteger: anInteger with: indent
(comment from inherited method)
visit an Integer

o  visitNilWith: indent
(comment from inherited method)
visit nil - fallback here is to call visitObject

o  visitNumber: aNumber with: indent
(comment from inherited method)
visit any number - fallback here is to call visitObject

o  visitObject: someObject with: indent
encode the object like a dictionary, using the object's instVarNames as keys.

o  visitProtoObject: someObject with: indent
I cannot encode proto objects

o  visitSequenceableCollection: aCollection with: indent
(comment from inherited method)
visit a SequenceableCollection

o  visitString: aString with: indent
dquote

o  visitTime: aTime with: indent
JSONReader encode:{ Time now }
JSONPrinter new
useISODateFormat:false;
encode:{ Time now }

JSONReader decode:(JSONReader encode:{ Time now })
JSONReader decode:(JSONPrinter new
useISODateFormat:false;
encode:{ Time now })

o  visitTimestamp: aTimestamp with: indent
Month dd, yyyy hh:mm:ss


Examples:


see examples in JSONReader

ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Mon, 04 Jul 2022 11:39:53 GMT