[Pharo-users] Is it bad to have an order NeoJsonObject?

Tim Mackinnon tim at testit.works
Mon Jun 11 04:19:06 EDT 2018


Yes i was a bit nervous about the ordering, but had also noticed other examples like this - but it is a good reminder for me to double check with the developers that they consciously have done this and to register an interest that it stays that way (particularly as they strangely don’t provide access to a schema for your user type definition - and hence could get the ordering from that).

On a second note - I did phase out my use of NeoJsonObject after the community warning (and realised how much it had worked into my solution). I think explicitly handling the absent key is better (I do loathe nil checks and am surprised how many languages set this terrible example - of shocked how easily I fell into that trap too). That said - I did miss the #atPath:  concept in NeoJson and ended up adding an equivalent (better?) #atPath:ifAbsent: to OrderedDictionary to cope with needing to reach into structures to pull out relevant attributes.

It did then remind how much I wish we could sort out some form of namespacing, as I don’t like to see my model code have to put prAtPath:ifAbsent: (it’s just gross). Maybe I might subclass OrderedDictionary to get the namespacing on a class and my methods can be safely clean).

Appreciate the commentary on this, it’s been a useful exercise.

Tim

Sent from my iPhone

> On 11 Jun 2018, at 08:02, Norbert Hartl <norbert at hartl.name> wrote:
> 
> 
> 
>> Am 11.06.2018 um 08:55 schrieb Richard O'Keefe <raoknz at gmail.com>:
>> 
>> The order of key:value pairs in a JSON "object"
>> is really NOT supposed to matter.  For example,
>> if you put {"a":1,"b":2} into a JSON database
>> you should not be surprised to get {"b":2,"a":1}
>> back.  Or vice versa, of course.
> But when it does what do you do? It is pretty clear that the order is important for that format. So saying it shouldn’t matter does not make it better. If you work with mongo DB you will see the same that for some queries you have to provide proper order of keys.
> 
> Norbert
> 
>>> On 8 June 2018 at 00:16, Tim Mackinnon <tim at testit.works> wrote:
>>> Thanks guys - it seems like I was on the right track.
>>> 
>>> Making my own NeoJsonObject did work, but I will go back and see if I’m really using the js nature of the values and if for this, just an OrderedDictionary might be clearer.
>>> 
>>> As an aside, I was doing some experiments with prismic.io (a headless cms) and the order of your content when rendering it on a page(generically) is obviously important.
>>> 
>>> Tim
>>> 
>>> Sent from my iPhone
>>> 
>>> > On 7 Jun 2018, at 07:46, Sven Van Caekenberghe <sven at stfx.eu> wrote:
>>> > 
>>> > 
>>> > 
>>> >> On 7 Jun 2018, at 08:16, Norbert Hartl <norbert at hartl.name> wrote:
>>> >> 
>>> >> 
>>> >> 
>>> >>> Am 07.06.2018 um 07:29 schrieb Sven Van Caekenberghe <sven at stfx.eu>:
>>> >>> 
>>> >>> Tim,
>>> >>> 
>>> >>>> On 7 Jun 2018, at 01:37, Tim Mackinnon <tim at testit.works> wrote:
>>> >>>> 
>>> >>>> Hi - I’ve hit some Json where the outputted values (they are field names) are written out in a specific order - and the author hasn’t chosen to use an array to represent those fields in a specific order.
>>> >>>> 
>>> >>>> { ‘field1’ : { ….}, ‘field2’: { … } }
>>> >>>> 
>>> >>>> I think this is technically incorrect and should be:
>>> >>>> 
>>> >>>> { [ {‘field1’ : { ….}, ‘field2’: { … } ]}
>>> >>>> 
>>> >>>> Anyway - given what I’ve got, would it be a terrible idea to create my own version NeoJsonObject and just make it a subclass of OrderedDictionary?
>>> >>>> 
>>> >>>> I can’t see any issues - but then I might be kidding myself…
>>> >>>> 
>>> >>>> Tim
>>> >>> 
>>> >>> Yea, I think you could try making a NeoJSONOrderedObject as a subclass of OrderedDictionary, modelled after NeoJSONObject. Just remember it is implemented using DNU, which will surprise you at one point.
>>> >>> 
>>> >> Wouldn‘t be setting #mapClass: on the reader to OrderedDictionary not enough? Dictionary is the default anyway.
>>> >> 
>>> >> Norbert
>>> > 
>>> > Yes, of course, Norbert, it keeps the order:
>>> > 
>>> > (NeoJSONReader on: '{"BB":1,"CC":2,"AA":3}' readStream) mapClass: OrderedDictionary; next.
>>> > 
>>> >  => an OrderedDictionary('BB'->1 'CC'->2 'AA'->3)
>>> > 
>>> > NeoJSONWriter toString: (OrderedDictionary new at: #BB put: 1; at: #CC put: 2; at: #AA put: 3; yourself). 
>>> > 
>>> >  => '{""BB"":1,""CC"":2,""AA"":3}'
>>> > 
>>> > It is not functionally 100% the same as a hypothetical NeoJSONOrderedObject, but might do.
>>> > 
>>> > Sven
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/attachments/20180611/8c4d3db2/attachment.html>


More information about the Pharo-users mailing list