NeoJSON with Rest

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

NeoJSON with Rest

Pharo Smalltalk Users mailing list
Hello.

I want an example of implementation of neojson on a pharo object.

I am trying to implement neoJsonOn: method on a pharo object but when I
test I get a Character mapping not found

in NeoJSONWriter.

Thanks.


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON with Rest

Pharo Smalltalk Users mailing list
I really want to know how to map my objects using NeoJSON.

I don't have any idea.

thanks.


On 19/11/2016 08:24, Asbath Sama biyalou wrote:

> Hello.
>
> I want an example of implementation of neojson on a pharo object.
>
> I am trying to implement neoJsonOn: method on a pharo object but when I
> test I get a Character mapping not found
>
> in NeoJSONWriter.
>
> Thanks.
>


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON with Rest

Sven Van Caekenberghe-2
In reply to this post by Pharo Smalltalk Users mailing list
Did you read https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/NeoJSON/NeoJSON.html ?

Did you check out the unit tests ? Some of them serve as examples. Checkout NeoJSONTestObject1 2 3 and the tests #testObject1 2 3 for example.

> On 19 Nov 2016, at 08:24, Asbath Sama biyalou via Pharo-users <[hidden email]> wrote:
>
>
> From: Asbath Sama biyalou <[hidden email]>
> Subject: NeoJSON with Rest
> Date: 19 November 2016 at 08:24:13 GMT+1
> To: Pharo users users <[hidden email]>
>
>
> Hello.
>
> I want an example of implementation of neojson on a pharo object.
>
> I am trying to implement neoJsonOn: method on a pharo object but when I
> test I get a Character mapping not found
>
> in NeoJSONWriter.
>
> Thanks.
>
>
>
>


kmo
Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON with Rest

kmo
In reply to this post by Pharo Smalltalk Users mailing list
This works for me:

Say you have a class called MyObject and you want a JSON representation of it.

On the class side of MyObject add this method:

neoJsonMapping: mapper
        "For inheritance mapping to work, the schema/classname must be self,
        but the instance variables have to be enumerated manually per class"

        mapper for: self do: [ :mapping |
                mapping mapAllInstVars ].


On the instance side of MyObject add:

asJson
        ^NeoJSONWriter toString: self.

Now you can return JSON for any instance of MyObject with a simple:

myobjectinstance asJson.

This code gives you all the instance variables of MyObject as a JSON string, but you can change the mapping statement to tailor it to include exactly what you want.

Hope this helps.

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON with Rest

Pharo Smalltalk Users mailing list
In reply to this post by Sven Van Caekenberghe-2

Thank you Sven. I check those example. very helpfull.

But I use this method toJson to convert the stream to json and I get :

PrimitiveFailed: primitive #basicNew: in ByteString class failed

toJson
^ String streamContents: [ :stream |
	(NeoJSONWriter on: stream)
	for: Date
	customDo: [ :mapping | mapping encoder: [ :value | value asDateAndTime printString  ] ];
	nextPut: data ]

Did you see some errors.



On 19/11/2016 09:52, Sven Van Caekenberghe wrote:
Did you read https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/NeoJSON/NeoJSON.html ?

Did you check out the unit tests ? Some of them serve as examples. Checkout NeoJSONTestObject1 2 3 and the tests #testObject1 2 3 for example. 

On 19 Nov 2016, at 08:24, Asbath Sama biyalou via Pharo-users [hidden email] wrote:


From: Asbath Sama biyalou [hidden email]
Subject: NeoJSON with Rest
Date: 19 November 2016 at 08:24:13 GMT+1
To: Pharo users users [hidden email]


Hello.

I want an example of implementation of neojson on a pharo object.

I am trying to implement neoJsonOn: method on a pharo object but when I
test I get a Character mapping not found

in NeoJSONWriter.

Thanks.





    

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON with Rest

Sven Van Caekenberghe-2
In reply to this post by Sven Van Caekenberghe-2
This works for me:

String streamContents: [ :out |
  (NeoJSONWriter on: out)
     for: Date customDo: [ :mapping | mapping encoder: [ :value | value asDateAndTime printString ] ];
     nextPut: { #date -> Date today } asDictionary ].
       
"'{""date"":""2016-11-19T00:00:00+01:00""}'"
       
String streamContents: [ :out |
  (NeoJSONWriter on: out)
     for: Date customDo: [ :mapping | mapping encoder: [ :value | value yyyymmdd ] ];
     nextPut: { #date -> Date today } asDictionary ].
       
"'{""date"":""2016-11-19""}'"

> On 19 Nov 2016, at 15:32, Asbath Sama biyalou <[hidden email]> wrote:
>
> Thank you Sven. I check those example. very helpfull.
>
> But I use this method toJson to convert the stream to json and I get :
>
> PrimitiveFailed: primitive #basicNew: in ByteString class failed
>
> toJson
> ^ String streamContents: [ :stream |
> (NeoJSONWriter on: stream)
> for: Date
> customDo: [ :mapping | mapping encoder: [ :value | value asDateAndTime printString  ] ];
> nextPut: data ]
>
> Did you see some errors.
>
>
>
> On 19/11/2016 09:52, Sven Van Caekenberghe wrote:
>> Did you read https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/NeoJSON/NeoJSON.html
>>  ?
>>
>> Did you check out the unit tests ? Some of them serve as examples. Checkout NeoJSONTestObject1 2 3 and the tests #testObject1 2 3 for example.
>>
>>
>>> On 19 Nov 2016, at 08:24, Asbath Sama biyalou via Pharo-users <[hidden email]>
>>>  wrote:
>>>
>>>
>>> From: Asbath Sama biyalou
>>> <[hidden email]>
>>>
>>> Subject: NeoJSON with Rest
>>> Date: 19 November 2016 at 08:24:13 GMT+1
>>> To: Pharo users users
>>> <[hidden email]>
>>>
>>>
>>>
>>> Hello.
>>>
>>> I want an example of implementation of neojson on a pharo object.
>>>
>>> I am trying to implement neoJsonOn: method on a pharo object but when I
>>> test I get a Character mapping not found
>>>
>>> in NeoJSONWriter.
>>>
>>> Thanks.
>>>
>>>
>>>
>>>
>>>
>