Published on InterSystems Developer Community (https://community.intersystems.com)

Home > %JSON.Adaptor with class that inherit from %ListOfObjects

Discussion
Javier Llobet · Jul 8, 2020

%JSON.Adaptor with class that inherit from %ListOfObjects

I was trying the %JSON.Adapter backport to cache and try it also in IRIS and i found a kind of an issue regarding to export and object to json that is an inheritance of %ListOfObjects
If we have a list of elementtype of and object and then we try to export this to a json we get this json instead of a correct json.

exported json:

{
  "ElementType":"test.element",
  "Size":"1"
}

Json that we want:

[
  {
    "field1":"testField1",
    "field2":"testField2"
  }
]

Test classes:

Class test.elementList Extends (%ListOfObjects, %ZJSON.Adaptor)
{ 
 /// The type (class name) of the elements stored in the collection.
 Parameter ELEMENTTYPE = "test.element";
}
Class test.element Extends (%RegisteredObject, %ZJSON.Adaptor)
{ 
 Property field1 As %String [ InitialExpression = "testField1" ];
 Property field2 As %String [ InitialExpression = "testField2" ];
}

Class test.Main Extends %RegisteredObject
{ 
ClassMethod Run()
{
  Set elem = ##class(test.element).%New()
  Set elemList = ##class(test.elementList).%New()
  Do elemList.Insert(elem)
  Do elemList.%JSONExportToString(.json)
  Do ##class(%ZJSON.Formatter).%New().Format(json)
 } 
}

I could fix it overwritting the %JSONExportToString in the elementList class in order to loop over elements and so on...but can be possible to fix this in IRIS? or is there any other solution?

Thanks !

#JSON #Caché #InterSystems IRIS

Source URL:https://community.intersystems.com/post/jsonadaptor-class-inherit-listofobjects