AnyLogic
Expand
Font size

Data conversion

Applies to AnyLogic Cloud 2.5.0. Last modified on November 20, 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.

There are a few exceptions when the conversion from Java to JSON is done differently (not with the help of Jackson). These are, primarily, the data objects from the Analysis palette of 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).

Conversion table

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
{
    dataX:[1,2,3,4],
    dataY:[10,20,30,40]
}
DataSet
{
    hits: [0,0,0,1,5,67,335,441,138,13],
    hitsOutHigh: 0,
    hitsOutLow: 0,
    intervalWidth: 1.2,
    lowerBound: -8.6,
    statistics:{
        count: 1000,
        max: 2.83,
        mean: 0.0005,
        min: -3.91,
        type: "DISCRETE",
        variance: 0.959
    }
}
HistogramData
{
    hits: [
        [8, 4, 4, 4, 9],
        [5, 6, 8, 2, 1],
        [3, 8, 7, 6, 8],
        [7, 8, 5, 7, 9],
        [3, 4, 3, 9, 6],
    ],
    hitsOutHigh: [0,0,0,0,0],
    hitsOutLow: [0,0,0,0,0],
    xMax: 10,
    xMin: 0,
    yMax: 1,
    yMin: 0
}
Histogram2DData
{
    type: "CONTINUOUS",
    count:1375730,
    min:0.0,
    max:10.0,
    mean:0.9915478405530092,
    variance:0.14075328464389159,
    totalTime:999999.2699032243
}
StatisticsContinuous
{
    type: "DISCRETE",
    count: 1000,
    max: 2.83,
    min: -3.91,
    mean: 0.0005,
    variance: 0.959
}
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 }
public class Person {
    private String name;
    private int age;

    public String getName() { return name; }

    public int getAge() {return age; }

    public void setName(String name ) {
    this.name = name;
    }

    public void setAge(int age ) {
    this.age = age;
    }
}
{ name:"John", age:33 }
public class Person {
    public String name;
    public int age;
}

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?