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,
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
Also note, that direct casting from regular objects and into dynamic was removed.
Thank you for clarifying this,
Interesting that the object to json support was removed, which is really what i was primarily looking for.
At this point I would need to create my own method, or maybe use this method:
set json=##class(%ZEN.Auxiliary.altJSONProvider).%ObjectToAET(person)
thanks again,
Randall, my understanding is that it wasn't removed. But some of the syntax that was introduced in 2016.1 in connection with it was subsequently dropped, meaning that code of yours which worked in 2016.1 has to be amended for 2016.2 and later. The links above should help.
Possibly answered in comments. I am posting this as an answer so the question no longer lists as "no answer", and so the OP can mark it as answered if desired (thus removing it from the "unanswered" view).
Will the PropertyClass mechanism meet your need?
i believe that is what i'm looking for,
thank you
CAPTION and COLLATION are default property parameters that you can add to any property.
yep, along with those there could be others, formGroup, requiredWith, ect...
thanks,
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:
Result: