A Validator for JSON-XML
Why is it that a transformation fails to produce valid JSON? To answer this question, I wrote a little JSON-XML validator.
The Problem
Since my current main business is to develop web applications with Business Server Pages, I thoroughly studied Horst Keller’s series of blog posts on JSON in ABAP. In the meantime, most of the JSON topics are available, well-structured and extensively documented, in the ABAP help.
In a current large-scope development project, I had the opportunity to apply many of these ideas. Building JSON from ABAP data always was a topic for BSP elements – in the commonly-used “framework” part of the application, when it came to providing UI components like jQuery data tables with the requested data. In most cases, the best solutions where transformations, transforming the data directly from the ABAP side into the desired JSON xstring, which was then passed via response->set_data( ) to the client. The necessary transformations were designed once and then addressed through the BSP-elements “behind the scenes”. With other words: The main part of the ABAP development was still – ABAP development, ???? not JavaScript or JSON or whatever.
When I designed my transformations, they did not run from the scratch, of course. They crashed. And what bothered me with these crashes was that it was hard to find out from the dump which error I had made while constructing the JSON-XML target. Of course, the well-formedness of the result is by far not sufficient. There are rules for JSON-XML, and the dump didn’t tell me which rule I had violated. I had to find that out myself by inspecting the result XML.
A Solution with Schematron
So I thought it would be a good idea to have the machine taking over that work for me – with other words: to have a validator for JSON-XML. To see what I am talking about – find here the
directly from the ABAP docu. For those who have never seen an example – here is one:
This little XML document should transform into the JSON string
{ "question":"Is JSON-XML cool?", "answer":true }
But is it really valid JSON-XML? Check it out yourself – with my validator. Here it goes:
I knew that Schematron was a validation language for XML, apt for writing and asserting rules on the structure of an XML document. So I thought this would be the chance to give Schematron a try for this task. I don’t regret the choice. (Alternatively, I could have used XML Schema and working with a schema validator – the approach that I had used in an earlier post for another issue. But, to admit it frankly: I was curious to see the Schematron technology in action. And it works!)
Basically – bringing all its syntax, document structures, and namespaces to a point – a Schematron document is a collection of XPath expressions which are applied successively on a set of specified context nodes of the document. The XPath expressions are designed to evaluate to true. If they fail, a failure node with a message is generated in the result document.
- A document may contain arbitrarily many patterns. Patterns can be used to group the validation rules according to the nodes’ different functions.
- The setting of the context nodes to inspect is called a rule.
- An XPath expression which is supposed to evaluate to true, is called an assertion. A rule contains arbitrarily many assertions. An assertion usually contains a diagnosis message as inner text, which will be issued if the assertion fails.
For JSON-XML, I saw three different groups of assertions:
- General assertions, which have to be satisfied by each element of the document.
- Assertions on simple elements – corresponding to simple datatypes – like , , and .
- Assertions on complex elements, having sub-elements (other than ): I.e., s and