Object to JSON

See also: JSON to object

Value resolver – Abstract

Purpose: Returns the data structure of the input value as text in JSON notation.


images/download/attachments/119247376/image2023-1-4_16-33-12-version-1-modificationdate-1672846392970-api-v2.png

The Object to JSON resolver returns the data structure of the input value as text in JSON notation (see JSON data encoder).

NOTE◄ If the input value is 'No value' ($null), the text null is output.

  • The Class field name parameter defines the name of the property to be used in the output format to declare the 'class' of objects.

  • The Enforce native types option (unchecked by default) enforces the use of native JSON data types, e.g. Number for numerical values and Array for lists.

Details on the effect of the "Enforce native types" option


Example:

The 'Roles' (roles) field of a user account (see Users) is defined as a 'Unique list' (Set), since the same user should be able to be assigned only the same role a maximum of once. The assigned roles are referenced in this list by their ID (id), which is by definition an integer of the Long type. The value of the roles field is therefore a Set of Long values. The following table illustrates the effect of checking or unchecking the Enforce native types option when creating a JSON string that reflects the value of the roles (roles) field:

'Enforce native types' option

Return value
(JSON string)

images/s/-95e2zf/9012/8yg2g7/_/images/icons/emoticons/error.svg unchecked
(Default)

{   "class": "set",
"data": [
{
"class": "java.lang.Long",
"value": "1"
},{
"class": "java.lang.Long",
"value": "501"
},{
"class": "java.lang.Long",
"value": "1001"
}
]
}

images/s/-95e2zf/9012/8yg2g7/_/images/icons/emoticons/check.svg checked

[ 1, 501, 1001 ]

NOTE◄ Since the Enforce native types option maps different data types used in Lobster Data Platform / Orchestration to the same JSON data type, information (about the data type) can get lost.
In the example, the return value with the checked option no longer contains the information that the list as a Set can only contain unique values and that the contained values are by definition of the Long type.

Configuration

An object is expected as input value.

The Class field name parameter allows the input of a static text as a Text field, which is used in the output format to declare the 'class' of objects within the JSON document.

The default value is the class string.

If no Class field name is specified, the class name is written as the value of a property named Empty string ("").

NOTE◄ A structure with a property containing an empty string ("") can also be parsed again using the JSON to object value resolver. If the Class field name is not empty (e.g. class), a client object is created that contains the class name in an unlabeled property.

A Long value (1343) as input value then becomes a client object with the following JSON structure:

{"":"java.lang.Long", "value":"1343"}

The value (1343) can still be taken from the value property as a string. The class name is contained – also as a string – in the unlabeled property.

However, for a client object with an unlabeled property, the following restrictions must be considered:

  1. The unlabeled property can no longer be accessed directly – e.g. via Object property value resolver, via get (Read value from data field) or as a data field (for Form elements).

  2. Trying to encode a client object with an unlabeled property into a client XML structure (e.g. via Object to client xml or Object to server xml) fails with the error (Failed to encode: INVALID_CHARACTER_ERR), because the encoder tries to create an XML element with the name "".
    NOTE◄ The Object to server xml value resolver, on the other hand, maps the unlabeled properties to a property with name="" without any problems.

The Enforce native types option controls the handling of certain data types (numerical values and lists) in the output for which no 1:1 image exists in JSON format.

  • If the Enforce native types option is unchecked (default), then the data types in question are output as data objects with an explicit class field if required.

  • If the Enforce native types option is checked, data of the respective data types will be mapped to the corresponding native JSON data type (Number or Array), which can simplify the output format (see info box above).

Examples

The screenshots in the following table do not show the Enforce native types option. The table contains corresponding notes as far as a selection of this option influences the displayed return value at all.

Input value

Configuration example

Return value (string)

Text
(for the server one
any object with string value)

images/download/attachments/119247376/image2022-6-20_17-1-59-version-1-modificationdate-1669201068956-api-v2.png

"HELLO WORLD!"

Date/Time
(DateTime object)

images/download/attachments/119247376/image2022-6-20_17-3-58-version-1-modificationdate-1669201068947-api-v2.png

{
"class": "de.lobster.scm.utils.date.DateTime",
"dateValue": "1655735288548",
"timeZone": "UTC"
}

or with images/s/-95e2zf/9012/8yg2g7/_/images/icons/emoticons/check.svg Enforce native type option:

{
"class": "de.lobster.scm.utils.date.DateTime",
"dateValue": 1655735288548,
"timeZone": "UTC"
}

Heterogeneous list of values
(one list object with possibly different types per entry)

images/download/attachments/119247376/image2022-6-20_17-5-21-version-1-modificationdate-1669201068935-api-v2.png

{
"class": "list",
"data": [
{
"class": "java.lang.Long",
"value": "4711"
},
"0815"
]
}

or with images/s/-95e2zf/9012/8yg2g7/_/images/icons/emoticons/check.svg Enforce native type option:

[
4711,
"0815"
]

Company account
(as an example of an entity)

images/download/attachments/119247376/image2022-6-20_17-8-9-version-1-modificationdate-1669201068928-api-v2.png


NOTE◄ Setting the Enforce native types option, which is unchecked by default, would result in the following differences for the excerpt from the return value shown on the right:

  • The field values for creatorId, lastModifierId and ownerId would be output as integers and no longer as strings ("...").

  • The field value for parentCompanies would be output as a list of integers (here: with only one entry) and no longer as a list of long objects:

        "parentCompanies": [ 702 ]
{
"class": "de.lobster.scm.base.company.CompanyAccount",
"address": { ... },
"attributes": [ ... ],
"created": {
"time": "1536306221204",
"nanos": 204000000
},
"creatorId": "1901",
"defaultTimeZone": null,
"id": "1902",
"lastModified": {
"time": "1651765029218",
"nanos": 218000000
},
"lastModifierId": "1901",
"locale": "en",
"metaType": "COMPANY",
"ownerId": "702",
"parentCompanies": [
{
"class": "java.lang.Long",
"value": "702"
}
],
"partialLoaded": false,
"styleName": "XFLOW",
"types": [
{
"class": "de.lobster.scm.denum.DynamicEnumDto",
"enumName": "de.lobster.scm.base.company.CompanyType",
"name": "FWD"
},
{
"class": "de.lobster.scm.denum.DynamicEnumDto",
"enumName": "de.lobster.scm.base.company.CompanyType",
"name": "CNE"
},
{
"class": "de.lobster.scm.denum.DynamicEnumDto",
"enumName": "de.lobster.scm.base.company.CompanyType",
"name": "AAL"
},
{
"class": "de.lobster.scm.denum.DynamicEnumDto",
"enumName": "de.lobster.scm.base.company.CompanyType",
"name": "DPA"
},
{
"class": "de.lobster.scm.denum.DynamicEnumDto",
"enumName": "de.lobster.scm.base.company.CompanyType",
"name": "INV"
}
]
}