Great the magritte json binding exists. I need it and thought I need to build it myself.
Can't wait to return from my vacation (ok, not really true) to test it.
I will use it together with the conumes (accept-...) feature you added to seaside-rest.
That's a nice example on how do multiformat web APIs.
Great stuff,
Norbert
Am 09.08.2011 um 06:58 schrieb Philippe Marschall <philippe.marschall(a)gmail.com>om>:
2011/8/8 Nick Ager <nick.ager(a)gmail.com>om>:
Hi,
I'm experimenting with a JSON converter for objects with Magritte
descriptions. So far I've added the following to GRObject:
jsonOn: aStream
| description |
description := self description.
description children isEmpty ifFalse: [
| first |
first := true.
aStream nextPut: ${.
description do: [ :each |
first
ifTrue: [ first := false ]
ifFalse: [ aStream nextPut: $, ].
aStream
json: each label;
nextPut: $:;
json: (each accessor read: self) ].
aStream nextPut: $} ]
you can then generate JSON from an object, for example:
((PRPage named: 'examplePage') contents: '!!Hello', String crlf,
'Hello
""world"" check') asJson
gives:
'{"Name":"examplePage","Visibility":false,"Navigation
Title":"ExamplePage","Tags":[],"Title":"ExamplePage","Owner":null,"Group":null,"Contents":{},"Plain
Text":"Hello\rHello world check","Environment":null,"Style
Sheet":null,"Shortcut Icon":null,"CSS Classes":null}'
The method fails with recursive structures, but otherwise is appears to work
well.
Recursive structures don't work in JSON in general.
Does this look like a sensible approach? Has
anyone else implemented
anything similar?
There is Magritte-Json in [1]. It gives you more control, you can
override the name of a property and choose not to serialize certain
objects. I found this necessary when implementing certain predefined
APIs. It's a bit more "magritty" in the sense that is uses reader and
writer objects. Though it could certainly be improved on. You can find
examples in [2].
[1]
http://source.lukas-renggli.ch/magritteaddons
[2]
http://www.squeaksource.com/orion
Cheers
Philippe
_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki