Hi,
would it be possible to provide option to automatically sort instance variables when serializing objects? Either always sorted NeoJSONObjectMapping>>writeObject: anObject on: jsonWriter jsonWriter writeMapStreamingDo: [ :jsonMapWriter | (properties sorted: #propertyName ascending) do: [ :each | each writeObject: anObject on: jsonMapWriter ] ] or put it behind some sortProperties boolean flag. (atm I don't see any downsides in having sort always enabled) Thanks, Peter |
From json.org
"An object is an unordered set of name/value pairs." IOW, it is not in the spec, it would be dangerous to rely up it in any way (not that you are suggesting that). It might look better sometimes, but even then alphabetic sorting might not be the most human friendly or most meaningful. Note that 'properties' in NeoJSONMapping is an OrderedCollection, so it keeps it order. So if you add them in the right order, that would do it. > On 25 Jul 2018, at 11:12, Peter Uhnák <[hidden email]> wrote: > > Hi, > > would it be possible to provide option to automatically sort instance variables when serializing objects? > > Either always sorted > > NeoJSONObjectMapping>>writeObject: anObject on: jsonWriter > jsonWriter writeMapStreamingDo: [ :jsonMapWriter | > (properties sorted: #propertyName ascending) do: [ :each | > each writeObject: anObject on: jsonMapWriter ] ] > > or put it behind some sortProperties boolean flag. (atm I don't see any downsides in having sort always enabled) Speed ? A false suggestion that is part of the spec ? > Thanks, > Peter |
Ah, you are right that it might not be human-friendly. I don't want to rely on the order, but for (file) versioning it is quite valuable. (or rather changing the order can easily make versioning quite worthless). I can manually sort it, but it creates a lot of mess: (NeoJSONWriter on: stream) prettyPrint: true; for: Something do: [ :mapping | mapping mapInstVars: (mapping identifier instVarNames \ #(unwanted)) sorted ]; for: Otherthing do: [ :mapping | mapping mapInstVars: mapping identifier instVarNames sorted ]; for: Point do: [ :mapping | mapping mapInstVars: mapping identifier instVarNames sorted ]; nextPut: anObject. I'd much rather have the following: (NeoJSONWriter on: stream) prettyPrint: true; sorted: true; for: Something do: [ :mapping | mapping mapInstVars: mapping identifier instVarNames \ #(unwanted) ]; mapInstVarsFor: Otherthing; mapInstVarsFor: Point; nextPut: anObject. or more fine-grained, although the above is imo nicer: (NeoJSONWriter on: stream) prettyPrint: true; for: Something do: [ :mapping | mapping mapInstVars: (mapping identifier instVarNames \ #(unwanted)) sorted ]; mapInstVarsFor: Otherthing sorted: true; mapInstVarsFor: Point sorted: true; nextPut: anObject. (or mapSortedInstVarsFor: or something).. Peter On Wed, Jul 25, 2018 at 11:20 AM, Sven Van Caekenberghe <[hidden email]> wrote: From json.org |
> On 25 Jul 2018, at 11:34, Peter Uhnák <[hidden email]> wrote: > > Ah, you are right that it might not be human-friendly. > > I don't want to rely on the order, but for (file) versioning it is quite valuable. (or rather changing the order can easily make versioning quite worthless). Ah, OK, I understand the use case. > I can manually sort it, but it creates a lot of mess: > > (NeoJSONWriter on: stream) > prettyPrint: true; > for: Something > do: [ :mapping | mapping mapInstVars: (mapping identifier instVarNames \ #(unwanted)) sorted ]; > for: Otherthing > do: [ :mapping | mapping mapInstVars: mapping identifier instVarNames sorted ]; > for: Point > do: [ :mapping | mapping mapInstVars: mapping identifier instVarNames sorted ]; > nextPut: anObject. Actually I do not think that the above is that bad ;-) > I'd much rather have the following: > > (NeoJSONWriter on: stream) > prettyPrint: true; > sorted: true; > for: Something > do: [ :mapping | mapping mapInstVars: mapping identifier instVarNames \ #(unwanted) ]; > mapInstVarsFor: Otherthing; > mapInstVarsFor: Point; > nextPut: anObject. > > or more fine-grained, although the above is imo nicer: > > (NeoJSONWriter on: stream) > prettyPrint: true; > for: Something > do: [ :mapping | mapping mapInstVars: (mapping identifier instVarNames \ #(unwanted)) sorted ]; > mapInstVarsFor: Otherthing sorted: true; > mapInstVarsFor: Point sorted: true; > nextPut: anObject. > > (or mapSortedInstVarsFor: or something).. I guess you could add some extension methods to NeoJSONWriter, or subclass it - yourself. After some consideration I still think that adding sorting options would be confusing. Unless there would be some convention in other JSON tools for this issue, or a larger demand (more use cases). > Peter > > On Wed, Jul 25, 2018 at 11:20 AM, Sven Van Caekenberghe <[hidden email]> wrote: > From json.org > > "An object is an unordered set of name/value pairs." > > IOW, it is not in the spec, it would be dangerous to rely up it in any way (not that you are suggesting that). It might look better sometimes, but even then alphabetic sorting might not be the most human friendly or most meaningful. > > Note that 'properties' in NeoJSONMapping is an OrderedCollection, so it keeps it order. So if you add them in the right order, that would do it. > > > On 25 Jul 2018, at 11:12, Peter Uhnák <[hidden email]> wrote: > > > > Hi, > > > > would it be possible to provide option to automatically sort instance variables when serializing objects? > > > > Either always sorted > > > > NeoJSONObjectMapping>>writeObject: anObject on: jsonWriter > > jsonWriter writeMapStreamingDo: [ :jsonMapWriter | > > (properties sorted: #propertyName ascending) do: [ :each | > > each writeObject: anObject on: jsonMapWriter ] ] > > > > or put it behind some sortProperties boolean flag. (atm I don't see any downsides in having sort always enabled) > > Speed ? > A false suggestion that is part of the spec ? > > > Thanks, > > Peter |
Hi Peter,
This functionallity is already available in STON where it preserves the order when you use an OrderedDictionary. And was implemented for the usecase of dealing with fileout and versioning. Perhaps the same could be added to NeoJson so that instead of some magic flag you provide an OrderedDictionary sorted as you would like and it would then preserve the order. If you aim to use it to preserve metada for filetree then there is a patch for that which can be found at https://github.com/DraagrenKirneh/PharoUtilities/tree/master/FileTreeFix Best regards, Henrik -- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html |
> On 25 Jul 2018, at 17:54, Henrik-Nergaard <[hidden email]> wrote: > > Hi Peter, > > This functionallity is already available in STON where it preserves the > order when you use an OrderedDictionary. > And was implemented for the usecase of dealing with fileout and versioning. > Perhaps the same could be added to NeoJson so that instead of some magic > flag you provide an OrderedDictionary sorted as you would like and it would > then preserve the order. You are right, using OrderedDictionaries would fix the order. But I don't think that is what he wants: to fix the order of arbitrary objects being mapped. Even in STON the order of properties of objects is undefined (as it is in JSON). > If you aim to use it to preserve metada for filetree then there is a patch > for that which can be found at > https://github.com/DraagrenKirneh/PharoUtilities/tree/master/FileTreeFix > > Best regards, > Henrik > > > > -- > Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html > |
Just as a comment to Henrik, NeoJSON will already preserve the order of an
OrderedDictionary. When working with Stef on the HTML scraping booklet, I decided to find a way to preserve the order of the selected items, partly for aesthetic reasons, but mainly to demonstrate that the output matched the problem specification. I switched the collection of the output items to an OrderedDictionary, not knowing whether it would work, and it came out as expected. Building an OrderedDictionary of just the required instvars would solve both the OP's problems, though perhaps less elegantly. Peter Kenny -----Original Message----- From: Pharo-users <[hidden email]> On Behalf Of Sven Van Caekenberghe Sent: 25 July 2018 17:49 To: Any question about pharo is welcome <[hidden email]> Subject: Re: [Pharo-users] NeoJSON sorting instance variables/properties > On 25 Jul 2018, at 17:54, Henrik-Nergaard <[hidden email]> wrote: > > Hi Peter, > > This functionallity is already available in STON where it preserves > the order when you use an OrderedDictionary. > And was implemented for the usecase of dealing with fileout and versioning. > Perhaps the same could be added to NeoJson so that instead of some > magic flag you provide an OrderedDictionary sorted as you would like > and it would then preserve the order. You are right, using OrderedDictionaries would fix the order. But I don't think that is what he wants: to fix the order of arbitrary objects being mapped. Even in STON the order of properties of objects is undefined (as it is in JSON). > If you aim to use it to preserve metada for filetree then there is a > patch for that which can be found at > https://github.com/DraagrenKirneh/PharoUtilities/tree/master/FileTreeF > ix > > Best regards, > Henrik > > > > -- > Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html > |
>
Actually I do not think that the above is that bad ;-) It is too explicit for me and easy to forget to do. :) >
I guess you could add some extension methods to NeoJSONWriter, or subclass it - yourself.
In the end, this is probably the most reasonable. And I can change some of the defaults too (like pretty printing & LF line ending). Thanks for the discussion! Peter On Wed, Jul 25, 2018 at 7:21 PM, PBKResearch <[hidden email]> wrote: Just as a comment to Henrik, NeoJSON will already preserve the order of an |
Free forum by Nabble | Edit this page |