Question
· Jan 25

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
}
Product version: IRIS 2023.3
Discussion (2)2
Log in or sign up to continue

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=1
do test.%JSONExport()
{"bool":true}

I'm using IRIS 2023.3, same as yours.

Maybe you have a different situation than the sample you posted?