JSON Jackson Serializer and Deserializer

Use JacksonSerializerarrow-up-right and JacksonDeserializerarrow-up-right to serialize and deserialize Java Jacksonarrow-up-right JSON object to and from the Candid payload of type RECORD.

A fully functional example using JacksonSerializer and JacksonDeserializer can be found herearrow-up-right.

This example uses Motoko to call the canister main.moarrow-up-right. The canister uses 2 complex types, LoanApplication and LoanOffer.

main.mo
// 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.

The example uses the file with JSON LoanApplication payloadarrow-up-right as an input.

To be able to properly map JSON names and values to Candid name types declare the IDLType structure as follows:

Next, create IDLValue using the JacksonSerializer create method.

The Serializer input is the variable type JsonNodearrow-up-right.

Use UpdateBuilder, QueryBuilder or Raw Methods to call the Canister and deserialize output to JsonNodearrow-up-right.

Last updated