Page 1 of 1

InsertJSONStructure: booleans

Posted: Tue Oct 04, 2016 10:32 am
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"

Re: InsertJSONStructure: booleans

Posted: Tue Oct 04, 2016 10:42 am
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

Re: InsertJSONStructure: booleans

Posted: Wed Oct 05, 2016 5:55 am
by Fred
Fair enough, i will take a closer look.

Re: InsertJSONStructure: booleans

Posted: Wed Oct 05, 2016 10:30 am
by SparrowhawkMMU
Fred, many thanks. You are one of the most responsive developers I know of, and it's greatly appreciated.

Re: InsertJSONStructure: booleans

Posted: Thu Oct 06, 2016 12:09 pm
by Fred
Thanks. We do this software for the users, so it's logical to take in account the ideas/remarks

Re: InsertJSONStructure: booleans

Posted: Fri Jun 23, 2017 2:02 pm
by SparrowhawkMMU
@Fred - just wondered whether this was still something you were considering to implement ?

Re: InsertJSONStructure: booleans

Posted: Fri Jun 23, 2017 8:51 pm
by Fred
Yes

Re: InsertJSONStructure: booleans

Posted: Sat Jun 24, 2017 3:27 pm
by SparrowhawkMMU
Great, thank you!

Re: InsertJSONStructure: booleans

Posted: Wed Sep 27, 2017 7:33 am
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.

Re: InsertJSONStructure: booleans

Posted: Wed Sep 27, 2017 2:09 pm
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.