Question
Token Ibragimov · Oct 13

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
00
0 0 8 199
Log in or sign up to continue

You can manipulate object.city, for example like a string :

w $E(object.city,5,*)
w object.city_"*"

Hope this could help:

w ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.stream,j)

1

SAMPLES>w stream.Read()

{

"_class":"Jiri.RegisteredObject",

"p1":"test",

"p2":"json"

}

Stolen from @Kyle Baxter answer here. 

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.

Great to see you, Kyle! 

Yes, this was a part of the plan )) Welcome back!

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