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

com.databasesandlife.util.jooq.PostgresJacksonConverter Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util.jooq;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import org.jooq.Converter;
import org.jooq.JSONB;

import java.io.IOException;

/**
 * Allows PostgreSQL JSON columns to be mapped to/from Java Jackson {@link JsonNode} objects.
 * For jOOQ 3.12 upwards.
 * 
 * @see StackOverflow Question
 */
public class PostgresJacksonConverter implements Converter {  // db, java

    @Override public Class fromType() {
        return JSONB.class;
    }

    @Override public Class toType() {
        return JsonNode.class;
    }

    @Override
    public JsonNode from(JSONB db) {
        try {
            if (db == null) return null;
            else return new ObjectMapper().readTree(db.data());
        } 
        catch (IOException e) { throw new RuntimeException(e); }
    }

    @Override
    public JSONB to(JsonNode u) {
        try {
            if (u == null || u.equals(NullNode.instance)) return null;
            else return JSONB.valueOf(new ObjectMapper().writeValueAsString(u));
        } 
        catch (IOException e) { throw new RuntimeException(e); }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy