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

net.optionfactory.hj.gson.GsonJsonDriver Maven / Gradle / Ivy

Go to download

Custom Hibernate types for serializing fields as JSON, using either jackson or gson

There is a newer version: 6.7
Show newest version
package net.optionfactory.hj.gson;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import net.optionfactory.hj.JsonDriver;
import net.optionfactory.hj.JsonMappingException;
import net.optionfactory.hj.TypeDescriptor;

public class GsonJsonDriver implements JsonDriver {

    private final Gson json;

    public GsonJsonDriver(Gson json) {
        this.json = json;
    }

    @Override
    public Object deserialize(String value, TypeDescriptor type) {
        try {
            return json.fromJson(value, type.as(TypeToken.class).getType());
        } catch (JsonParseException ex) {
            throw new JsonMappingException(ex);
        }
    }

    @Override
    public Object deserialize(String value, Type type) {
        try {
            return json.fromJson(value, type);
        } catch (JsonParseException ex) {
            throw new JsonMappingException(ex);
        }
    }
    
    
    @Override
    public String serialize(Object value, TypeDescriptor type) {
        try {
            return json.toJson(value, type.as(TypeToken.class).getType());
        } catch (JsonParseException ex) {
            throw new JsonMappingException(ex);
        }
    }


    @Override
    public String serialize(Object value, Type type) {
        try {
            return json.toJson(value, type);
        } catch (JsonParseException ex) {
            throw new JsonMappingException(ex);
        }
    }

    @Override
    public TypeDescriptor fieldType(Field field, Class context) {
        return new GsonTypeDescriptor(TypeToken.get(field.getGenericType()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy