All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.casper.sdk.jackson.deserializer.TransactionEntryPointDeserializer Maven / Gradle / Ivy

Go to download

SDK to streamline the 3rd party Java client integration processes. Such 3rd parties include exchanges & app developers.

The newest version!
package com.casper.sdk.jackson.deserializer;

import com.casper.sdk.exception.CasperClientException;
import com.casper.sdk.model.transaction.entrypoint.CustomEntryPoint;
import com.casper.sdk.model.transaction.entrypoint.TransactionEntryPoint;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.Iterator;

/**
 * The Deserializer for {@link TransactionEntryPoint} objects as Jackson in not able match nested Rust enum types
 *
 * @author [email protected]
 */
public class TransactionEntryPointDeserializer extends JsonDeserializer {

    @Override
    public TransactionEntryPoint deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

        if (p.getCurrentToken() == JsonToken.START_OBJECT) {
            // Nested rust enums are represented as objects in JSON
            return createNestedEntryPoint(p.readValueAsTree());
        } else if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
            // Standard Rust enum types are read from string
            return createSimpleEntryPoint(p.getValueAsString());
        } else {
            throw new IllegalArgumentException("Unknown scheduling type: " + p);
        }
    }

    private CustomEntryPoint createNestedEntryPoint(final ObjectNode objectNode) {
        final Iterator stringIterator = objectNode.fieldNames();
        final String next = stringIterator.next();
        return new CustomEntryPoint(objectNode.get(next).asText());
    }

    private TransactionEntryPoint createSimpleEntryPoint(final String name) {
        try {
            //noinspection unchecked
            final Class clazz = (Class) Class.forName(buildClassName(name));
            return clazz.newInstance();
        } catch (Exception e) {
            throw new CasperClientException("Invalid TransactionEntryPoint " + name, e);
        }
    }

    private String buildClassName(String name) {
        return TransactionEntryPoint.class.getPackage().getName() + "." + name + "EntryPoint";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy