AnyLogic Cloud API abstracts away from the language that was used to develop the model (Java in case of AnyLogic) and allows you to interact with the model using multiple different languages on the client side. API offers methods to set up the model inputs, read outputs, pass and retrieve data to and from the running model. This means that data conversion is a part of the API.
In general, it works this way:
- JSON is used as a universal data format throughout AnyLogic Cloud API and the primary format of the REST API.
-
Jackson is used to convert data between Java and JSON. Conversion examples are given in the table below. To ensure safe reliable conversion for your own Java classes you should follow some simple rules also described below.
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. -
There are a few exceptions when conversion from Java to JSON is done in a special way (not with the help of Jackson). These are, primarily, the data objects from the Analysis palette in AnyLogic: DataSet, HistogramData, Histogram2DData, StatisticsContinuous, StatisticsDiscrete.
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).
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> |
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?
-