|
Class: JSONPrinter
Object
|
+--Visitor
|
+--AspectVisitor
|
+--ObjectCoder
|
+--JSONPrinter
- Package:
- stx:goodies/communication
- Category:
- Net-Communication-JSON
- Version:
- rev:
1.60
date: 2023/09/05 09:01:12
- user: stefan
- file: JSONPrinter.st directory: goodies/communication
- module: stx stc-classLibrary: communication
By default, 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.
And I do not handle recursive data structures.
However:
you can specify special type encodings which - with various detail and features
allow more info to be generated as additional '@type' slots.
Because similar schemes have been developed by various other frameworks,
and no common standard exists, these may or may not fit your needs.
If in doubt, you can add your own subclass of typeEncoder and do things customized.
Provided:
typeInfoFormat: nil - standard JSON only
typeInfoFormat: #stx - (in lack of a better name):
adds a slot named @type to dictionary objects
Does NOT support references (i.e. shared objects)
typeInfoFormat: #stx2 - (also: in lack of a better name):
wraps objects into a little dictionary containing
@type and @value slots.
Does NOT support references (i.e. shared objects)
typeInfoFormat: #stx3 - (a private scheme):
wraps objects into a little dictionary containing
@type and @value slots.
In addition, stored objects get a @id slot,
and can be references later with @ref
However, it does NOT (currently) support recursive references
from inner to outer object (i.e. from elements to their container),
because inner objects are decoded first.
This feature may be added in the future.
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
copyrightCOPYRIGHT (c) 2007 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
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).
-
encode: anObject typeInfoFormat: formatSymbolOrNil 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).
-
encodePrettyPrinted: anObject printLimit: limitOrNil
-
return a JSON string which represents the object (can be sent to JavaScript).
-
encodePrettyPrinted: anObject printLimit: limitOrNil on: aStream
-
append a prettyPrinted (indented) JSON string which represents the object
onto aStream (can be sent to JavaScript).
-
encodePrettyPrinted: anObject printLimit: limitOrNil typeInfoFormat: formatSymbolOrNil on: aStream
-
append a prettyPrinted (indented) JSON string which represents the object
onto aStream (can be sent to JavaScript).
-
encodePrettyPrinted: anObject typeInfoFormat: formatSymbolOrNil 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
-
defaultPrettyPrintLimit
-
-
deltaIndent
-
-
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 ...
...
}
-
typeInfoFormatSTX3
-
if set via typeInfoFormat:,
object are stored wrapped, as:
{ '@obj': , <id>
'@type': <classNameString> ,
'@value':
...
... regular slots ...
...
}
references are resolved as:
{ '@ref': , <id> }
instance creation
-
new
-
return an initialized instance
accessing
-
dateFormat
-
aSymbolOrNil controls how dates/times/timestamps are encoded;
it can be:
#iso (the default), generates a string containing the ISO format
#expression, generates a non-standard (but supported by some browsers) format
which is an isntance creation expression (eg. 'new Date(y,m,d)').
#escapedSlashes, generates a format used/suggested by many users, which generates a
string with type info in escaped slashes.
(eg. '\/Date(<isoFormatDate>)\/').
a 1-arg block, responsible to generate whatever it wants
-
dateFormat: aSymbolOrNilOrBlock
-
aSymbolOrNil controls how dates/times/timestamps are encoded;
it can be:
#iso (the default), generates a string containing the ISO format
#expression, generates a non-standard (but supported by some browsers) format
which is an isntance creation expression (eg. 'new Date(y,m,d)').
#escapedSlashes, generates a format used/suggested by many users, which generates a
string with type info in escaped slashes.
(eg. '\/Date(<isoFormatDate>)\/').
a 1-arg block, responsible to generate whatever it wants
-
prettyPrint
-
returns true if pretty printing (i.e. with line breaks and indentation);
false if not (i.e. all in one long line)
-
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.
|
-
printLimit: numberOfChars
-
to set a limit on the generated output string.
Only to be used to present JSON to a user (as in the inspector);
Truncated JSON cannot be decoded (obviusly)
-
skipNil
-
if true, nil entries are skipped (default is false)
-
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': {slots ...<encoding>} }
#stx2 : encode as { '@type': <classNameString> , slots... }
#stx3 : encode as { '@object': <id>, '@type': <classNameString> , { slots } }
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 }.
JSONPrinter new
typeInfoFormat:(JSONPrinter typeInfoFormatSTX);
encode:(10@10 corner:100@100).
JSONPrinter new
typeInfoFormat:(JSONPrinter typeInfoFormatSTX2);
encode:{ Date today . Time now . Timestamp now }.
JSONPrinter new
typeInfoFormat:(JSONPrinter typeInfoFormatSTX2);
encode:(10@10 corner:100@100).
|
-
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)')
It is not recommended to use this non-standard format, unless you really really need to.
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 }.
|
-
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 }.
|
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
visiting
-
visitBoolean: aBoolean with: indent
-
callback from a boolean object to be encoded
-
visitCDatum: aCDatum with: indent
-
callback from a CDatums object to be encoded
-
visitDate: aDate with: indent
-
callback from a date object to be encoded
-
visitDictionary: aDictionary with: indent
-
callback from a dictionary object to be encoded
-
visitFloat: aFloat with: indent
-
callback from a float object to be encoded
-
visitInteger: anInteger with: indent
-
callback from an integer object to be encoded
-
visitNilWith: indent
-
callback from nil to be encoded
-
visitNumber: aNumber with: indent
-
callback from any other number (not floar or integer) to be encoded
-
visitObject: someObject with: indent
-
encode the object like a dictionary, using the object's instVarNames as keys.
-
visitProtoObject: someObject with: indent
-
callback from a proto/handle/proxy to be encoded
-
visitSequenceableCollection: aCollection with: indent
-
callback from a collection to be encoded
-
visitSet: aSet with: indent
-
callback from a set to be encoded
-
visitString: aString with: indent
-
callback from a string to be encoded
-
visitTime: aTime with: indent
-
callback from a time to be encoded
-
visitTimestamp: aTimestamp with: indent
-
callback from a timestamp to be encoded
see examples in JSONReader
|