InsertJSONStructure: booleans
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
InsertJSONStructure: booleans
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"
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.
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: InsertJSONStructure: booleans
For clarity, here is some sample code:
Which outputs:
Ideally, there would be some way for the code above to produce this instead:
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
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
Code: Select all
{
"myString": "A String",
"myInteger": 12345,
"myBoolTrue": 1,
"myBoolFalse": 0
}
Code: Select all
{
"myString": "A String",
"myInteger": 12345,
"myBoolTrue": true,
"myBoolFalse": false
}
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.
Re: InsertJSONStructure: booleans
Fair enough, i will take a closer look.
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: InsertJSONStructure: booleans
Fred, many thanks. You are one of the most responsive developers I know of, and it's greatly appreciated.
Re: InsertJSONStructure: booleans
Thanks. We do this software for the users, so it's logical to take in account the ideas/remarks
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: InsertJSONStructure: booleans
@Fred - just wondered whether this was still something you were considering to implement ?
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: InsertJSONStructure: booleans
Great, thank you!
Re: InsertJSONStructure: booleans
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.
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: InsertJSONStructure: booleans
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.
OK for now I'll just keep using manual JSON construction.