NeoJSON

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

NeoJSON

stepharong
Hi sven

I'm trying to adapt the teapot library example to get a simple item  
collector (as a project for a future book) and
so that I can learn and use it for my PS2/PS3 game collection :)

Now when I set up teapot to emit JSON I get an NeoJSONMappingNotFound
In the library example attila does not manipulate objects but dictionaries.
So I imagine that I have to do something :)
I read the NeoJSON chapter but I did not find the solution.

Should I implement
        neoJsonOn:
on my domain?

Stef

--
Using Opera's mail client: http://www.opera.com/mail/

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

stepharong
I looked at the Neo code and I do not see how I can specify a mapping at  
the class level
because I cannot control the json writer creation.
So I should probably shortcut everything at the neoJsonOn: level.

Stef


> Hi sven
>
> I'm trying to adapt the teapot library example to get a simple item  
> collector (as a project for a future book) and
> so that I can learn and use it for my PS2/PS3 game collection :)
>
> Now when I set up teapot to emit JSON I get an NeoJSONMappingNotFound
> In the library example attila does not manipulate objects but  
> dictionaries.
> So I imagine that I have to do something :)
> I read the NeoJSON chapter but I did not find the solution.
>
> Should I implement
> neoJsonOn:
> on my domain?
>
> Stef
>


--
Using Opera's mail client: http://www.opera.com/mail/

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

Sven Van Caekenberghe-2
In reply to this post by stepharong
Stef,

> On 16 Dec 2016, at 15:00, stepharong <[hidden email]> wrote:
>
> Hi sven
>
> I'm trying to adapt the teapot library example to get a simple item collector (as a project for a future book) and
> so that I can learn and use it for my PS2/PS3 game collection :)
>
> Now when I set up teapot to emit JSON I get an NeoJSONMappingNotFound
> In the library example attila does not manipulate objects but dictionaries.
> So I imagine that I have to do something :)
> I read the NeoJSON chapter but I did not find the solution.
>
> Should I implement
> neoJsonOn:
> on my domain?

Section 5 of https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/NeoJSON/NeoJSON.html explains most of this.

You either add the mapping to the writer, builder style, or you add it to the class side of your model objects as #neoJsonMapping: (search for implementors as examples). I see that this second aspect is not well explained in the book.

In the simplest case, the following is enough:

neoJsonMapping: mapper
        mapper for: self do: [ :mapping |
                mapping mapInstVars: #(id width height data) ]

But is gets a bit more complicated with inheritance.

In a last resort you could also overwrite #neoJsonOn:

Now, this is all for the writer side. Reading is harder because JSON has no type info (that what STON adds, among others), so you have to tell the reader what (static) type you want the parser to create. This is based on the same mapping. This is what #nextAs: does.

If you have a more complex graph with collection values, you need to type all of them 'statically'. There are some examples in the unit tests.

Sven

> Stef
>
> --
> Using Opera's mail client: http://www.opera.com/mail/
>


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

Sven Van Caekenberghe-2
In reply to this post by stepharong

> On 16 Dec 2016, at 15:10, stepharong <[hidden email]> wrote:
>
> I looked at the Neo code and I do not see how I can specify a mapping at the class level
> because I cannot control the json writer creation.

See my previous message (sent at the same time ;-)

<< add it to the class side of your model objects as #neoJsonMapping: (search for implementors as examples). >>

> So I should probably shortcut everything at the neoJsonOn: level.
>
> Stef
>
>
>> Hi sven
>>
>> I'm trying to adapt the teapot library example to get a simple item collector (as a project for a future book) and
>> so that I can learn and use it for my PS2/PS3 game collection :)
>>
>> Now when I set up teapot to emit JSON I get an NeoJSONMappingNotFound
>> In the library example attila does not manipulate objects but dictionaries.
>> So I imagine that I have to do something :)
>> I read the NeoJSON chapter but I did not find the solution.
>>
>> Should I implement
>> neoJsonOn:
>> on my domain?
>>
>> Stef
>>
>
>
> --
> Using Opera's mail client: http://www.opera.com/mail/
>


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

stepharong
In reply to this post by Sven Van Caekenberghe-2
Ok I see.

I was doing

GameCollection >> neoJsonOn: neoJSONWriter

        neoJSONWriter
                writeObject: games


GameItem >> neoJsonOn: neoJSONWriter
        self class instanceVariables
                do: [ :each |
                                neoJSONWriter writeObject: (self instVarNamed: each) ]


Now it did not work because byteString is not covered and I found that  
strange.

I will use your solution.

I read the section 5 before so there is something missing there.
I will see how I can add an example.

May be we should improve the class comment of mapper stating this class  
methods.

Stef

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

Sven Van Caekenberghe-2

> On 16 Dec 2016, at 15:23, stepharong <[hidden email]> wrote:
>
> Ok I see.
>
> I was doing
>
> GameCollection >> neoJsonOn: neoJSONWriter
>
> neoJSONWriter
> writeObject: games
>
>
> GameItem >> neoJsonOn: neoJSONWriter
> self class instanceVariables
> do: [ :each |
> neoJSONWriter writeObject: (self instVarNamed: each) ]
>
>
> Now it did not work because byteString is not covered and I found that strange.
>
> I will use your solution.
>
> I read the section 5 before so there is something missing there.
> I will see how I can add an example.
>
> May be we should improve the class comment of mapper stating this class methods.

It is right there, in NeoJSONMapper's class comment

...
A mapping can be specified explicitely on a mapper, or can be resolved using the #neoJsonMapping: class method.
...

This is the superclass of both NeoJSONReader and NeoJSONWriter.

But we should add it to the book chapter too.

> Stef


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

stepharong
Ok it works :)

And I read the class comment but I do not see it.

Now do you have an idea why I got this other missing class mapping for  
bytestring?

Stef

On Fri, 16 Dec 2016 15:25:40 +0100, Sven Van Caekenberghe <[hidden email]>  
wrote:

>
>> On 16 Dec 2016, at 15:23, stepharong <[hidden email]> wrote:
>>
>> Ok I see.
>>
>> I was doing
>>
>> GameCollection >> neoJsonOn: neoJSONWriter
>>
>> neoJSONWriter
>> writeObject: games
>>
>>
>> GameItem >> neoJsonOn: neoJSONWriter
>> self class instanceVariables
>> do: [ :each |
>> neoJSONWriter writeObject: (self instVarNamed: each) ]
>>
>>
>> Now it did not work because byteString is not covered and I found that  
>> strange.
>>
>> I will use your solution.
>>
>> I read the section 5 before so there is something missing there.
>> I will see how I can add an example.
>>
>> May be we should improve the class comment of mapper stating this class  
>> methods.
>
> It is right there, in NeoJSONMapper's class comment
>
> ...
> A mapping can be specified explicitely on a mapper, or can be resolved  
> using the #neoJsonMapping: class method.
> ...
>
> This is the superclass of both NeoJSONReader and NeoJSONWriter.
>
> But we should add it to the book chapter too.
>
>> Stef
>


--
Using Opera's mail client: http://www.opera.com/mail/

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

Sven Van Caekenberghe-2

> On 16 Dec 2016, at 15:47, stepharong <[hidden email]> wrote:
>
> Ok it works :)

Good.

> And I read the class comment but I do not see it.

Strange.

In Neo-JSON-Core-SvenVanCaekenberghe.37 in the class comment of NeoJSONMapper last paragraph before the examples.

> Now do you have an idea why I got this other missing class mapping for bytestring?

You must have done something wrong ;-)

NeoJSONWriter toString: { 'string'. #symbol. 1. Float pi }.

'["string","symbol",1,3.141592653589793]'

> Stef
>
> On Fri, 16 Dec 2016 15:25:40 +0100, Sven Van Caekenberghe <[hidden email]> wrote:
>
>>
>>> On 16 Dec 2016, at 15:23, stepharong <[hidden email]> wrote:
>>>
>>> Ok I see.
>>>
>>> I was doing
>>>
>>> GameCollection >> neoJsonOn: neoJSONWriter
>>>
>>> neoJSONWriter
>>> writeObject: games
>>>
>>>
>>> GameItem >> neoJsonOn: neoJSONWriter
>>> self class instanceVariables
>>> do: [ :each |
>>> neoJSONWriter writeObject: (self instVarNamed: each) ]
>>>
>>>
>>> Now it did not work because byteString is not covered and I found that strange.
>>>
>>> I will use your solution.
>>>
>>> I read the section 5 before so there is something missing there.
>>> I will see how I can add an example.
>>>
>>> May be we should improve the class comment of mapper stating this class methods.
>>
>> It is right there, in NeoJSONMapper's class comment
>>
>> ...
>> A mapping can be specified explicitely on a mapper, or can be resolved using the #neoJsonMapping: class method.
>> ...
>>
>> This is the superclass of both NeoJSONReader and NeoJSONWriter.
>>
>> But we should add it to the book chapter too.
>>
>>> Stef
>>
>
>
> --
> Using Opera's mail client: http://www.opera.com/mail/


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

stepharong

> Strange.
>
> In Neo-JSON-Core-SvenVanCaekenberghe.37 in the class comment of  
> NeoJSONMapper last paragraph before the examples.

sure I just meant that it was not visible enough.
We should add for example

XXX >> neoJsonMapping: aMapper

        aMapper for: self do: [ :mapping |
                mapping mapInstVars:
  #(#title #kind #hasDoc #grade #stars #isCollectorEdition #paidPrice  
#language #zone)]



>
>> Now do you have an idea why I got this other missing class mapping for  
>> bytestring?
>
> You must have done something wrong ;-)
>
> NeoJSONWriter toString: { 'string'. #symbol. 1. Float pi }.
>
> '["string","symbol",1,3.141592653589793]'

I do not think so because my objects totally stupid.
So this is why I was surprised.


here are two

exampleKlonoa
        <sampleInstance>
        ^ self new
                title: 'Klonoa';
                ps2;
                threeStar;
                grade: '(16/15)';
                paidPrice: 1


exampleWildArm
        <sampleInstance>
        ^ self new
                title: 'Wild Arm';
                ps2;
                threeStar;
                grade: '(16/15)';
                paidPrice: 1

only strings, numbers and symbols.
this ByteString looked strange.


>
>> Stef
>>
>> On Fri, 16 Dec 2016 15:25:40 +0100, Sven Van Caekenberghe  
>> <[hidden email]> wrote:
>>
>>>
>>>> On 16 Dec 2016, at 15:23, stepharong <[hidden email]> wrote:
>>>>
>>>> Ok I see.
>>>>
>>>> I was doing
>>>>
>>>> GameCollection >> neoJsonOn: neoJSONWriter
>>>>
>>>> neoJSONWriter
>>>> writeObject: games
>>>>
>>>>
>>>> GameItem >> neoJsonOn: neoJSONWriter
>>>> self class instanceVariables
>>>> do: [ :each |
>>>> neoJSONWriter writeObject: (self instVarNamed: each) ]
>>>>
>>>>
>>>> Now it did not work because byteString is not covered and I found  
>>>> that strange.
>>>>
>>>> I will use your solution.
>>>>
>>>> I read the section 5 before so there is something missing there.
>>>> I will see how I can add an example.
>>>>
>>>> May be we should improve the class comment of mapper stating this  
>>>> class methods.
>>>
>>> It is right there, in NeoJSONMapper's class comment
>>>
>>> ...
>>> A mapping can be specified explicitely on a mapper, or can be resolved  
>>> using the #neoJsonMapping: class method.
>>> ...
>>>
>>> This is the superclass of both NeoJSONReader and NeoJSONWriter.
>>>
>>> But we should add it to the book chapter too.
>>>
>>>> Stef
>>>
>>
>>
>> --
>> Using Opera's mail client: http://www.opera.com/mail/
>


--
Using Opera's mail client: http://www.opera.com/mail/

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON

Sven Van Caekenberghe-2
Stef,

Do you still have trouble with this ?
It is really hard for me to help you without a reproducible case.

Sven

> On 16 Dec 2016, at 21:02, stepharong <[hidden email]> wrote:
>
>>
>> Strange.
>>
>> In Neo-JSON-Core-SvenVanCaekenberghe.37 in the class comment of NeoJSONMapper last paragraph before the examples.
>
> sure I just meant that it was not visible enough.
> We should add for example
>
> XXX >> neoJsonMapping: aMapper
>
> aMapper for: self do: [ :mapping |
> mapping mapInstVars:
> #(#title #kind #hasDoc #grade #stars #isCollectorEdition #paidPrice #language #zone)]
>
>
>
>>
>>> Now do you have an idea why I got this other missing class mapping for bytestring?
>>
>> You must have done something wrong ;-)
>>
>> NeoJSONWriter toString: { 'string'. #symbol. 1. Float pi }.
>>
>> '["string","symbol",1,3.141592653589793]'
>
> I do not think so because my objects totally stupid.
> So this is why I was surprised.
>
>
> here are two
>
> exampleKlonoa
> <sampleInstance>
> ^ self new
> title: 'Klonoa';
> ps2;
> threeStar;
> grade: '(16/15)';
> paidPrice: 1
>
>
> exampleWildArm
> <sampleInstance>
> ^ self new
> title: 'Wild Arm';
> ps2;
> threeStar;
> grade: '(16/15)';
> paidPrice: 1
>
> only strings, numbers and symbols.
> this ByteString looked strange.
>
>
>>
>>> Stef
>>>
>>> On Fri, 16 Dec 2016 15:25:40 +0100, Sven Van Caekenberghe <[hidden email]> wrote:
>>>
>>>>
>>>>> On 16 Dec 2016, at 15:23, stepharong <[hidden email]> wrote:
>>>>>
>>>>> Ok I see.
>>>>>
>>>>> I was doing
>>>>>
>>>>> GameCollection >> neoJsonOn: neoJSONWriter
>>>>>
>>>>> neoJSONWriter
>>>>> writeObject: games
>>>>>
>>>>>
>>>>> GameItem >> neoJsonOn: neoJSONWriter
>>>>> self class instanceVariables
>>>>> do: [ :each |
>>>>> neoJSONWriter writeObject: (self instVarNamed: each) ]
>>>>>
>>>>>
>>>>> Now it did not work because byteString is not covered and I found that strange.
>>>>>
>>>>> I will use your solution.
>>>>>
>>>>> I read the section 5 before so there is something missing there.
>>>>> I will see how I can add an example.
>>>>>
>>>>> May be we should improve the class comment of mapper stating this class methods.
>>>>
>>>> It is right there, in NeoJSONMapper's class comment
>>>>
>>>> ...
>>>> A mapping can be specified explicitely on a mapper, or can be resolved using the #neoJsonMapping: class method.
>>>> ...
>>>>
>>>> This is the superclass of both NeoJSONReader and NeoJSONWriter.
>>>>
>>>> But we should add it to the book chapter too.
>>>>
>>>>> Stef
>>>>
>>>
>>>
>>> --
>>> Using Opera's mail client: http://www.opera.com/mail/
>>
>
>
> --
> Using Opera's mail client: http://www.opera.com/mail/