Question about NeoJSON

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

Question about NeoJSON

Mariano Martinez Peck
Hi,


I see an example of a custom mapping like this one:

mapper for: TestObject do: [ :mapping |
    mapping mapInstVars: #(id name).
    (mapping mapInstVar: #timestamp to: 'created-at') valueSchema: DateAndTime.
    (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
    (mapping mapInstVar: #bytes) valueSchema: ByteArray ].


I need something similar, but I need to store the original JSON string that was used to build a particular TestObject instance, in an instance variable of TestObject too. In other words, I need an instVar "originalResponse" in TestObject that would be set with the original JSON string.

Any ideas how can I implement this?

Thanks in advance,

--
Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Esteban A. Maringolo
Assuming you store the original JSON string as a String object in an
instance variable named "json" you could map it as any other string.

Or are you planning to store it as JSON object itself?
Esteban A. Maringolo


2015-03-12 16:50 GMT-03:00 Mariano Martinez Peck <[hidden email]>:

> Hi,
>
> In this link:
> https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md
>
> I see an example of a custom mapping like this one:
>
> mapper for: TestObject do: [ :mapping |
>     mapping mapInstVars: #(id name).
>     (mapping mapInstVar: #timestamp to: 'created-at') valueSchema:
> DateAndTime.
>     (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
>     (mapping mapInstVar: #bytes) valueSchema: ByteArray ].
>
>
> I need something similar, but I need to store the original JSON string that
> was used to build a particular TestObject instance, in an instance variable
> of TestObject too. In other words, I need an instVar "originalResponse" in
> TestObject that would be set with the original JSON string.
>
> Any ideas how can I implement this?
>
> Thanks in advance,
>
> --
> Mariano
> http://marianopeck.wordpress.com

Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Mariano Martinez Peck


On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]> wrote:
Assuming you store the original JSON string as a String object in an
instance variable named "json" you could map it as any other string.


But how do I get the JSON string associated to the TestObject instance?  In other words...

mapper for: TestObject do: [ :mapping |

"HERE how do I get the JSON string of TestObject in order to map it to 'jsonOriginalString'  ?  " 
 
]

 
Or are you planning to store it as JSON object itself?

no, a string is OK. 

Thanks in advance, 


 
Esteban A. Maringolo


2015-03-12 16:50 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> Hi,
>
> In this link:
> https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md
>
> I see an example of a custom mapping like this one:
>
> mapper for: TestObject do: [ :mapping |
>     mapping mapInstVars: #(id name).
>     (mapping mapInstVar: #timestamp to: 'created-at') valueSchema:
> DateAndTime.
>     (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
>     (mapping mapInstVar: #bytes) valueSchema: ByteArray ].
>
>
> I need something similar, but I need to store the original JSON string that
> was used to build a particular TestObject instance, in an instance variable
> of TestObject too. In other words, I need an instVar "originalResponse" in
> TestObject that would be set with the original JSON string.
>
> Any ideas how can I implement this?
>
> Thanks in advance,
>
> --
> Mariano
> http://marianopeck.wordpress.com




--
Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Sven Van Caekenberghe-2
Mariano,

I assume you what this while reading JSON. That won't be possible. NeoJSON is designed as an efficient stream parser, working as it goes. So it never knows the whole JSON expression.

I would do it manually, but that can only be done at the top level.

Sven

> On 12 Mar 2015, at 21:22, Mariano Martinez Peck <[hidden email]> wrote:
>
>
>
> On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]> wrote:
> Assuming you store the original JSON string as a String object in an
> instance variable named "json" you could map it as any other string.
>
>
> But how do I get the JSON string associated to the TestObject instance?  In other words...
>
> mapper for: TestObject do: [ :mapping |
>
> "HERE how do I get the JSON string of TestObject in order to map it to 'jsonOriginalString'  ?  "
>  
> ]
>
>  
> Or are you planning to store it as JSON object itself?
>
> no, a string is OK.
>
> Thanks in advance,
>
>
>  
> Esteban A. Maringolo
>
>
> 2015-03-12 16:50 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> > Hi,
> >
> > In this link:
> > https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md
> >
> > I see an example of a custom mapping like this one:
> >
> > mapper for: TestObject do: [ :mapping |
> >     mapping mapInstVars: #(id name).
> >     (mapping mapInstVar: #timestamp to: 'created-at') valueSchema:
> > DateAndTime.
> >     (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
> >     (mapping mapInstVar: #bytes) valueSchema: ByteArray ].
> >
> >
> > I need something similar, but I need to store the original JSON string that
> > was used to build a particular TestObject instance, in an instance variable
> > of TestObject too. In other words, I need an instVar "originalResponse" in
> > TestObject that would be set with the original JSON string.
> >
> > Any ideas how can I implement this?
> >
> > Thanks in advance,
> >
> > --
> > Mariano
> > http://marianopeck.wordpress.com
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com


Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Mariano Martinez Peck


On Thu, Mar 12, 2015 at 5:32 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Mariano,

I assume you what this while reading JSON. That won't be possible. NeoJSON is designed as an efficient stream parser, working as it goes. So it never knows the whole JSON expression.

Uhhhh I imagined that :(  
 

I would do it manually, but that can only be done at the top level.

Well...my list of TestObject is not at the root, but second level.....
But in any case... how would you do that? 

Thanks!
 

Sven

> On 12 Mar 2015, at 21:22, Mariano Martinez Peck <[hidden email]> wrote:
>
>
>
> On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]> wrote:
> Assuming you store the original JSON string as a String object in an
> instance variable named "json" you could map it as any other string.
>
>
> But how do I get the JSON string associated to the TestObject instance?  In other words...
>
> mapper for: TestObject do: [ :mapping |
>
> "HERE how do I get the JSON string of TestObject in order to map it to 'jsonOriginalString'  ?  "
>
> ]
>
>
> Or are you planning to store it as JSON object itself?
>
> no, a string is OK.
>
> Thanks in advance,
>
>
>
> Esteban A. Maringolo
>
>
> 2015-03-12 16:50 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> > Hi,
> >
> > In this link:
> > https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md
> >
> > I see an example of a custom mapping like this one:
> >
> > mapper for: TestObject do: [ :mapping |
> >     mapping mapInstVars: #(id name).
> >     (mapping mapInstVar: #timestamp to: 'created-at') valueSchema:
> > DateAndTime.
> >     (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
> >     (mapping mapInstVar: #bytes) valueSchema: ByteArray ].
> >
> >
> > I need something similar, but I need to store the original JSON string that
> > was used to build a particular TestObject instance, in an instance variable
> > of TestObject too. In other words, I need an instVar "originalResponse" in
> > TestObject that would be set with the original JSON string.
> >
> > Any ideas how can I implement this?
> >
> > Thanks in advance,
> >
> > --
> > Mariano
> > http://marianopeck.wordpress.com
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com





--
Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Sven Van Caekenberghe-2
I don't really understand why you want to keep the original JSON. It is a bit like you don't trust the parsing and/or mapping. Now, the mapping I understand (somewhat).

So I would parse once without mapping, which would give you pure Arrays and Dictionaries that you are guaranteed can be used to reproduce the full original input. Pick the second level in that structure and use it.

> On 12 Mar 2015, at 21:47, Mariano Martinez Peck <[hidden email]> wrote:
>
>
>
> On Thu, Mar 12, 2015 at 5:32 PM, Sven Van Caekenberghe <[hidden email]> wrote:
> Mariano,
>
> I assume you what this while reading JSON. That won't be possible. NeoJSON is designed as an efficient stream parser, working as it goes. So it never knows the whole JSON expression.
>
> Uhhhh I imagined that :(  
>  
>
> I would do it manually, but that can only be done at the top level.
>
> Well...my list of TestObject is not at the root, but second level.....
> But in any case... how would you do that?
>
> Thanks!
>  
>
> Sven
>
> > On 12 Mar 2015, at 21:22, Mariano Martinez Peck <[hidden email]> wrote:
> >
> >
> >
> > On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]> wrote:
> > Assuming you store the original JSON string as a String object in an
> > instance variable named "json" you could map it as any other string.
> >
> >
> > But how do I get the JSON string associated to the TestObject instance?  In other words...
> >
> > mapper for: TestObject do: [ :mapping |
> >
> > "HERE how do I get the JSON string of TestObject in order to map it to 'jsonOriginalString'  ?  "
> >
> > ]
> >
> >
> > Or are you planning to store it as JSON object itself?
> >
> > no, a string is OK.
> >
> > Thanks in advance,
> >
> >
> >
> > Esteban A. Maringolo
> >
> >
> > 2015-03-12 16:50 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> > > Hi,
> > >
> > > In this link:
> > > https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md
> > >
> > > I see an example of a custom mapping like this one:
> > >
> > > mapper for: TestObject do: [ :mapping |
> > >     mapping mapInstVars: #(id name).
> > >     (mapping mapInstVar: #timestamp to: 'created-at') valueSchema:
> > > DateAndTime.
> > >     (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
> > >     (mapping mapInstVar: #bytes) valueSchema: ByteArray ].
> > >
> > >
> > > I need something similar, but I need to store the original JSON string that
> > > was used to build a particular TestObject instance, in an instance variable
> > > of TestObject too. In other words, I need an instVar "originalResponse" in
> > > TestObject that would be set with the original JSON string.
> > >
> > > Any ideas how can I implement this?
> > >
> > > Thanks in advance,
> > >
> > > --
> > > Mariano
> > > http://marianopeck.wordpress.com
> >
> >
> >
> >
> > --
> > Mariano
> > http://marianopeck.wordpress.com
>
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com


Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Esteban A. Maringolo
In reply to this post by Mariano Martinez Peck
2015-03-12 17:22 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]>
> wrote:

>> Assuming you store the original JSON string as a String object in an
>> instance variable named "json" you could map it as any other string.

> But how do I get the JSON string associated to the TestObject instance?  In
> other words...

Why doesn't this work?

mapper for: TestObject do: [ :mapping |
     "..."
    (mapping mapInstVar: #jsonOriginalString)
].

That would make sense if the jsonOriginalString is an immutable object
(from the application standpoint) that you keep in order to preserve
some sort of logging of how/where the object was created.

Regards.


Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Esteban A. Maringolo
In reply to this post by Sven Van Caekenberghe-2
2015-03-12 18:09 GMT-03:00 Sven Van Caekenberghe <[hidden email]>:
> I don't really understand why you want to keep the original JSON. It is a bit like you don't trust the parsing and/or mapping. Now, the mapping I understand (somewhat).

I guess this has to do with the fact that if you have a REST+JSON API
he wants to preserve the JSON payload that was used to create the
instance (in case of a POST).

So you first parse the JSON, create the object from it, and then
assign the JSON string to it (and this should never change).

Some API's preserve the entire request (including HTTP headers) in the
created/updated object.


Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Mariano Martinez Peck
In reply to this post by Sven Van Caekenberghe-2


On Thu, Mar 12, 2015 at 6:09 PM, Sven Van Caekenberghe <[hidden email]> wrote:
I don't really understand why you want to keep the original JSON. It is a bit like you don't trust the parsing and/or mapping. Now, the mapping I understand (somewhat).


Yes, there are a few domain-specific reasons, but let me outline a few:

1) Imagine that from this JSON I have a list of objects that I build. Imagine for a minute these are "InvestmentTransactions". The way I build these objects from JSON could be quite complex. And can even have different kind of JSONs that I receive to build same type of objects. So sometimes..it is very handy to have the original JSON string to see why an object was built that way, why it has certain state, etc.

2) The app user could later MODIFY the created instances of InvestmentTransactions. However, it is nice to see which was the original values in case of the transactions that were imported via JSON. 

3) These JSON I receive may have some query / API limits (queries per second etc). And I may need to replicate this to multiple other apps of mine. If all my apps were all calling the API I may likely reach limits. So one possibility was to implement our own REST / JSON server for our local apps. In that case, having the original JSON would allow me a "repeater server" very easily. BTW...note that having a cache in each server does not solve the ratio limit problem.

4) Imagine that so far the InvestmentTransction object have 20 instVars but the JSON element of a transaction brings 50 fields. I don't want to right now store everything...just the 20 instVars I need. In addition, the server that sends the JSON may send you only a time-window..say..the transactions of the last month. So..maybe.. in the future, my app has more features and then suddenly I need other properties I was not yet mapping. But the server only sends me the last month. Therefore, if I store the whole response string, I can have access to all data even later. 

Ok..these were a few reasons. Not all scenarios are like that, and maybe storing the original string is not the best solution either. I am just explaining why I find useful to store it. 
 
So I would parse once without mapping, which would give you pure Arrays and Dictionaries that you are guaranteed can be used to reproduce the full original input. Pick the second level in that structure and use it.


Indeed, that was what I was thinking too. Thanks for the alternative. 
 
> On 12 Mar 2015, at 21:47, Mariano Martinez Peck <[hidden email]> wrote:
>
>
>
> On Thu, Mar 12, 2015 at 5:32 PM, Sven Van Caekenberghe <[hidden email]> wrote:
> Mariano,
>
> I assume you what this while reading JSON. That won't be possible. NeoJSON is designed as an efficient stream parser, working as it goes. So it never knows the whole JSON expression.
>
> Uhhhh I imagined that :(
>
>
> I would do it manually, but that can only be done at the top level.
>
> Well...my list of TestObject is not at the root, but second level.....
> But in any case... how would you do that?
>
> Thanks!
>
>
> Sven
>
> > On 12 Mar 2015, at 21:22, Mariano Martinez Peck <[hidden email]> wrote:
> >
> >
> >
> > On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]> wrote:
> > Assuming you store the original JSON string as a String object in an
> > instance variable named "json" you could map it as any other string.
> >
> >
> > But how do I get the JSON string associated to the TestObject instance?  In other words...
> >
> > mapper for: TestObject do: [ :mapping |
> >
> > "HERE how do I get the JSON string of TestObject in order to map it to 'jsonOriginalString'  ?  "
> >
> > ]
> >
> >
> > Or are you planning to store it as JSON object itself?
> >
> > no, a string is OK.
> >
> > Thanks in advance,
> >
> >
> >
> > Esteban A. Maringolo
> >
> >
> > 2015-03-12 16:50 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> > > Hi,
> > >
> > > In this link:
> > > https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md
> > >
> > > I see an example of a custom mapping like this one:
> > >
> > > mapper for: TestObject do: [ :mapping |
> > >     mapping mapInstVars: #(id name).
> > >     (mapping mapInstVar: #timestamp to: 'created-at') valueSchema:
> > > DateAndTime.
> > >     (mapping mapInstVar: #points) valueSchema: #ArrayOfPoints.
> > >     (mapping mapInstVar: #bytes) valueSchema: ByteArray ].
> > >
> > >
> > > I need something similar, but I need to store the original JSON string that
> > > was used to build a particular TestObject instance, in an instance variable
> > > of TestObject too. In other words, I need an instVar "originalResponse" in
> > > TestObject that would be set with the original JSON string.
> > >
> > > Any ideas how can I implement this?
> > >
> > > Thanks in advance,
> > >
> > > --
> > > Mariano
> > > http://marianopeck.wordpress.com
> >
> >
> >
> >
> > --
> > Mariano
> > http://marianopeck.wordpress.com
>
>
>
>
>
> --
> Mariano
> http://marianopeck.wordpress.com





--
Reply | Threaded
Open this post in threaded view
|

Re: Question about NeoJSON

Mariano Martinez Peck
In reply to this post by Esteban A. Maringolo


On Thu, Mar 12, 2015 at 6:19 PM, Esteban A. Maringolo <[hidden email]> wrote:
2015-03-12 17:22 GMT-03:00 Mariano Martinez Peck <[hidden email]>:
> On Thu, Mar 12, 2015 at 5:16 PM, Esteban A. Maringolo <[hidden email]>
> wrote:

>> Assuming you store the original JSON string as a String object in an
>> instance variable named "json" you could map it as any other string.

> But how do I get the JSON string associated to the TestObject instance?  In
> other words...

Why doesn't this work?

mapper for: TestObject do: [ :mapping |
     "..."
    (mapping mapInstVar: #jsonOriginalString)
].


Because that would do nothing..that would let jsonOriginalString in nil.

 
That would make sense if the jsonOriginalString is an immutable object
(from the application standpoint) that you keep in order to preserve
some sort of logging of how/where the object was created.



Yes, exactly. This is one of the reasons.
 
Cheers, 

--