Question
· Oct 13, 2021

serialize from object to %string

hello!

i have object bellow

Set object = ##class(%ZEN.proxyObject).%New()
set object.city = "New York"
set object.Target = "TEST" 
set object.Details = "TEST"
set object.RefCode = "123"
set object.Reason = "123TTTT"

I want to get string from json object. 

Could you help please ?

Product version: Caché 2017.1
$ZV: Cache for UNIX (Red Hat Enterprise Linux for x86-64) 2017.2.2 (Build 867_4_20165U) Mon Aug 17 2020 11:28:28 EDT
Discussion (8)0
Log in or sign up to continue

WHO DARES SUMMON ME!

Oh, hi Evgeny!  Great to see you, hope you're well!

For everyone else, Evgeny is right, of course (because he's quoting me laugh) but note that in newer versions (IRIS, for instance), you can make use of the %JSON.Adaptor to do something like:

Class Test.DoesItJSON Extends (%RegisteredObject, %JSON.Adaptor)
{
Property aString As %String;
Property anInteger As %Integer;
}

The do something like:

s obj=##class(Test.DoesItJSON).%New()
s obj.aString="Hello"
s obj.anInteger=123

d obj.%JSONExportToString(.jsonstring)
zw jsonstring
    jsonstring="{""aString"":""Hello"",""anInteger"":123}"

This is better for creating new classes (again in later versions than you're on, you need to use the jsonProvider or the altJsonProvider) that you think you're going to want to serialize into JSON.

Set object ##class(%ZEN.proxyObject).%New()
set object.city "New York"
set object.Target "TEST" 
set object.Details "TEST"
set object.RefCode "123"
set object.Reason "123TTTT"

set string=object.%Serialize()

;write string,!

set anotherObj=##class(%ZEN.proxyObject).%New()
do anotherObj.%ZENDeserialize(string)

write anotherObj.Reason ; ==> 123TTTT