View on GitHub

zserio

zero sugar, zero fat, zero serialization overhead

Java Generator for Zserio

Zserio extension generates Java serialization API from the Zserio schema together with additional API.

The generated code must be always used with Java Runtime Library which provides functionality common for all generated code.

For a quick start see the Java Tutorial.

Content

Supported Java Versions

Serialization API

        Subtypes

Additional API

        Range Check

        Validation

        Code Comments

        Type Information

        JSON Debug String

Compatibility Check

Optimizations

Supported Java Versions

Zserio Java generator supports the Java SE 8 (LTS), the Java SE 11 (LTS) and the Java SE 17 (LTS).

Although newer Java versions are not tested, they should work as well as long as they are backward compatible.

Serialization API

The serialization API provides the following features for all Zserio structures, choices and unions:

Subtypes

Because Java language does not support aliases for types, Zserio subtypes in Java are resolved during API generation (resolved means that generated Java API uses always original type directly).

However, there are some use cases where it would be beneficial to have a generated Java class for subtype types of compounds, e.g. to parse compound type which is given by a subtype class name.

Therefore, for each subtype of compound, a Java class is generated which inherits the base type. These generated subtype classes are not used by generated code and are just meant to support applications.

Note: Subtypes of primitive types are not reflected in generated code at all. This is because simple types are unboxed and other built-in types (e.g. String) are final (i.e. not possible to inherit).

Additional API

The following additional API features which are disabled by default, are available for users:

All of these features can be enabled using command line options which are described in the Zserio User Guide document.

Range Check

Because not all Zserio integer types can be directly mapped to the Java types (e.g. bit:4 is mapped to byte), it can be helpful to explicitly check values stored in Java types for the correct ranges (e.g to check if byte value which holds bit:4, is from range <0, 15>). Such explicit checks allow throwing exception with the detailed description of the Zserio field with wrong value.

The range check code is generated only in the setters method directly before the field is set.

Validation

The validation generates method validate() in all generated SQL databases. This method validates all SQL tables which are present in the SQL database. The SQL table validation consists of the following steps:

The check of all columns consists of the following steps:

Code Comments

The code comments generate Javadoc comments for all generated Zserio objects. Some comments available in Zserio schema are used as well.

Type Information

The type information generates static method typeInfo() in all generated Zserio types (except of Zserio subtypes). This method returns all static information of Zserio type which is available in the Zserio schema (e.g. schema name, if field is optional, if field is array, etc…).

JSON Debug String

JSON debug string feature provides export and import to/from JSON string for all Zserio structures, choices and unions:

Note that this feature is available only if type information is enabled!

Compatibility check

Java generator honors the zserio_compatibility_version specified in the schema. However note that only the version specified in the root package of the schema is taken into account. The generator checks that language features used in the schema are still encoded in a binary compatible way with the specified compatibility version and fires an error when it detects any problem.

Note: Binary encoding of packed arrays has been changed in version 2.5.0 and thus versions 2.4.x are binary incompatible with later versions.

Optimizations

The Java generator provides the following optimizations of the generated code:

Such optimizations can be done because Zserio relays on the fact that the entire schema is known during the generation. Therefore, splitting schema into two parts and generating them independently cannot guarantee correct functionality. This can lead to a problem especially for templates, if a template is defined in one part and instantiated in the other.