Magritte -> Json

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Magritte -> Json

Nick
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. 

Does this look like a sensible approach? Has anyone else implemented anything similar?

Thanks

Nick


_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: Magritte -> Json

Jan van de Sandt
Hi,

There is something simular in the Magritte Add-ons repository: the Magritte-Json package. It's definitely usefull functionality!

Jan.

On Mon, Aug 8, 2011 at 8:15 PM, Nick Ager <[hidden email]> wrote:
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. 

Does this look like a sensible approach? Has anyone else implemented anything similar?

Thanks

Nick


_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki


_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: Magritte -> Json

Philippe Marschall
In reply to this post by Nick
2011/8/8 Nick Ager <[hidden email]>:

> 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
Reply | Threaded
Open this post in threaded view
|

Re: Magritte -> Json

Nick
Hi Philippe,


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

It looks good and more comprehensive than my effort.

It appears that objects have to be derived from MJObject in order to use the Json conversion. Would there be a problem moving the #javascriptOn: method from MJObject to an extension method on GRObject to allow more objects to gain the Json conversion functionality? (I choose GRObject as WAObject, PRObject & MAObject are all ultimately derived from GRObject).

The difference between #asJavascript (with it's accompanying #javascriptOn:), asJson (with it's accompanying #jsonOn:)? It appears that even though #asJavascript and #asJson have largely the same effect, #asJson implements a Json compatible subset of #asJavascript. Would it not make sense to use #asJson instead of #asJavascript and #jsonOn: rather than #javascriptOn:, or have I missed something?

Cheers

Nick

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: Magritte -> Json

Philippe Marschall
2011/8/9 Nick Ager <[hidden email]>:

> Hi Philippe,
>
>> 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
>
> It looks good and more comprehensive than my effort.
> It appears that objects have to be derived from MJObject in order to use the
> Json conversion.

Without being certain I don't think so. It should work with any class
that has the same methods.

> Would there be a problem moving the #javascriptOn: method
> from MJObject to an extension method on GRObject to allow more objects to
> gain the Json conversion functionality? (I choose GRObject as WAObject,
> PRObject & MAObject are all ultimately derived from GRObject).

No.

> The difference between #asJavascript (with it's
> accompanying #javascriptOn:), asJson (with it's accompanying #jsonOn:)? It
> appears that even though #asJavascript and #asJson have largely the same
> effect, #asJson implements a Json compatible subset of #asJavascript. Would
> it not make sense to use #asJson instead of #asJavascript and #jsonOn:
> rather than #javascriptOn:,

Yes.

> or have I missed something?

No.

Feel free to change your proposed changes. I might also make sense to
move over some of the orion tests.

I hope you don't have to change your slides now ;-) I'm sure you're
also aware of Magritte-XMLBinding in the same repository that does XML
data binding.

Cheers
Philippe

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: Magritte -> Json

Nick
Hi Philippe,
 
Feel free to change your proposed changes. I might also make sense to
move over some of the orion tests.

I've checked in Magritte-Json-NickAger.10  and I've modified Orion, but couldn't check it in (permission issue) so I've attached my changes.

Cheers

Nick

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki

Orion-NickAger.18.mcz (18K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Magritte -> Json

NorbertHartl
In reply to this post by Philippe Marschall
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 <[hidden email]>:

> 2011/8/8 Nick Ager <[hidden email]>:
>> 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

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki