Sometimes you need to deal with structured data in JSON representation in xUML, for which you don't have a corresponding class definition that you can parse it into. Either because no specification is available, or the JSON contains features that are impossible to map onto a strictly typed class structure.
This is where JsonNode comes in for the rescue. You can parse arbitrary JSON into a generic node structure and traverse it, manipulate it, or create a new node structure from scratch, and render a structure (back) to its JSON representation.
Auto-detection of DateTime values on parsing is supported (if these are rendered in the same format the xUML runtime would accept, i.e. ISO 8601 UTC), but can be disabled.
Important note: Never use create with JsonNode
Never use create with an instance of JsonNode directly, but always use the JsonNodeFactory methods instead. The default create cannot be made unaccessible, so the compiler would accept it, but you are likely to see exceptions or, even worse, unexpected behavior later on.
Reason: The JsonNode class is just a convenience layer on top of the underlying imported Java class (GenericNode) that represents the JSON in memory. All operations on JsonNode read or manipulate the underlying GenericNode structure.
Overview
Unsupported Operations
The following actions are not supported and will raise an exception when attempted to being used:
-
setting a value of a different type, e.g. calling setStringValue() on a node of type INTEGER (no implicit type conversion supported)
-
adding a value of different type to an array, e.g. calling appendStringValue() on an ARRAY node that is not empty and contains element(s) of e.g. INTEGER
-
calling getAs...() on ARRAY nodes of incompatible type
-
calling get...Value() on node of incompatible type
-
calling get/setChildNode on non-OBJECT nodes
-
etc.
In short, if you know what to expect, you can directly access the nodes and their value, otherwise you will have to check on their type first (use getNodeType(), getChildNodes() on OBJECT and getArrayNodeAt() on ARRAY nodes)
Examples
Consider the following relations:
This allows us to create a generic JsonNode structure that mimics an equivalent class structure, and there will be no semantic differences when comparing the JSON strings generated from either representation:
Or, likewise, to manipulate a given JsonNode structure in a similar way you would do with mapped objects:
Related Content
Related Pages:
-
JsonDiff
Calculate JSON patch from two JSONdocuments, or apply a patch to a document. -
JsonMatch
Testing JSON strings or object instances for matching against a DSL (Domain Specific Language) similar toWHEREclauses in SQL. -
https://scheer-pas-doc.atlassian.net/wiki/x/vYCRJQ
Find and extract parts of JSON object based on JsonPath queries. -
JsonPointer
Extract part of JSON object using a JsonPointer (such as returned as part of a patch from JsonDiff). -
Json Schema
Convert a JSON schema file to an XSD, ready to be imported into the PAS Builder. -
https://scheer-pas-doc.atlassian.net/wiki/x/koGRJQ
Generate SQL statements from JSON objects.