Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

InsertJSONStructure: booleans

by SparrowhawkMMU Tue Oct 04, 2016 10:32 am

At the moment we cannot specify values as boolean when added to a JSON object using InsertJSONStructure() as they are always added as integer values

Suggestions:

1) new data type: boolean which has an integer range of 0 to 1 (ie false, true). Thus functions would know when to convert a value to boolean implicitely. Not sure how massive a job that is under the hood -
or
2) extend the InsertJSONStructure() and other similar functions to accept a new flag, eg #PB_JSON_ByteToBoolean which takes byte type values and converts them to JSON boolean true and false - without this flag bytes would be treated as normal of course

Rationale: APIs are usually strict things (with good reason). So if they specify a true/false value but get 1/0, they will probably fail.

The current work around is to insert each element from a structure one by one and using the JSON library's SetJSONBoolean() function, which involves a LOT more coding

Unless of course I have missed something and there is a way of doing this already with the existing commands?

Edit: renamed suggested new constant to include the word "JSON"
Last edited by SparrowhawkMMU on Wed Oct 05, 2016 10:34 am, edited 1 time in total.
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

by SparrowhawkMMU Tue Oct 04, 2016 10:42 am

For clarity, here is some sample code:

Code: Select all
EnableExplicit

Structure MyStructure
   myString.s
   myInteger.i
   myBoolTrue.b
   myBoolFalse.b
EndStructure

Define myVar.MyStructure

With myVar
   \myString    = "A String"
   \myInteger   = 12345
   \myBoolTrue  = #True
   \myBoolFalse = #False
EndWith   

If CreateJSON(0)

   InsertJSONStructure(JSONValue(0), @myVar, MyStructure)
   
   ClearStructure(@myVar, MyStructure)
   
   Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
   
   FreeJSON(0)

EndIf


Which outputs:
Code: Select all
{
   "myString": "A String",
   "myInteger": 12345,
   "myBoolTrue": 1,
   "myBoolFalse": 0
}


Ideally, there would be some way for the code above to produce this instead:
Code: Select all
{
   "myString": "A String",
   "myInteger": 12345,
   "myBoolTrue": true,
   "myBoolFalse": false
}


Edit: fixed typos
Edit 2: explicitly set boolean values in the structure to #True and #False rather than 1 or 0 to make the point clearer
Last edited by SparrowhawkMMU on Wed Oct 05, 2016 10:33 am, edited 1 time in total.
Fred
Site Admin
 
Posts: 1018
Joined: Mon Feb 24, 2014 10:51 am

Re: InsertJSONStructure: booleans

by Fred Wed Oct 05, 2016 5:55 am

Fair enough, i will take a closer look.
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

by SparrowhawkMMU Wed Oct 05, 2016 10:30 am

Fred, many thanks. You are one of the most responsive developers I know of, and it's greatly appreciated.
Fred
Site Admin
 
Posts: 1018
Joined: Mon Feb 24, 2014 10:51 am

Re: InsertJSONStructure: booleans

by Fred Thu Oct 06, 2016 12:09 pm

Thanks. We do this software for the users, so it's logical to take in account the ideas/remarks
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

by SparrowhawkMMU Fri Jun 23, 2017 2:02 pm

@Fred - just wondered whether this was still something you were considering to implement ?
Fred
Site Admin
 
Posts: 1018
Joined: Mon Feb 24, 2014 10:51 am

Re: InsertJSONStructure: booleans

by Fred Fri Jun 23, 2017 8:51 pm

Yes
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

by SparrowhawkMMU Sat Jun 24, 2017 3:27 pm

Great, thank you!
Fred
Site Admin
 
Posts: 1018
Joined: Mon Feb 24, 2014 10:51 am

Re: InsertJSONStructure: booleans

by Fred Wed Sep 27, 2017 7:33 am

Just to let you know, I gave it a try but unfortunately we don't store structure types for now so it's not possible to tell if it's a long or a byte without doing a compiler change. I will postpone this then until I find a satisfacting solution.
User avatar
SparrowhawkMMU
 
Posts: 267
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

by SparrowhawkMMU Wed Sep 27, 2017 2:09 pm

Thanks for trying Fred. I did suspect that it would be tricky to implement. I appreciate you taking a look.

OK for now I'll just keep using manual JSON construction.
Return to Feature Requests and Wishlists

Who is online

Users browsing this forum: No registered users and 1 guest