Page 1 of 1

json bool

Posted: Mon Mar 17, 2025 6:11 pm
by Garfield
The following simple example:

Code: Select all

Structure json_
  test1.b
  test2.b
EndStructure

Define json.json_

json\test1 = #True
json\test2 = #False

js = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(js), @json, json_)
Debug ComposeJSON(js, #PB_JSON_PrettyPrint)
the output should be:
{
"test1": true,
"test2": false
}
but it is
{
"test2": 0,
"test1": 1
}
My two questions:

Why is bool interpreted wrong? In Json 0 and 1 are numbers and not booleans.

Why is the output not in the order of how it was declared the structure?

Re: json bool

Posted: Mon Mar 17, 2025 6:32 pm
by mk-soft
With PureBasic, the variable type Boolean does not exist.

Variable var.b is a signed byte ...

Re: json bool

Posted: Mon Mar 17, 2025 6:47 pm
by Garfield
Yes, but byte is no data type in Json. (only string, number, bool, array and object)

So I thought, it would be interpreted as boolean.

Is there a good way to get the right output from insert/compose?

Or do I have to do a dirty way with defining it as string in structure and do a replacement after compose?

Re: json bool

Posted: Mon Mar 17, 2025 7:14 pm
by Little John
@Garfield

You can use SetJSONBoolean().

As far as the order is concerned:
InsertJSONStructure() creates a JSON object, and the order of members of a JSON object is not defined. You can use Save JSON data with object members well-arranged to set their order.

Re: json bool

Posted: Tue Mar 18, 2025 6:19 pm
by HeX0R
I came accross that also in the past, it removes the ability to easily extract/insert structures, when boolean values are needed (for external APIs e.g.).
Would be cool, if we had some flag for extract/insertstructure, like #PB_JSON_BYTES_EQUAL_BOOLEAN or similar

Re: json bool

Posted: Tue Mar 18, 2025 6:26 pm
by Little John
HeX0R wrote: Tue Mar 18, 2025 6:19 pm Would be cool, if we had some flag for extract/insertstructure, like #PB_JSON_BYTES_EQUAL_BOOLEAN or similar
+1