JSON

Getting started

Flavour provides ability to serialize and deserialize Java objects to and from JSON. Java types system does not map to JSON clearly, so additional conventions and annotations are required. Jackson is a de-facto standard for Java-to-JSON serialization, hence Flavour supports reasonable subset of Jackson annotations. This helps to achieve good adoption for developers who already use Java on server and familiar with Jackson. But most of all, the goal of implementing subset of Jackson was to allow developers reuse annotated classes both on client and server side.

The main use-case for JSON serialization is communication to server. In this case Flavour has built-in solution, so you don’t have to deal with JSON API directly. But there are some cases when you want to call JSON API. The following example shows how to use it:

    MyClass a = new MyClass();
    a.foo = "hello";
    a.bar = 23;
    String json = JSON.serialize(a).stringify();
    System.out.println(json);

    MyClass b = JSON.deserialize(Node.parse(json), MyClass.class);
    System.out.println(b.foo + "," + b.bar);

where MyClass is defined like this:

public class MyClass {
    public String foo;
    public int bar;
}

this code must be self-descriptive. You can find that main entry points to JSON API are two methods from JSON class: serialize and deserialize. Both operate on JSON nodes represented by Node class, so some additional code required to convert to and from string.

Supported subset of Jackson

To learn Jackson annotations, you can read its official documentation. Here is the list of supported annotations:

Note that TeaVM optimizer often remove classes which are unreachable from anywhere. For example, it removes a class if it is never instantiated from reachable methods. In case of inheritance, when a subclass can only be instantiated by deserializer, TeaVM may remove the class. To prevent TeaVM from removing classes, you should always use @JsonSubTypes when you want polymorphic deserialization.


Improve this page