It is currently Wed May 27, 2020 5:17 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: JSON Bug or Feature?
PostPosted: Sun Apr 02, 2017 8:41 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Sep 20, 2004 7:12 am
Posts: 565
Location: Hell
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:
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?

_________________
Link dead?
Change h3x0r.ath.cx into hex0rs.coderbu.de and all will be fine.


Top
 Profile  
Reply with quote  
 Post subject: Re: JSON Bug or Feature?
PostPosted: Thu Apr 13, 2017 7:52 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1801
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:
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...

_________________
On GitHub: PB Includes - IDE Tools - Color Themes - IDE Branches


Top
 Profile  
Reply with quote  
 Post subject: Re: JSON Bug or Feature?
PostPosted: Tue Sep 05, 2017 8:04 pm 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3845
Location: Berlin, Germany
@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:
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()

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: JSON Bug or Feature?
PostPosted: Tue Feb 13, 2018 11:42 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Sep 20, 2004 7:12 am
Posts: 565
Location: Hell
Shit, I seem to have completely overlooked that :shock:

Thanks bro, I still can make good use of this!

_________________
Link dead?
Change h3x0r.ath.cx into hex0rs.coderbu.de and all will be fine.


Top
 Profile  
Reply with quote  
 Post subject: Re: JSON Bug or Feature?
PostPosted: Wed Feb 14, 2018 12:28 am 
Online
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3266
Location: Boston, MA
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


Top
 Profile  
Reply with quote  
 Post subject: Re: JSON Bug or Feature?
PostPosted: Wed Feb 14, 2018 8:01 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Sep 20, 2004 7:12 am
Posts: 565
Location: Hell
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 ;)

_________________
Link dead?
Change h3x0r.ath.cx into hex0rs.coderbu.de and all will be fine.


Top
 Profile  
Reply with quote  
 Post subject: Re: JSON Bug or Feature?
PostPosted: Thu Feb 15, 2018 6:16 am 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3845
Location: Berlin, Germany
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.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye