Pojo Serializer and Deserializer

Use PojoSerializer and PojoDeserializer 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 PojoSerializer and PojoDeserializer can be found here.

This example calls a canister using Motoko main.mo.

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.

 public shared (msg) func apply(input : LoanApplication) : async LoanOffer { 
       counter += 1;
        Debug.print("Loan Application for user " #Principal.toText(msg.caller));
        let offer  : LoanOffer = {
            providerid = Principal.fromActor(this);
            providername = "Loan Provider";
            applicationid = counter;
            apr = 3.14;
            created = Time.now();

        var userOffers :  ?Offers<LoanOffer> = offers.get(msg.caller);

        switch userOffers {
            case (null) { var userOffer : Offers<LoanOffer> = Buffer.Buffer(0); userOffer.add(offer);  offers.put(msg.caller, userOffer)};
            case (?userOffer) { userOffer.add(offer); };
        return offer;

public query (msg) func getOffers() : async [LoanOffer] {
        var userOffers :  ?Offers<LoanOffer> = offers.get(msg.caller);

        switch userOffers {
            case (null) { return [] };
            case (?userOffer) { return userOffer.toArray() };

To call this canister the annotated proxy Java interface LoanProxy.java and ProxyBuilder will be used.

public interface LoanProxy {
	@Waiter(timeout = 30)
	public CompletableFuture<LoanOffer> apply(@Argument(Type.RECORD) LoanApplication loanApplication);
	public LoanOffer[] getOffers();

LoanApplication and LoanOffer Java classes with the Candid annotation are defined in LoanApplication.java and LoanOffer.java.

public class LoanApplication{
    public BigInteger id;
    public Double amount;
    public Short term;
    public String firstName;
    public String lastName;
    public String ssn;
    public String zipcode;
    public BigInteger created;
public class LoanOffer{
    public String providerId;	
    public String providerName;    
    public Principal userId;
    public Integer applicationId;
    public Double apr;
    public Long created;

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 @Field to override the default type and @Name to override the default name.

To skip serialization and deserialization of certain member variables, use the @Ignore annotation.

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

LoanApplication loanApplication = new LoanApplication();
loanApplication.firstName = "John";
loanApplication.lastName = "Doe";
loanApplication.ssn = "111-11-1111";
loanApplication.term = 48;
loanApplication.zipcode = "95134";		
loanApplication.amount = (double) 20000.00;
loanApplication.id = new BigInteger("11");
loanApplication.created = new BigInteger("0");
LoanProxy loanProxy = ProxyBuilder
		.create(agent, Principal.fromString(icCanister))
CompletableFuture<LoanOffer> response = loanProxy.apply(loanApplication);		
LoanOffer loanOffer = response.get();				
LOG.info("Loan Offer APR is " + loanOffer.apr);		

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

IDLValue idlValue = IDLValue.create(loanApplication, new PojoSerializer());
List<IDLValue> args = new ArrayList<IDLValue>();
IDLArgs idlArgs = IDLArgs.create(args);

byte[] payload = idlArgs.toBytes();

CompletableFuture<byte[]> response = UpdateBuilder
	.create(agent, Principal.fromString(canisterid), "apply")
	.callAndWait(Waiter.create(60, 5));
byte[] output = queryResponse.get();

LoanOffer loanOffer = IDLArgs.fromBytes(output).getArgs().get(0).getValue(new PojoDeserializer(), LoanOffer.class);	

Last updated