Provide a mapping for a collection of <Association>s in NeoJSON
Hello I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint? What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for. Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings? Best wishes Peter Kenny
Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this NDBFood>>neoJsonOn: neoJSONWriter "custom writing" neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself) i just implement my own writing for my objects. HTH On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk> wrote:
Hello
I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint?
What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for.
Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings?
Best wishes
Peter Kenny
-- Bernardo E.C. Sent from a cheap desktop computer in South America.
Bernardo Thanks for this. After sending my previous mail, I did what I should have done first and looked at list of collection objects in the system browser. So I discovered OrderedDictionary for the first time. I am pleased to see that this is the key to the solution. Peter From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Bernardo Ezequiel Contreras Sent: 30 June 2016 16:17 To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Subject: Re: [Pharo-users] Provide a mapping for a collection of <Association>s in NeoJSON Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this NDBFood>>neoJsonOn: neoJSONWriter "custom writing" neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself) i just implement my own writing for my objects. HTH On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk <mailto:peter@pbkresearch.co.uk> > wrote: Hello I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint? What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for. Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings? Best wishes Peter Kenny -- Bernardo E.C. Sent from a cheap desktop computer in South America.
Hi, Yes, object properties in JSON are not ordered, by definition. But for humans it can make sense. Converting your domain objects to OrderedDictionaries or overwriting #neoJSONOn: are valid ways to control NeoJSON. However, I also think that it might be possible to solve the ordering requirement with a regular mapping, provided I make a little internal change - I will investigate that and report back. Sven
On 30 Jun 2016, at 17:29, PBKResearch <peter@pbkresearch.co.uk> wrote:
Bernardo
Thanks for this. After sending my previous mail, I did what I should have done first and looked at list of collection objects in the system browser. So I discovered OrderedDictionary for the first time. I am pleased to see that this is the key to the solution.
Peter
From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Bernardo Ezequiel Contreras Sent: 30 June 2016 16:17 To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Subject: Re: [Pharo-users] Provide a mapping for a collection of <Association>s in NeoJSON
Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this
NDBFood>>neoJsonOn: neoJSONWriter "custom writing"
neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself)
i just implement my own writing for my objects.
HTH
On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk> wrote:
Hello
I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint?
What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for.
Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings?
Best wishes
Peter Kenny
-- Bernardo E.C.
Sent from a cheap desktop computer in South America.
Thanks sven I was thinking to add a section in the chapter but I will wait for your solution. stef Le 1/7/16 à 00:56, Sven Van Caekenberghe a écrit :
Hi,
Yes, object properties in JSON are not ordered, by definition. But for humans it can make sense.
Converting your domain objects to OrderedDictionaries or overwriting #neoJSONOn: are valid ways to control NeoJSON.
However, I also think that it might be possible to solve the ordering requirement with a regular mapping, provided I make a little internal change - I will investigate that and report back.
Sven
On 30 Jun 2016, at 17:29, PBKResearch <peter@pbkresearch.co.uk> wrote:
Bernardo
Thanks for this. After sending my previous mail, I did what I should have done first and looked at list of collection objects in the system browser. So I discovered OrderedDictionary for the first time. I am pleased to see that this is the key to the solution.
Peter
From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Bernardo Ezequiel Contreras Sent: 30 June 2016 16:17 To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Subject: Re: [Pharo-users] Provide a mapping for a collection of <Association>s in NeoJSON
Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this
NDBFood>>neoJsonOn: neoJSONWriter "custom writing"
neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself)
i just implement my own writing for my objects.
HTH
On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk> wrote:
Hello
I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint?
What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for.
Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings?
Best wishes
Peter Kenny
-- Bernardo E.C.
Sent from a cheap desktop computer in South America.
btw, why NeoJSON is not deprecated in benefit of STON? Esteban
On 01 Jul 2016, at 08:52, stepharo <stepharo@free.fr> wrote:
Thanks sven
I was thinking to add a section in the chapter but I will wait for your solution.
stef
Le 1/7/16 à 00:56, Sven Van Caekenberghe a écrit :
Hi,
Yes, object properties in JSON are not ordered, by definition. But for humans it can make sense. Converting your domain objects to OrderedDictionaries or overwriting #neoJSONOn: are valid ways to control NeoJSON.
However, I also think that it might be possible to solve the ordering requirement with a regular mapping, provided I make a little internal change - I will investigate that and report back.
Sven
On 30 Jun 2016, at 17:29, PBKResearch <peter@pbkresearch.co.uk> wrote:
Bernardo Thanks for this. After sending my previous mail, I did what I should have done first and looked at list of collection objects in the system browser. So I discovered OrderedDictionary for the first time. I am pleased to see that this is the key to the solution. Peter From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Bernardo Ezequiel Contreras Sent: 30 June 2016 16:17 To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Subject: Re: [Pharo-users] Provide a mapping for a collection of <Association>s in NeoJSON Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this NDBFood>>neoJsonOn: neoJSONWriter "custom writing" neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself) i just implement my own writing for my objects. HTH On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk> wrote:
Hello I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint? What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for. Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings? Best wishes Peter Kenny
-- Bernardo E.C. Sent from a cheap desktop computer in South America.
On 01 Jul 2016, at 11:27, Esteban Lorenzano <estebanlm@gmail.com> wrote:
btw, why NeoJSON is not deprecated in benefit of STON?
Although there is obviously a relation between the two (STON is inspired by JSON, STON has some basic backward compatibility with JSON, same project style, same author), there are also differences. JSON is by definition much simpler and much more limited (no type info for objects, no complex structures like shared and circular references) while STON was designed specifically to cover those. JSON needs much more explicit mapping support to deal with anything except for simple maps and lists. In terms of documentation, the API, it would confuse the hell out of regular JSON users to see things mixed up with STON which they are probably not interested in at all. I think this last point might even be the most important one.
Esteban
On 01 Jul 2016, at 08:52, stepharo <stepharo@free.fr> wrote:
Thanks sven
I was thinking to add a section in the chapter but I will wait for your solution.
stef
Le 1/7/16 à 00:56, Sven Van Caekenberghe a écrit :
Hi,
Yes, object properties in JSON are not ordered, by definition. But for humans it can make sense. Converting your domain objects to OrderedDictionaries or overwriting #neoJSONOn: are valid ways to control NeoJSON.
However, I also think that it might be possible to solve the ordering requirement with a regular mapping, provided I make a little internal change - I will investigate that and report back.
Sven
On 30 Jun 2016, at 17:29, PBKResearch <peter@pbkresearch.co.uk> wrote:
Bernardo Thanks for this. After sending my previous mail, I did what I should have done first and looked at list of collection objects in the system browser. So I discovered OrderedDictionary for the first time. I am pleased to see that this is the key to the solution. Peter From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Bernardo Ezequiel Contreras Sent: 30 June 2016 16:17 To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Subject: Re: [Pharo-users] Provide a mapping for a collection of <Association>s in NeoJSON Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this NDBFood>>neoJsonOn: neoJSONWriter "custom writing" neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself) i just implement my own writing for my objects. HTH On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk> wrote:
Hello I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint? What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for. Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings? Best wishes Peter Kenny
-- Bernardo E.C. Sent from a cheap desktop computer in South America.
Hi, I use both, NeoJSON and STON and I agreed with Sven here. Having both separated give different entry points for different developers. Data oriented ones would start probably with NeoJSON while devs looking for more general data serialization language for graphs, documents and so on will start with STON. Cheers, Offray On 01/07/16 04:54, Sven Van Caekenberghe wrote:
On 01 Jul 2016, at 11:27, Esteban Lorenzano <estebanlm@gmail.com> wrote:
btw, why NeoJSON is not deprecated in benefit of STON? Although there is obviously a relation between the two (STON is inspired by JSON, STON has some basic backward compatibility with JSON, same project style, same author), there are also differences.
JSON is by definition much simpler and much more limited (no type info for objects, no complex structures like shared and circular references) while STON was designed specifically to cover those.
JSON needs much more explicit mapping support to deal with anything except for simple maps and lists.
In terms of documentation, the API, it would confuse the hell out of regular JSON users to see things mixed up with STON which they are probably not interested in at all. I think this last point might even be the most important one.
Esteban
On 01 Jul 2016, at 08:52, stepharo <stepharo@free.fr> wrote:
Thanks sven
I was thinking to add a section in the chapter but I will wait for your solution.
stef
Le 1/7/16 à 00:56, Sven Van Caekenberghe a écrit :
Hi,
Yes, object properties in JSON are not ordered, by definition. But for humans it can make sense. Converting your domain objects to OrderedDictionaries or overwriting #neoJSONOn: are valid ways to control NeoJSON.
However, I also think that it might be possible to solve the ordering requirement with a regular mapping, provided I make a little internal change - I will investigate that and report back.
Sven
On 30 Jun 2016, at 17:29, PBKResearch <peter@pbkresearch.co.uk> wrote:
Bernardo Thanks for this. After sending my previous mail, I did what I should have done first and looked at list of collection objects in the system browser. So I discovered OrderedDictionary for the first time. I am pleased to see that this is the key to the solution. Peter From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Bernardo Ezequiel Contreras Sent: 30 June 2016 16:17 To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Subject: Re: [Pharo-users] Provide a mapping for a collection of <Association>s in NeoJSON Peter, i had the same issue, i couldn't find the way to do it, but i hack my own writing like this NDBFood>>neoJsonOn: neoJSONWriter "custom writing" neoJSONWriter writeMap: (OrderedDictionary new add: 'ndb_no' -> ndbno; add: 'full_name' -> name; add: 'food_group' -> fg; add: 'factors' -> self factors; add: 'nutrients' -> self nutrients; yourself) i just implement my own writing for my objects. HTH On Thu, Jun 30, 2016 at 12:06 PM, PBKResearch <peter@pbkresearch.co.uk> wrote:
Hello I have recently used NeoJSON for the first time. All went pretty well, but I would like to make a small tweak to the output, which probably involves defining a mapping. I have read through the information in the NeoJSON pdf, and also looked at the class comments, but I am a bit lost. Could Sven or anyone please give me a hint? What I want is to construct a JSON object as a Pharo dictionary, but then control the order in which the fields are output to the JSON file. Clearly the order is irrelevant for a computer, but it could be helpful to a human reader to put them in an order which reflects the way humans think about the objects. The keys of a dictionary are of course unordered, so this is not possible, but if we step back a bit we see that a dictionary is often constructed from a collection of associations; if we could stop at this stage, and have a mapping which outputs the associations in the order in which they appear in the collection, this would give the control I am looking for. Am I talking nonsense, or is there any way of implementing this using NeoJSON mappings? Best wishes Peter Kenny -- Bernardo E.C. Sent from a cheap desktop computer in South America.
On 01 Jul 2016, at 00:56, Sven Van Caekenberghe <sven@stfx.eu> wrote:
However, I also think that it might be possible to solve the ordering requirement with a regular mapping, provided I make a little internal change - I will investigate that and report back.
I committed the following (#bleedingEdge): === Name: Neo-JSON-Core-SvenVanCaekenberghe.34 Author: SvenVanCaekenberghe Time: 2 July 2016, 12:03:08.919979 am UUID: ded70581-c06f-4687-936c-11a029a39ba4 Ancestors: Neo-JSON-Core-SvenVanCaekenberghe.33 Use an OrderedDictionary instead of a regular Dictionary to store the properties inside a NeoJSONObjectMapping - this allows the user to control the order of properties while writing, if needed Add NeoJSONWriterTests>>#testPreservePropertyOrder === Name: Neo-JSON-Tests-SvenVanCaekenberghe.34 Author: SvenVanCaekenberghe Time: 2 July 2016, 12:03:30.417248 am UUID: 883b6ef1-e5fc-4d85-b86c-b21c1b5a3d6c Ancestors: Neo-JSON-Tests-SvenVanCaekenberghe.33 Use an OrderedDictionary instead of a regular Dictionary to store the properties inside a NeoJSONObjectMapping - this allows the user to control the order of properties while writing, if needed Add NeoJSONWriterTests>>#testPreservePropertyOrder === This allows for the following: NeoJSONWriterTests>>#testPreservePropertyOrder | writer testObject | (testObject := NeoJSONTestObject2 new) id: 123; width: 100; height: 50; data: 'test'. writer := [ :object | String streamContents: [ :stream | (NeoJSONWriter on: stream) for: NeoJSONTestObject2 do: [ :mapping | mapping mapInstVars: #(id width height data) ]; nextPut: object ] ]. self assert: (writer value: testObject) equals: '{"id":123,"width":100,"height":50,"data":"test"}'. For any permutation of the list of instance variables, each resulting in differently ordered JSON. This applies to all mapping techniques. Sven
participants (6)
-
Bernardo Ezequiel Contreras -
Esteban Lorenzano -
Offray Vladimir Luna Cárdenas -
PBKResearch -
stepharo -
Sven Van Caekenberghe