Question
· May 9

Using %JSON.Adaptor with %Integer value using the DISPLAYLIST VALUELIST parameters

Is there a way to use the %JSON.Adaptor to project readable JSON when a property is defined as a %Integer but with a DISPLAYLIST ? 

 

Class User.CExampleJSON Extends (%RegisteredObject, %JSON.Adaptor, %XML.Adaptor)
{

Property something As %Integer(DISPLAYLIST = ",OK,Error,Warning", VALUELIST = ",0,1,2") [ InitialExpression = 0 ];

ClassMethod RunMe()
{
      set obj = ..%New()
      set obj.something = 2

      do obj.%JSONExportToString(.string)
      write "JSON : " _ string,!!!

      write "Content  : " _ ..somethingLogicalToDisplay(obj.something),!
}

}

Which gave on the output : 
 

JSON : {"something":2}

Content  : Warning



I would prefer :

JSON : {"something":"Warning"}
Product version: IRIS 2023.1
$ZV: IRIS for Windows (x86-64) 2023.1.3 (Build 517U) Wed Jan 10 2024 13:36:58 EST
Discussion (4)2
Log in or sign up to continue
  1. Hi.

Maybe you should use calculated property - something like this:

Class User.CExampleJSON Extends (%RegisteredObject, %JSON.Adaptor, %XML.Adaptor)
{
Property something As %Integer(%JSONINCLUDE = "NONE", DISPLAYLIST = ",OK,Error,Warning", VALUELIST = ",0,1,2") [ InitialExpression = ];

ClassMethod RunMe()
{
      set obj = ..%New()
      set obj.something = 2       
      do obj.%JSONExportToString(.string)
      write "JSON : " _ string,!!!
      write "Content : " _ ..somethingLogicalToDisplay(obj.something),!
}

Property somethingDisplay As %String(%JSONFIELDNAME = "something") [ Calculated ]; Method somethingDisplayGet() As %String [ ServerOnly = ]
{
Quit ..somethingLogicalToDisplay(..something)
}

}

I'd implement a custom datatype, something like:

Class Community.dt.IntJSON Extends %Integer
{

Parameter JSONTYPE = "string";
ClassMethod JSONToLogical(%val As %String) As %Integer [ CodeMode = expression, ServerOnly = 1 ]
{
..DisplayToLogical(%val)
}

ClassMethod LogicalToJSON(%val As %Integer) As %String [ CodeMode = expression, ServerOnly = 1 ]
{
..LogicalToDisplay(%val)
}

}

Then in your class:

Class Community.json.TestDT Extends (%RegisteredObject, %JSON.Adaptor)
{

Property something As Community.dt.IntJSON(DISPLAYLIST = ",OK,Error,Warning", VALUELIST = ",0,1,2") [ InitialExpression = 0 ];

ClassMethod RunMe()
{
      set obj = ..%New()
      set obj.something = 2
      do obj.%JSONExportToString(.string)
      write "JSON : " _ string,!

      write "Content  : " _ ..somethingLogicalToDisplay(obj.something),!!

      set obj2=..%New()
      do obj2.%JSONImport(string)
      write "Imported something value: ",obj2.something,!
}

}

Result:

EPTEST>d ##class(Community.json.TestDT).RunMe()
JSON : {"something":"Warning"}
Content  : Warning
 
Imported something value: 2