反序列化泛型类型与杰克逊 [英] Deserializing a Generic Type with Jackson

查看:187
本文介绍了反序列化泛型类型与杰克逊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用杰克逊反序列化类POJO的。

I am trying to make a class that uses the Jackson to deserialize POJO's.

它看起来像这样...

It looks like this...

public class DeserialiserImp<T> implements Deserialiser<T> {

        protected ObjectMapper objectMapper = new ObjectMapper();

        @Override
        public T get(String content, Class clazz) throws IOException {
            return (T) objectMapper.readValue(content, clazz);
        }

        @Override
        public List<T> getList(String content, Class clazz) throws IOException {
            return objectMapper.readValue(content, TypeFactory.collectionType(ArrayList.class, clazz));
        }

    }

我对这个实施2个问题。

I have 2 questions about this implementation.

首先是,我传递的类类型入方法,使得objectmapper知道应该反序列化的类型。有没有使用泛型更好的办法?

The first is that I am passing the class type into the methods so the objectmapper knows the type that should deserialize. Is there a better way using generics?

此外,在get方法,我投了对象从objectMapper到T.这似乎是这样做的特别讨厌的方式,我必须投牛逼这里,然后我又回到了也投对象类型从哪个是方法调用它。

Also in the get method I am casting an object returned from the objectMapper to T. This seems particularly nasty way of doing it as I have to cast T here and then I have to also cast the object type from the method which is calling it.

我使用Roboguice这个项目,所以这将是很好,如果我可以通过注射改变的类型,然后签注通用型我需要它返回的对象。我读<一href="http://google-guice.google$c$c.com/git/javadoc/com/google/inject/TypeLiteral.html">TypeLiteral ,不知道它是否能解决这个问题?

I am using Roboguice in this project so it would be nice if I could change the type through injection and then annotate the object which the Generic type I need it to return. I read about TypeLiteral and wondering if it could solve this problem?

推荐答案

所以,我想我理解了它的尽头。请评论,如果你看到什么毛病我在做什么。

So I think I figured it out in the end. Please comment if you see something wrong with what I am doing.

该接口定义如下:

public interface Deserialiser<T> {

    T get(String content) throws IOException;

    List<T> getList(String content) throws IOException;
}

该接口的实现是这样的...

The implementation of the interface is like this...

public class DeserialiserImp<T> implements Deserialiser<T> {

    private ObjectMapper objectMapper = new ObjectMapper();
    private final Class<T> klass;

    @Inject
    public DeserialiserImp(TypeLiteral<T> type){
        this.klass = (Class<T>) type.getRawType();
    }

    @Override
    public T get(String content) throws IOException {
        return objectMapper.readValue(content, klass);
    }

    @Override
    public List<T> getList(String content) throws IOException {
        return objectMapper.readValue(content, TypeFactory.collectionType(ArrayList.class, klass));
    }

}

我绑定2像这样..

I bind the 2 like so..

    bind(new TypeLiteral<Deserialiser<User>>(){}).annotatedWith(Names.named("user")).to(new TypeLiteral<DeserialiserImp<User>>(){});

然后,所有我需要做的,使用它是这样的...

Then all I need to do to use it is this...

@Inject
@Named("user")
private Deserialiser<User> deserialiserImp;

public void test(String userString) {
    User user = deserialiserImp.get(UserString);
}

此模式也可以工作得很好,如果类为抽象类的DAO对象使用

This pattern could also work well if the class as an abstract class to use in a DAO object

的文章帮我

这篇关于反序列化泛型类型与杰克逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆