Applies to AnyLogic Cloud 2.5.1. Last modified on December 09, 2024.
AnyLogic Cloud API abstracts away from the Java code you use to create and run models in AnyLogic, allowing for using multiple different languages to interact with the model.
JSON is used as a universal data transfer format in all languages of AnyLogic Cloud API. It also serves as the primary format of the REST API.
To convert data between JSON and Java, AnyLogic Cloud uses Jackson.
Simply put, in terms of data conversion it does not matter which API language you use. When, for example, setting inputs for your API-based experiments, you always will provide values in the JSON format. This data will then be converted into the appropriate format for working in Java.
The table below contains the conversion examples, with additional recommendations and requirements.
When converting from JSON to Java, Jackson identifies the target data type and will try to match the incoming string accordingly. When converting from Java to JSON, the target type is not identified.
Objects of those classes are considered as model output only and cannot be passed in the reverse direction (from the API client to the model).
In the left column, the examples of inputs for various data types are demonstrated. You will always use them when providing inputs for your Cloud-based experiments.
Most complex data types are presented in JSON as strings: see the bottom of the table.
JSON | Java | |
---|---|---|
Primitive types | ||
Number | ↔ | double, float, int, long, and so on (any numeric type) |
Boolean | ↔ | boolean |
String | ↔ | String |
Exception: AnyLogic classes with specific Java to JSON conversion, reverse conversion is NOT possible | ||
|
← | DataSet |
|
← | HistogramData |
|
← | Histogram2DData |
|
← | StatisticsContinuous |
|
← | StatisticsDiscrete |
Standard complex types, for which Jackson rules apply, for example: | ||
Formatted string, for example, "2019-05-13T15:34:03.976" | ↔ | Date |
Date | → | Date |
[12.5, 34,156.9] array of numbers | ↔ | double[] |
["red", "white","blue"] array of strings | ↔ | ArrayList<String> |
JSON-formatted string containing information about the file’s hash code and name | ↔ | FileResource |
Just convert it to JSON using the API and explore the result to observe the structure | ↔ | Any complex class |
User-defined classes, for which Jackson rules apply for example: | ||
{ name:"John",age:33 } | ↔ |
|
{ name:"John", age:33 } | ↔ |
|
To make sure you have safe bidirectional JSON-Java conversion for your custom objects, you should follow simple rules:
- Class fields that you wish to pass should either be declared public in Java or, if they are private, they must have public setters and getters
- There should be a default public constructor available in the Java class
-
How can we improve this article?
-