InsertJSONStructure shouldn't re-sort the structure fields

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

InsertJSONStructure shouldn't re-sort the structure fields

Post by Sicro »

Code: Select all

Structure InfoStruc
  ofield1.i
  zfield2.i
  hfield3.i
EndStructure

Define json
Define info.InfoStruc

json = CreateJSON(#PB_Any)
If json
  InsertJSONStructure(JSONValue(json), @info, InfoStruc)
  Debug ComposeJSON(json, #PB_JSON_PrettyPrint)
  FreeJSON(json)
EndIf
Output is:

Code: Select all

{
  "hfield3": 0,
  "ofield1": 0,
  "zfield2": 0
}
Output should be:

Code: Select all

{
  "ofield1": 0,
  "zfield2": 0,
  "hfield3": 0
}
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: InsertJSONStructure shouldn't re-sort the structure fiel

Post by Marc56us »

I may be wrong, but my understanding is that JSON data is like MAP: it's the system that manages the order of storage whatever structure order.

Try the example of the doc by changing the order: the output will always be the same.
https://www.purebasic.com/documentation ... cture.html
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: InsertJSONStructure shouldn't re-sort the structure fiel

Post by Little John »

By definition a JSON object is an unordered collection of name/value pairs, so this is not a mistake by PureBasic. However, I think PureBasic will be allowed to retain the order. ;-)
I would appreciate that, too. So +1 from me.

I stumbled across the same problem when saving JSON data to a file, and wrote my own code to fix that issue.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: InsertJSONStructure shouldn't re-sort the structure fiel

Post by davido »

+1
DE AA EB
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: InsertJSONStructure shouldn't re-sort the structure fiel

Post by NicTheQuick »

This is definitely a feature request and no bug at all.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: InsertJSONStructure shouldn't re-sort the structure fiel

Post by Sicro »

@Marc56us:
I also realized that the sorting is not random.

@Little John:
Yes, exactly. Whenever the JSON code can be viewed or edited by humans, you want to display the JSON code in a fixed and clear order.
Thanks for your code. I will have a look at it later.

@NicTheQuick:
Right. For me, this is not a bug.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: InsertJSONStructure shouldn't re-sort the structure fields

Post by Quin »

Is there a solution floating around here that can preserve the order of fields, or at least allow me to re-sort them once I get them from the JSON library?
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: InsertJSONStructure shouldn't re-sort the structure fields

Post by spikey »

Quin wrote: Thu Mar 14, 2024 12:55 am Is there a solution floating around here that can preserve the order of fields, or at least allow me to re-sort them once I get them from the JSON library?
InsertJSONStructure and ExtractJSONStructure will "round-trip" properly if used correctly with the same Structure definition. You can ignore the layout in the file - because it's supposed to be order insignificant in the specification. This example still works even though I've changed the input JSON sequence. The order of array members is preserved in the output.

Code: Select all

Structure Person
  Name$
  Age.l
  List Books.s()
EndStructure

Input$ = "{" + Chr(34) + "Age" + Chr(34) + ": 42, " + 
         Chr(34) + "Books" + Chr(34) + ": [" +
         Chr(34) + "Investing For Dummies" + Chr(34) + ", " + 
         Chr(34) + "A Little Bit of Everything For Dummies" + Chr(34) + "] ," + 
         Chr(34) + "Name" + Chr(34) + ": " + Chr(34) + "John Smith" + Chr(34) +
         "}"

ParseJSON(0, Input$)
ExtractJSONStructure(JSONValue(0), @P.Person, Person)

Debug P\Name$
Debug P\Age
Debug ListSize(P\Books())
ForEach P\Books()
  Debug P\Books()
Next
Post Reply