|
Class: JSONPrinter
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
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
API - encoding
-
encode: anObject
-
return a JSON string which represents the object (can be sent to JavaScript).
-
encode: anObject on: aStream
-
append a compact JSON string which represents the object onto aStream
(can be sent to JavaScript).
-
encodePrettyPrinted: anObject
-
return a JSON string which represents the object (can be sent to JavaScript).
-
encodePrettyPrinted: anObject on: aStream
-
append a prettyPrinted (indented) JSON string which represents the object
onto aStream (can be sent to JavaScript).
-
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
-
typeInfoFormatNone
-
if set via typeInfoFormat:, slots are encoded in standard JSON (no type info)
-
typeInfoFormatSTX
-
if set via typeInfoFormat:,
slots are encoded as:
{ '@type': , <classNameString> , 'value': <encoding> }
-
typeInfoFormatSTX2
-
if set via typeInfoFormat:,
object are store with an additional slot, as:
{ '@type': , <classNameString> ,
...
... regular slots ...
...
}
instance creation
-
new
-
return an initialized instance
accessing
-
skipNil
-
if true, nil entries are skipped (default is false)
-
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
-
encode: anObject
-
-
encode: anObject on: aStream
-
initialization
-
initialize
-
(comment from inherited method)
just to ignore initialize to objects which do not need it
-
makeSlotEncoderFor: typeInfoFormat
-
JSONTypeEncoder newEncoderFor:nil
JSONTypeEncoder newEncoderFor:#stx
-
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.
|
-
skipNil: aBoolean
-
if true, nil entries are skipped (default is false)
-
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 }.
|
-
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
-
visitBoolean: aBoolean with: indent
-
(comment from inherited method)
visit a Boolean - fallback here is to call visitObject
-
visitDate: aDate with: indent
-
months start at 0
-
visitDictionary: aDictionary with: indent
-
(comment from inherited method)
visit a Dictionary
-
visitFloat: aFloat with: indent
-
(comment from inherited method)
visit a Float
-
visitInteger: anInteger with: indent
-
(comment from inherited method)
visit an Integer
-
visitNilWith: indent
-
(comment from inherited method)
visit nil - fallback here is to call visitObject
-
visitNumber: aNumber with: indent
-
(comment from inherited method)
visit any number - fallback here is to call visitObject
-
visitObject: someObject with: indent
-
encode the object like a dictionary, using the object's instVarNames as keys.
-
visitProtoObject: someObject with: indent
-
I cannot encode proto objects
-
visitSequenceableCollection: aCollection with: indent
-
(comment from inherited method)
visit a SequenceableCollection
-
visitString: aString with: indent
-
dquote
-
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 })
-
visitTimestamp: aTimestamp with: indent
-
Month dd, yyyy hh:mm:ss
see examples in JSONReader
|