[Seaside] WAJsonCanvas

Esteban A. Maringolo emaringolo at gmail.com
Mon Feb 24 19:10:22 UTC 2014

Hi Johan,

2014-02-24 15:47 GMT-03:00 Johan Brichau <johan at inceptive.be>:
> Hi Esteban,
>> But even when it's not a common use case scenario, you could have a
>> mixed objects array.
>> Something like: ['string', 3.14, {key: 'value', foo: 'baz'}, [...], false]
>> In that case it wouldn't be just "extending" the WAJsonObjectBrush
>> openTag, but it should be something at other level, that let you
>> manage the elements any way you want. Hence the #element: method.
> I believe this is exactly the way #value: works.
> Is there a reason why the following does work for you in such a case?
> WAJsonCanvas builder render: [ :json |
>         json value:  (Array with: 'string' with: 3.14 with: (Dictionary new at: 'key' put: 'value';yourself))
>         ]
> The way I understand how the canvas is designed is that #value still supports the 'old-style' conversion of Smalltalk object to json, and that #object:, #array, etc. allow to build json directly without the intermediate stop of building arrays and dictionaries. But I must admit I have not played with the JSON Canvas in 3.1 yet.

It works that way if you already have the collection and it's
converted to its JSON representation, but if you're building a JSON
that doesn't exactly maps the "structure" of your objects, then you
can't. Or even if you're creating a JSON that doesn't look anything
like the receiver's object graph. (*)

Or... if I'm fetching objects from a forward only collection (stream,
database, etc.) and serializing them as JSON, I don't have (nor want
to have either) the whole objects collection in memory.

e.g. (unsafe/naive iteration)

WAJsonCanvas builder render: [ :json |
  json array: [
    [ aStream atEnd ] whileFalse: [
         json element: [json value: aStream next]

If instead of #element: I use #value: I get all the elements rendered
at JSON but with no element separator. It is... without any commas in


Esteban A. Maringolo

(*) That's why I used the old #jsonOn: instead of the stream based
NeoJSON, because it was simpler to me to build the intermediate
dictionaries and convert everything to a JSON string at the end, with
memory penalty of having a huge nested dictionaries object graph.

More information about the seaside mailing list