Question
· Oct 4, 2017

Using $fromObject throws <SYNTAX> error

I'm simply trying to evaluate what is available in terms of JSON projection of registered objects within Caché.

I search through the group and found examples of object script that looked promising,

 

however, when I try to using the object script I get a <SYNTAX> error,

I have to two Caché builds that I'm evaluating with:

  • Cache for Windows (x86-64) 2017.1 (Build 792) Mon Mar 20 2017 19:36:11 EDT
  • Cache for Windows (x86-64) 2016.2.1 (Build 803) Wed Oct 26 2016 12:43:35 EDT

the example I'm using comes straight from this group:

set person=##class(Sample.Person).%OpenId(1)
write person.$toJSON()

I also tried:

set pobj={}.$compose(person)

and this:

set pobj=##CLASS(%Object).$fromObject(person)

the above object scripts all throw a <SYNTAX> error,

not sure what I'm missing here, according to posts I've read in this group this syntax should be available in 2016.2 +

thanks, 

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

Hi Randall.

The early implementation of JSON support (which exposes system methods with a '$' prefix like '$toJSON()', etc), was deprecated after 2016.1 releases (which you are using).

Unfortunately the articles you are referencing where posted at the time the old syntax was supported, hence the <SYNTAX> error.

This post in the community explains the rationale for this change in more  detail.

This link is the on-line equivalent to the JSON documentation available for 2017.1, and covers all the new syntax you should be using.   In there you will find the correct methods to instantiate a dynamic object, and how to work with it.

Sincerely,

Steve

the part about the syntax changing in versions 2016.2+,

however, the initial question, and perhaps i did not make it clear was a way to project an object (such as Samples.Person) to JSON /dynamic object.

the example in pre 2016.2 version was: 

set person=##class(Sample.Person).%OpenId(1)
write person.$toJSON()

however i have not found a "api" using dynamic objects to do the same, something like this:

set person=##class(Sample.Person).%OpenId(1)

write person.%toJSON()

other than using the %ZEN.Auxiliary.altJSONProvider methods,

which i can live with,

i could also create my own method to "walk" the class properties and build the json/dynamic object myself,

which brings me to a follow up question:

is there a way to add custom property parameters, not a class parameter but the parameters you would see in the inspector within studio for a given property: CALCSELECTIVITY, CAPTION, COLLATION, CONTENT, etc...

the idea i have would be to add parameters i can use to include/exclude various properties from code that is accessing data from the class based on user "level" or other criteria.

thanks,

The following code has been tested on versions 2015.2 and 2017.2:

Class dc.demo Extends %RegisteredObject
{

Property f1 As %String;

Property f2 As %Integer;

Property f3 As %ListOfDataTypes;

Method GetTarget(
  ByRef pParms As %String,
  Output pObj As %RegisteredObjectAs %Status
{
  pObj = ..%New()
  
  i $g(pParms(1))="a" {
    pObj.f1 "Peter"
    pObj.f2 = 21
  }else{
    pObj.f1 "Bob"
    pObj.f2 = 12
    pObj.f3 ##class(%ListOfDataTypes).%New()
    pObj.f3.InsertList($lb("a","b","c"))
  }
  q $$$OK
}

ClassMethod Obj2Json()
{
  ;d ##class(dc.demo).Obj2Json()
  
  parms(1)="a"
  ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.stream,..%New(),"GetTarget",.parms,$$$YES,"aeloq")
  "1) ",stream.Read($$$MaxLocalLength),!
  
  parms
  ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.stream,..%New(),"GetTarget",.parms,$$$YES,"aeloq")
  "2) ",stream.Read($$$MaxLocalLength),!
}

ClassMethod Json2Obj()
{
  ;d ##class(dc.demo).Json2Obj()
  
  ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject("{""f1"":""Peter"",""f2"":21,""f3"":[]}",$classname(),.obj)
  obj.f1," : ",obj.f3.Count(),!

  ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject("{""f1"":""Bob"",""f2"":12,""f3"":[""a"",""b"",""c""]}",$classname(),.obj)
  obj.f1," : ",obj.f3.Count(),!
}

}

Result:

USER>##class(dc.demo).Obj2Json()
1) {"f1":"Peter","f2":21,"f3":[]}
2) {"f1":"Bob","f2":12,"f3":["a","b","c"]}
 
USER>##class(dc.demo).Json2Obj()
Peter : 0
Bob : 3