How to ignore serialize a property in a class to JSon

Hi all,

I have a class that I want to serialize to JSon. So I'm using the object %ZEN.Auxiliary.jsonProvider)

set myClass = ##class(myapp.myclass).%New()

set myClass.property1 ="value 1"

set myClass.property2 = "value 2"

set myClass.property3 = "value 3"

do ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.tStream,myClass ,,,1,"ed")

write tStream.Read()

By definition of myclass, all properties are serialized, that's fine. But I want not serialize the property3. I think is using a XML attribute in the property, or something about. But I didn't find anything about.

 

Anyone can help me?

 

Answers

For example so:

Class myapp.jsonProvider Extends %ZEN.Auxiliary.jsonProvider
{

ClassMethod getOrderedProps(
  pClass As %Dictionary.CompiledClass,
  ByRef pList) [ Internal ]
{
  ##super(pClass,.pList)

  key "" key $o(pList(key))  q:""=key
    zk:$lf(^||skipProps,pList(key)) pList(key)
  }
}

}

Class myapp.myclass Extends %RegisteredObject
{

Property property1 As %String;

Property property2 As %String;

Property property3 As %String;

/// d ##class(myapp.myclass).Test()
ClassMethod Test()
{
  myClass = ..%New()
  myClass.property1 "value 1"
  myClass.property2 "value 2"
  myClass.property3 "value 3"
  
  i=$lb("property3"),$lb("property2","property1"{
    ^||skipProps=i
    ##class(myapp.jsonProvider).%WriteJSONStreamFromObject(.tStream,myClass,,,$$$YES,"ed")
    "skip:",$lts(i),?25," -> ",tStream.Read(),!
  }
}

}

USER>##class(myapp.myclass).Test()
skip:property3            -> {"property1":"value 1","property2":"value 2"}
skip:property2,property1  -> {"property3":"value 3"}

WOW !!!! IT WORKS !!!!

Thanks for all.

I going to add this method in my utility library

Best regards,

Francisco Lopez

Hi Francisco!

Very curious of your utility library - do you want to share some on Open Exchange and Github? )

I'm thinking to create a framework with some utilities for Rest, utils functionst, etc... When it works fine, I'll do

Really nice guys. 

I do not have a use for it now, but definitely a really good one

If you use InterSystems IRIS 2019.2 you can extend from %JSON.Adaptor class, which is similar to XML adaptor and allows property parameters to customize serialization behaviour. In your case 

%JSONINCLUDE=NONE

  %ZEN.Auxiliary.jsonProvider skips % and private properties with some flags. You can use that.