Question
· 4 hr ago

%JSON.Adaptor don't clear "list of" property

Hi,

I'm having a problem with %JSON.Adaptor and "list of" binding.

For exemple, this class with a property "oazisCode As list of %String".

Class User.ADGroup Extends (%Persistent, %JSON.Adaptor)

{
/// Code interne
Property code As %String;

/// Libellé
Property text As %String(MAXLEN = "");

Property oazisCode As list Of %String;

}

The first time call to %JSONImport works properly

s r = ##class(User.ADGroup).%New()
s json = {"code": "123", "text":"456", "oazisCode": ["1","2"]}
d r.%JSONImport(json)
w r.oazisCode.Size
2

If I call %JSONIMport a second time with another json , the "oazisCode" property is not cleared and new values are added to the list

s json = {"code": "123", "text":"456", "oazisCode": ["4","5"]}
d r.%JSONImport(json)
w r.oazisCode.Size
4

If I try to clear the property by passing an empty array, it does nothing

s json = {"code": "123", "text":"456", "oazisCode": []}
d r.%JSONImport(json)
w r.oazisCode.Size
4

It seems to be a problem with "GenerateImportInternal" from %JSON.Generator class wich never call "Clear" method of Collection properties.

Is there a workaround or something to do to clear a "list of" property ?

Regards

Product version: IRIS 2024.1
$ZV: IRIS for Windows (x86-64) 2024.1 (Build 267_2U) Tue Apr 30 2024 16:35:10 EDT
Discussion (5)2
Log in or sign up to continue

Finaly "solved" with a call to "Clear" in the generic class.

Method bind(pModel As %DynamicObject, Output sc As %Status = {$$$OK}) [ CodeMode = objectgenerator ]
{
  set kProp = ""
  set prop = %compiledclass.Properties.GetNext(.kProp)
  while kProp '= "" {
    if prop.Collection = "list" {
      do %code.WriteLine(" do .."_prop.Name_".Clear()")
    }
    set prop = %compiledclass.Properties.GetNext(.kProp)
  }
  do %code.WriteLine(" set sc = ..%JSONImport(pModel)")
  do %code.WriteLine(" return")
  return $$$OK
}

Thank you for your inspiration ;-)

Regards