Exporting nullable booleans using %JSON.Adaptor
When exporting a %JSON.Adaptor inherited class, undefined %Boolean properties are exported to JSON as "false" instead of null or empty
is there any way to control this behavior?
Class Test.Json extends%JSON.Adaptor
{
Property bool as%Boolean;
}
set test = ##class(Test.Json).%New()
do test.%JSONExport()
{
"bool": false
}Comments
The sample you are posting does not work, %JSON.Adaptor is an abstract class and cannot be instantiated, so the line:
set test = ##class(Test.Json).%New()
returns <METHOD DOES NOT EXIST> error.
In order to instantiate it you need to inherit from a non abstract class like %RegisteredObject, here is my test:
Class Community.TestJSON Extends (%RegisteredObject, %JSON.Adaptor)
{
Property bool As%Boolean;
}Then this is what I get:
set test=##class(Community.TestJSON).%New()
do test.%JSONExport()
{}Another test:
set test=##class(Community.TestJSON).%New()
set test.bool=1do test.%JSONExport()
{"bool":true}I'm using IRIS 2023.3, same as yours.
Maybe you have a different situation than the sample you posted?
My bad, I simplified my sample too much without actually testing it.
I think I found the issue, it seems to happen because I have been using the production test interface, which doesn't seem to allow null values for booleans, unlike strings
.png)
Sorry for the inconvenience