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 ?

  • 0
  • 0
  • 1553
  • 8
  • 3

Answers

Bapu,

have a look at this article, it's explaining everything:

https://community.intersystems.com/post/introducing-new-json-capabilitie...

and this one explains the updates that have been made to the syntax: https://community.intersystems.com/post/json-changes-cach%C3%A9-20162

Cheers,
Fab

Hello Fab,

thanks for reply. Looks like in 2016.2, it has changed. I keep getting error when i execute

do myObject.$toJSON()

i tried to %ToJSON() as well. i get following error - <METHOD DOES NOT EXIST> *%ToJSON

%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"}

When I attempt to use "%ZEN.Auxiliary.altJSONProvider").%WriteJSONFromObject() on a rather large Object (non-dynamic) I receive a FRAMESTACK error, anyone else have the same issue?  If so, how did you overcome it?
 

I am attempting to use this method to produce JSON output of my non-dynamic object, since non-dynamic objects do not provide a toJSON() method.  I am running version Cache for Windows (x86-64) 2016.2.1 (Build 806_0_17136U) Tue Mar 7 2017 12:25:45 EST

I should say that when I use the %ZEN method and grab the output in a file, the file is over 12MB, which leads me to believe that the FRAMESTACK error is caused by the many references to child objects within the parent. 

You can use RESTForms. It supplies converting object-to-JSON and JSON-to-object.

But for that he needs to add an extending class to his classes. Using the native functions directly is less overhead.