InsertJSONStructure: booleans

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

InsertJSONStructure: booleans

Post by SparrowhawkMMU »

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: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

Post by SparrowhawkMMU »

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: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: InsertJSONStructure: booleans

Post by Fred »

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

Re: InsertJSONStructure: booleans

Post by SparrowhawkMMU »

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

Re: InsertJSONStructure: booleans

Post by Fred »

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

Re: InsertJSONStructure: booleans

Post by SparrowhawkMMU »

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

Re: InsertJSONStructure: booleans

Post by Fred »

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

Re: InsertJSONStructure: booleans

Post by SparrowhawkMMU »

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

Re: InsertJSONStructure: booleans

Post by Fred »

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: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: InsertJSONStructure: booleans

Post by SparrowhawkMMU »

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.
Post Reply