Question
Yann Simons · Feb 26, 2022

%JSON.Adaptor thow an error with XData with JSON content

Hi Community,

for our forms validation we use XData to describe fields on server side.

These XData are in JSON like this

XData FormValidator [ MimeType = application/json ]
{
 {
  "username": {
   "required": true 
  },
  "email": {
   "datatype": "email"
  }
 }
}

When I try to use %JSON.Adaptor in same classes, IRIS throw an error at compile time

ERROR #6301: SAX XML Parser Error: invalid document structure while processing Anonymous Stream at line 1 offset 23
  > ERROR #5490: Error running generator for method '%JSONGenerate:helora.worklist.Group'

It looks like method "GenerateMappingFromXdata" from %JSON.Generator try to parse XML only.

Is anyone have a workaround for this ?

Thank you

Product version: IRIS 2021.1
$ZV: IRIS for Windows (x86-64) 2021.1 (Build 215U) Wed Jun 9 2021 09:39:22 EDT [Health:3.3.0]
0
0 134
Discussion (8)1
Log in or sign up to continue

It looks like your JSON Mapping is already content of what you labeled XDATA.
The essence of XDATA is to contain an XML structure to describe your data.
that is to be transformed into a JSON mapping. 
In your case, you have the mapping already in hands.
So what other mapping would you expect ? 

And that's the description

classmethod GenerateMappingFromXdata(class As %String, ByRef mapping) as %Status

Get the JSON adaptor configuration information from class and property parameters.

BUT:
https://docs.intersystems.com/iris20212/csp/documatic/%25CSP.Documatic.cls?&LIBRARY=%25SYS&CLASSNAME=%25JSON.Generator

This class is used internally by InterSystems IRIS. You should not make direct use of it within your applications. There is no guarantee made about either the behavior or future operation of this class.

Just checked.  The method  uses %XML.Reader=##class(%XML.Reader).%New()
So you better  compose a nice XML description.
this tells you what is expected:

Do reader.CorrelateRoot("%JSON.Mapping")
 

detailed description is here  Defining an XData Mapping Block

  XData {MappingName}
  {
    <Mapping  {ClassAttribute}="value" [...] xmlns="http://www.intersystems.com/jsonmapping".>
      <{Property Name}="PropertyName" {PropertyAttribute}="value" [...] />
      [... more Property elements]
    </Mapping>
  }

Hi Robert, 

thank you for your response.

I'm not trying to do JSON mapping.

The XData in my first post is for validating forms server side. We generate methods based on this XData to check if each fields are correctly completed by users. According to this doc : https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls..., we used JSON instead of XML in XData because is easier to parse.

So my XData block is not related to new JSON mapping in IRIS.

When I moved to IRIS, I try to use %JSON.Adaptor in our class instead of Daniel Kutac adaptor (https://github.com/dkutac/cache-json-regular-vs-dynamic-object) to bind/export objects in JSON

For exemple, this class : 

Class helora.worklist.RessourceType Extends (%Persistent, %JSON.Adaptor)
{
Property code As %String;
Property description As %String(MAXLEN = "");
Index codeIdx On code [ Unique ];
XData FormValidator [ MimeType = application/json ] {
{
 "code":  { "required": true }
}
}
}

Does not compile. 

So it looks like impossible to use JSON in XData when classe extends %JSON.Adaptor.

You reference the documentation  for 

but your $ZV shows.

$ZV: IRIS for Windows (x86-64) 2021.1 (Build 215U) Wed Jun 9 2021 09:39:22 EDT [Health:3.3.0]

this might be the reason !

The difference is significant.

Version is not the reason. It is %JSON Adaptor that breaks it. 
Removing it let it compile.  Just tried it with 2022.1 preview
I suggest you deposit a bug report at WRC. Either compiler od Doc is wrong

Thank you for investigating in IRIS sources.

I'll do a bug report.

Regards