反序列化泛型类型与杰克逊

编程入门 行业动态 更新时间:2024-10-26 01:33:02
本文介绍了反序列化泛型类型与杰克逊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让使用杰克逊反序列化类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这个项目,所以这将是很好,如果我可以通过注射改变的类型,然后签注通用型我需要它返回的对象。我读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

这的文章帮我

更多推荐

反序列化泛型类型与杰克逊

本文发布于:2023-08-04 05:55:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:杰克逊   类型   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!