Question André-Claude Gendron · May 9, 2024

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 = 2do 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

Comments

Matjaz Murko · May 9, 2024
  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)
}

}

0
André-Claude Gendron  May 10, 2024 to Matjaz Murko

I appreciate the suggestion. However this would only work for a JSON Export and not a JSON Import.

0
Enrico Parisi · May 11, 2024

I'd implement a custom datatype, something like:

Class Community.dt.IntJSON Extends%Integer
{

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

ClassMethod LogicalToJSON(%valAs%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 = 2do 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
0
André-Claude Gendron  May 13, 2024 to Enrico Parisi

That would work and is a clean solution. This is what I expected from the the framework.

0