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?
Comments
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.
I'm afraid not. I'm using Healthshare Ensemble 2017.2
WOW !!!! IT WORKS !!!!
Thanks for all.
I going to add this method in my utility library
Best regards,
Francisco Lopez
Really nice guys.
I do not have a use for it now, but definitely a really good one
For example so:
Class myapp.jsonProvider Extends %ZEN.Auxiliary.jsonProvider
{
ClassMethod getOrderedProps(
pClass As %Dictionary.CompiledClass,
ByRef pList) [ Internal ]
{
d ##super(pClass,.pList)
s key = "" f { s 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()
{
s myClass = ..%New()
s myClass.property1 = "value 1"
s myClass.property2 = "value 2"
s myClass.property3 = "value 3"
f i=$lb("property3"),$lb("property2","property1") {
s ^||skipProps=i
d ##class(myapp.jsonProvider).%WriteJSONStreamFromObject(.tStream,myClass,,,$$$YES,"ed")
w "skip:",$lts(i),?25," -> ",tStream.Read(),!
}
}
}
USER>d ##class(myapp.myclass).Test()
skip:property3 -> {"property1":"value 1","property2":"value 2"}
skip:property2,property1 -> {"property3":"value 3"}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
Hi Francisco,
You could take a look at this JSON library for Caché, it has a JSONIGNORE for properties, check the docs folder for more details...