JSON Bug or Feature?

Just starting out? Need help? Post your questions and find answers here.
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

JSON Bug or Feature?

Post by HeX0R »

This is the first time I'm playing with JSON, it seems to be perfect for my needs in my current project.

I will have a quite huge JSON file in the end, and the idea is to let customers change this file later.
But for this it is important to keep the structure, otherwise the data looks quite chaotic on a first look.

Example code:

Code: Select all

Structure _SLAVE_INFO_
	Version.s
	Locales.s
	Deviceclass.s
	Devicename.s
	Codenumber.i
	Manufacturername.s
	Codenumber_Offset.i
	PasswordID.i
	ServiceName.s
	FactoryCode.i
EndStructure


Global NewList Groupnames.s()
Global GL._SLAVE_INFO_

AddElement(Groupnames()) : Groupnames() = "Product ID"
AddElement(Groupnames()) : Groupnames() = "Operation"
AddElement(Groupnames()) : Groupnames() = "Failure"
AddElement(Groupnames()) : Groupnames() = "Measurement Setup"
AddElement(Groupnames()) : Groupnames() = "I/O And Com. Setup"
AddElement(Groupnames()) : Groupnames() = "Service"
AddElement(Groupnames()) : Groupnames() = "Basic Data logger 1"
AddElement(Groupnames()) : Groupnames() = "Basic Data logger 2"
AddElement(Groupnames()) : Groupnames() = "Advanced information"
AddElement(Groupnames()) : Groupnames() = "Option"



Procedure main()
	Protected OV, FileInfo, Group, Groups, Languages
	
	If CreateJSON(0)
		OV       = SetJSONObject(JSONValue(0))
		FileInfo = AddJSONMember(OV, "FileInfo")
		InsertJSONStructure(FileInfo, @GL, _SLAVE_INFO_)
		
		Groups = AddJSONMember(OV, "Groups")
		SetJSONObject(Groups)
		
		ForEach Groupnames()
			Group = AddJSONMember(Groups, Groupnames())
		Next
		
		Languages = AddJSONMember(OV, "Languages")

		Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
		FreeJSON(0)
	EndIf
EndProcedure

main()
If you look at the output, the structure looks quite disordered.
The fileinfo structure order is completely different to my structure _SLAVE_INFO_ and the groupnames order doesn't reflect the order I added them.

I know, that it doesn't make any difference in the end, any JSON parser will be able to parse it successfully.
But the JSON file looks very unstructured and it will be difficult for customers to find the correct groupname and setting.
Is it somehow possible to keep the order?
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: JSON Bug or Feature?

Post by kenmo »

In PB's JSON lib, Arrays keep their order (a JSON requirement)
and Objects don't (maybe Objects use a PB Map internally?)

So you can fix your groupname order easily:

Code: Select all

Groups = AddJSONMember(OV, "Groups")
SetJSONArray(Groups)
      
ForEach Groupnames()
   SetJSONString(AddJSONElement(Groups), Groupnames())
Next
But I don't think there's a way currently to control the order of object members...
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: JSON Bug or Feature?

Post by Little John »

@HeXOR:
It is not a bug, since a JSON object by definition is an unordered collection of name/value pairs.

I also stumbled across that problem, and this module which allows to control the order of the saved object members solves the issue for me.
This is your original code, with 5 additional lines:

Code: Select all

XIncludeFile "jsave.pbi"   ; new


Structure _SLAVE_INFO_
   Version.s
   Locales.s
   Deviceclass.s
   Devicename.s
   Codenumber.i
   Manufacturername.s
   Codenumber_Offset.i
   PasswordID.i
   ServiceName.s
   FactoryCode.i
EndStructure


Global NewList Groupnames.s()
Global GL._SLAVE_INFO_

AddElement(Groupnames()) : Groupnames() = "Product ID"
AddElement(Groupnames()) : Groupnames() = "Operation"
AddElement(Groupnames()) : Groupnames() = "Failure"
AddElement(Groupnames()) : Groupnames() = "Measurement Setup"
AddElement(Groupnames()) : Groupnames() = "I/O And Com. Setup"
AddElement(Groupnames()) : Groupnames() = "Service"
AddElement(Groupnames()) : Groupnames() = "Basic Data logger 1"
AddElement(Groupnames()) : Groupnames() = "Basic Data logger 2"
AddElement(Groupnames()) : Groupnames() = "Advanced information"
AddElement(Groupnames()) : Groupnames() = "Option"



Procedure main()
   Protected OV, FileInfo, Group, Groups, Languages
   
   If CreateJSON(0)
      OV       = SetJSONObject(JSONValue(0))
      FileInfo = AddJSONMember(OV, "FileInfo")
      InsertJSONStructure(FileInfo, @GL, _SLAVE_INFO_)
      
      Groups = AddJSONMember(OV, "Groups")
      SetJSONObject(Groups)
      
      ForEach Groupnames()
         Group = AddJSONMember(Groups, Groupnames())
      Next
      
      Languages = AddJSONMember(OV, "Languages")
      
      Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
      
      Debug "------------------------------------"             ; new
      JSave::InitObjectStr("", "Groups, FileInfo, Languages")  ; new
      JSave::InitObject("Groups", Groupnames())                ; new
      JSave::Save(0, "")                                       ; new
      
      FreeJSON(0)
   EndIf
EndProcedure

main()
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: JSON Bug or Feature?

Post by HeX0R »

Shit, I seem to have completely overlooked that :shock:

Thanks bro, I still can make good use of this!
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: JSON Bug or Feature?

Post by skywalk »

HeX0R wrote:This is the first time I'm playing with JSON, it seems to be perfect for my needs in my current project.
I will have a quite huge JSON file in the end, and the idea is to let customers change this file later.
[Code omitted...]
If you look at the output, the structure looks quite disordered.
The fileinfo structure order is completely different to my structure _SLAVE_INFO_ and the groupnames order doesn't reflect the order I added them.

I know, that it doesn't make any difference in the end, any JSON parser will be able to parse it successfully.
But the JSON file looks very unstructured and it will be difficult for customers to find the correct groupname and setting.
Is it somehow possible to keep the order?
I am curious why you cannot deliver your large data as a SQLite(or some other flavor) database? That can be edited in many data browsers(or your own code) without the human limitations of text based browsing large files.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: JSON Bug or Feature?

Post by HeX0R »

In general this file should stay as is, only experienced users can change this or that.
And I like the possibility to save my whole structured list with nested lists and maps with almost just one command.
Loading it again is as easy as saving it.
I don't want to mess with a database just to make it more comfortable for experienced users ;)
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: JSON Bug or Feature?

Post by Little John »

Hi HeX0R,

you are most welcome! I'm glad that the code is useful for you.
I've just posted a new version of the module, which contains a bug fix.
Post Reply