Removing properties from a ZENProxyObject before it is returned by a REST service
Hi
I have been experimenting with the creation of a set of REST services for an app. The basic GET operation is set to create a %ZEN.proxyObject instance, and then set an instance of a Persistant class as a property, which gives me all of the values I want to return. However, it also gives me some values that I don't want to return over REST (because they are both private, and large registered objects which will bog down performance)
How do I remove these values from the ZENproxyObject? I can only see a full clear() in the documentation, but nothing which can remove individual elements
Thanks
Chris
Comments
By default %ToJSON method prints empty properties.
If you pass pFormat without "e" flags (that is passed by default), then empty properties are skipped:
USER>set p = ##class(%ZEN.proxyObject).%New()
USER>set p.a = 1
USER>set p.b = 2
USER>do p.%ToJSON()
{
"a":1,
"b":2
}
USER>set p.b = ""
USER>do p.%ToJSON()
{
"a":1,
"b":""
}
USER>do p.%ToJSON(,"alotw")
{
"a":1
}
I encourage you to use Caché 2016.1 with native JSON support. Don't start with %ZEN.proxyObject. See great article by Stefan Wittman about JSON support in 2016.1: https://community.intersystems.com/post/introducing-new-json-capabilities-cach%C3%A9-20161
There is also this way:
set p = ##class(%ZEN.proxyObject).%New()
set p.a = 1
set p.b = 2
do p.%ToJSON()
kill p.%data("b")
do p.%ToJSON()kill p.%data("b") would completely remove b property. But better use Caché 2016.1 with native JSON support.
Fantastic. Thank you both for your help. I will migrate over to using %Object instead