Written by

Développeur at Talan
Question Antoine.Dh · Jan 25, 2024

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

Comments

Enrico Parisi · Jan 25, 2024

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?

0
Antoine.Dh  Jan 25, 2024 to Enrico Parisi

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

Sorry for the inconvenience

0