AnyLogic
Expand
Font size

Data conversion

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
{
    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>
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?