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

com.greenback.kit.jackson.JacksonGreenbackCodec Maven / Gradle / Ivy

package com.greenback.kit.jackson;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fizzed.crux.jackson.EnumDeserializeStrategy;
import com.fizzed.crux.jackson.EnumSerializeStrategy;
import com.fizzed.crux.jackson.EnumStrategyModule;
import com.fizzed.crux.jackson.JavaTimePlusModule;
import com.greenback.kit.client.GreenbackCodec;
import com.greenback.kit.model.*;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TimeZone;

public class JacksonGreenbackCodec implements GreenbackCodec {

    protected ObjectMapper objectMapper;
    
    public JacksonGreenbackCodec() {
        this.objectMapper = new ObjectMapper()
//            .enable(SerializationFeature.INDENT_OUTPUT)
            .setSerializationInclusion(Include.NON_NULL)
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
//            .registerModule(new JavaTimeModule())
            // much more flexible JSR310 de(serializing)
            .registerModule(JavaTimePlusModule.build(false))
            .registerModule(new EnumStrategyModule(EnumSerializeStrategy.LOWER_CASE, EnumDeserializeStrategy.IGNORE_CASE)
                .setNullOnUnknown(true));
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        this.objectMapper.setDateFormat(df);
    }
    
    public ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    protected void verifySuccess(JsonNode rootNode) throws IOException, GreenbackException {
        // has error?
        if (rootNode.has("error")) {
            GreenbackError error = this.objectMapper.treeToValue(rootNode.get("error"), GreenbackError.class);
            
            throw new GreenbackException(error);
        }
    }
    
    protected  T read(
            InputStream input,
            TypeReference typeReference) throws IOException, GreenbackException {
        
        final JsonNode rootNode = this.objectMapper.readTree(input);

        this.verifySuccess(rootNode);
        
        return this.objectMapper.convertValue(rootNode, typeReference);
    }
    
    static private final TypeReference> TYPEREF_FLATTENED_MAP
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_USERS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_USER
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_ENTITLEMENTS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_CONNECTS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_CONNECT
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_CONNECT_INTENT
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_ACCOUNTS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_ACCOUNT
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_VISIONS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_VISION
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_MESSAGES
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_MESSAGE
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_SYNCS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_SYNC
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_TRANSACTIONS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_TRANSACTION
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_TRANSACTION_EXPORTER
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_TRANSACTION_EXPORT
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_TRANSFORMS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_TRANSFORM
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_AUTO_EXPORTS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_AUTO_EXPORT
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_AUTO_EXPORT_RUNS
        = new TypeReference>() {};
    static private final TypeReference> TYPEREF_AUTO_EXPORT_RUN
        = new TypeReference>() {};

    @Override
    public String prettyPrint(Object value) throws IOException {
        return this.objectMapper.writeValueAsString(value);
    }
    
    @Override
    public Map toFlattenedMap(Object value) throws IOException {
        if (value == null) {
            return null;
        }
        return this.objectMapper.convertValue(value, TYPEREF_FLATTENED_MAP);
    }
    
    @Override
    public byte[] writeBytes(Object value) throws IOException {
        if (value == null) {
            return new byte[0];
        }
        return this.objectMapper.writeValueAsBytes(value);
    }
    
    @Override
    public Paginated readUsers(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_USERS);
    }
    
    @Override
    public User readUser(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_USER).getValue();
    }

    @Override
    public Entitlements readEntitlements(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_ENTITLEMENTS).getValue();
    }

    @Override
    public Paginated readConnects(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_CONNECTS);
    }
    
    @Override
    public Connect readConnect(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_CONNECT).getValue();
    }
    
    @Override
    public ConnectIntent readConnectIntent(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_CONNECT_INTENT).getValue();
    }
    
    @Override
    public Paginated readAccounts(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_ACCOUNTS);
    }
    
    @Override
    public Account readAccount(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_ACCOUNT).getValue();
    }
    
    @Override
    public Paginated readVisions(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_VISIONS);
    }
    
    @Override
    public Vision readVision(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_VISION).getValue();
    }
    
    @Override
    public Paginated readMessages(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_MESSAGES);
    }
    
    @Override
    public Message readMessage(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_MESSAGE).getValue();
    }
    
    @Override
    public Paginated readSyncs(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_SYNCS);
    }
    
    @Override
    public Sync readSync(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_SYNC).getValue();
    }
    
    @Override
    public Paginated readTransactions(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_TRANSACTIONS);
    }
    
    @Override
    public Transaction readTransaction(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_TRANSACTION).getValue();
    }
    
    @Override
    public TransactionExportIntent readTransactionExporter(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_TRANSACTION_EXPORTER).getValue();
    }
    
    @Override
    public TransactionExport readTransactionExport(
            InputStream input) throws IOException {
        
        return this.read(input, TYPEREF_TRANSACTION_EXPORT).getValue();
    }

    @Override
    public Paginated readTransforms(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_TRANSFORMS);
    }

    @Override
    public Transform readTransform(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_TRANSFORM).getValue();
    }
    
    @Override
    public Paginated readAutoExports(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_AUTO_EXPORTS);
    }

    @Override
    public AutoExport readAutoExport(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_AUTO_EXPORT).getValue();
    }

    @Override
    public Paginated readExportRuns(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_AUTO_EXPORT_RUNS);
    }

    @Override
    public ExportRun readExportRun(
            InputStream input) throws IOException {

        return this.read(input, TYPEREF_AUTO_EXPORT_RUN).getValue();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy