Question
· Mar 24, 2017

Convert to JSON string

here is my $ZV string "Cache for Windows (x86-64) 2016.2.1 (Build 803) Wed Oct 26 2016 12:43:35 EDT"

 two questions

1. How do i convert any Persistent object to JSON String ?

2. How do i convert an y Register object to JSON string ?

3. Do i need to extend from any adaptor class for 1 & 2 ?

Discussion (11)0
Log in or sign up to continue

%ToJSON() is only defined on objects extending %DynamicAbstractObject.
For example:

SAMPLES>s obj={"test":"42"}
SAMPLES>d obj.%ToJSON()
{"test":"42"}
SAMPLES>

To serialize arbitrary %Persistent/%Registered objects you'll have to use altJSONProvider
for example:

SAMPLES>s p=##class(Sample.Person).%OpenId(1)
SAMPLES>d ##class("%ZEN.Auxiliary.altJSONProvider").%WriteJSONFromObject(,.p)
{"_class":"Sample.Person","_id":1,"Name":"Love,Wolfgang A.","SSN":"917-96-1802","DOB":45275,"Home":{"_class":"Sample.Address","Street":"8360 First Street","City":"Zanesville","State":"WV","Zip":25788},"Office":{"_class":"Sample.Address","Street":"1842 Maple Court","City":"Hialeah","State":"NH","Zip":65893},"Spouse":"","FavoriteColors":[],"Age":"52"}