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 ?
Comments
You can manipulate object.city, for example like a string :
w $E(object.city,5,*) w object.city_"*"
Do you want to serialize it?
yes.
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
) 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!
Thank you!
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