You can serialize the current ViewModel to a JSON-formatted string with ViewModelAsJSon; use it when an OCL expression needs the ViewModel's current content as JSON.
Use ViewModelAsJSon
ViewModelAsJSon is a function on selfVM, the variable available in a ViewModel context. Call it without arguments:
selfVM.ViewModelAsJSon()
The function returns the content of the current ViewModel as a JSON-formatted string.
Control JSON property names
By default, a JSON property uses the ViewModel attribute's name. Set the attribute's Presentation Name to use a different property name in the JSON output.
This lets you use a property name that cannot be used as a ViewModel attribute name, such as a name containing a dash.
Example
Assume the ViewModel has these attributes:
| ViewModel attribute | Value | Presentation Name |
|---|---|---|
CustomerName
|
Sandra
|
Not set |
OrderAmount
|
6000
|
order-amount
|
Use this expression:
selfVM.ViewModelAsJSon()
The returned JSON string uses CustomerName for the first attribute and the Presentation Name, order-amount, for the second:
{"CustomerName":"Sandra","order-amount":6000}
Choose JSON or XML
Use ViewModelAsJSon when the receiving code expects JSON. If it expects XML, use ViewModelAsXml instead. The XML operator has its own XML-specific naming and rendering options.
