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"}Comments
- 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 = 0 ];
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 = 1 ]
{
Quit ..somethingLogicalToDisplay(..something)
}
}
I appreciate the suggestion. However this would only work for a JSON Export and not a JSON Import.
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: 2That would work and is a clean solution. This is what I expected from the the framework.