Pojo Serializer and Deserializer

Use PojoSerializerarrow-up-right and PojoDeserializerarrow-up-right to serialize and deserialize the annotated POJO (Plain Old Java Object) to and from Candid payload of type RECORD.

Serializer and Deserializer will use Candid Java annotations to get the Candid Name and Type.

A fully functional example using PojoSerializerarrow-up-right and PojoDeserializerarrow-up-right can be found herearrow-up-right.

This example calls a canister using Motoko main.moarrow-up-right.

The canister uses 2 complex types, LoanApplication and LoanOffer.

 // Loan Application
public type LoanApplication = {
    id: Nat;
    firstname: Text;
    lastname: Text;
    zipcode: Text;
    ssn: Text;
    amount: Float;
    term: Nat16;
    created: Int;
 };

// Loan Offer
public type LoanOffer = {
    providerid: Principal;
    providername: Text;
    applicationid: Nat;
    apr: Float;
    created: Int;
};

The canister has 2 methods apply and getOffers.

To call this canister the annotated proxy Java interface LoanProxy.javaarrow-up-right and ProxyBuilder will be used.

LoanApplication and LoanOffer Java classes with the Candid annotation are defined in LoanApplication.javaarrow-up-right and LoanOffer.javaarrow-up-right.

By default, PojoSerializer and PojoDeserializer will use Java to Candid type mapping and use the Java member variable name as the name of the Candid RECORD field.

Use Candid annotations @Fieldarrow-up-right to override the default type and @Namearrow-up-right to override the default name.

To skip serialization and deserialization of certain member variables, use the @Ignorearrow-up-right annotation.

ProxyBuilder will implicitly use PojoSerializer and PojoDeserializer to convert Candid RECORD to a Java object.

To use Raw methods or QueryBuilder and UpdateBuilder use PojoSerializer and PojoDeserializer directly in the Java code.

Last updated