使用泛型将Jackon json字符串转换为Java对象 [英] Jackon json string to Java object using generics

查看:626
本文介绍了使用泛型将Jackon json字符串转换为Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多与我的下面的查询有关的帖子,但无法找到如何处理这个问题:
我有一个类如下:

  public class MyClass< T>实现IMyInterface< String,T> {

@Override
public T myMethod(String jsonString){
ObjectMapper mapper = new ObjectMapper();
// T结果= mapper.readValue(jsonString,T.class)//确定T.class没有意义,但有什么选择?


$ / code $ / pre

我想把这个类作为一个util类用于任何可以将它们的json字符串传递给 myMethod 的客户端,并获得所需的Java对象,该对象映射到他们在实例化 MyClass
所以作为一个例子,一个客户端代码没有

  MyClass< MyType1> o1 = new MyClass< MyType1>(); 
MyType1 myType1 = o1.myMethod(aJsonString);

获得一个 MyType1



以及另一个代码

  MyClass< MyType2> o2 =新的MyClass< MyType2>(); 
MyType2 myType2 = o2.myMethod(anotherJsonString);

获取 MyType2



我看到很多与TypeReference和JavaType使用有关的帖子,但无法确切地知道如何使用它们适合我的上述用例。



我的主要目标是 MyClass 实际上应该作为一个util类而不知道在编译时什么是它将转换输入json字符串的Java对象至。它应该在运行时决定。



任何帮助将不胜感激!



非常感谢和祝福

解决方案

以下解决方案适用于您。有一个类型为Class的私有实例并在构造函数中初始化它。

  import org.codehaus.jackson.map.ObjectMapper; 

公共类MyClass< T>实现IMyInterface< String,T> {

私人课程< T> clazz中;

public MyClass(Class< T> clazz){
this.clazz = clazz;
}

@Override
public T myMethod(String jsonString)throws Exception {
ObjectMapper mapper = new ObjectMapper();
T result = mapper.readValue(jsonString,clazz);
返回结果;
}

}

您的客户端代码将为:

  MyClass< MyType1> o1 = new MyClass< MyType1>(MyType1.class); 
MyType1 myType1 = o1.myMethod(aJsonString);

为第二个问题添加答案:

在spring的上下文中,定义你的bean如下。

 < bean id =simpleMyType1Class类= com.yourpackage.MyClass > 
< constructor-arg>
<! - 这可以是课程的完整路径 - >
<值> com.yourpackage.MyType1< /值>
< / constructor-arg>



在Java代码中,如下所示。

  MyClass< MyType> o1 =(MyClass)context.getBean(simpleMyType1Class); 


I've seen numerous posts related to my below query but unable to find exactly how to handle this issue: I've a class as below:

  public class MyClass<T> implements IMyInterface<String, T> {

       @Override
       public T myMethod(String jsonString) {
           ObjectMapper mapper = new ObjectMapper();
           //T result = mapper.readValue(jsonString, T.class) //for sure T.class does not make sense, but what is the alternative ?
       }
}

I want to use this class as a util class for any client who can pass on their json string to the myMethod and get the desired Java object that maps to the class they mention while instantiating MyClass. So as an example,a client code that does

MyClass<MyType1> o1 = new MyClass<MyType1>();
MyType1 myType1 = o1.myMethod(aJsonString);

get an instance of MyType1

and another code that does

MyClass<MyType2> o2 = new MyClass<MyType2>();
MyType2 myType2 = o2.myMethod(anotherJsonString);

get an instance of MyType2

I'm seeing many posts related to TypeReference and JavaType usage but not able to get hold of exactly how to fit them for my above use case.

My main objective is that MyClass should actually act as a util class without knowing at compile time what is the Java object it will convert the input json string to. It should decide that at runtime only.

Any help will be highly appreciated !

Many thanks and Best Regards

解决方案

Below solution works for you. Have a private instance of type Class and initialize it in constructor.

import org.codehaus.jackson.map.ObjectMapper;

public class MyClass<T> implements IMyInterface<String, T> {

   private Class<T> clazz;

   public MyClass(Class<T> clazz) {
    this.clazz = clazz;
   }

   @Override
   public T myMethod(String jsonString) throws Exception {
       ObjectMapper mapper = new ObjectMapper();
       T result = mapper.readValue(jsonString, clazz); 
       return result;
   }

}

You client code will be :

MyClass<MyType1> o1 = new MyClass<MyType1>(MyType1.class);
MyType1 myType1 = o1.myMethod(aJsonString); 

ADDED answer for second question :

In spring context file, define your bean as below.

<bean id="simpleMyType1Class" class="com.yourpackage.MyClass">
<constructor-arg>
    <!-- this can be any full path to a class -->
   <value>com.yourpackage.MyType1</value> 
</constructor-arg>

In Java code, get its bean reference as below.

MyClass<MyType> o1 = (MyClass)context.getBean("simpleMyType1Class");

这篇关于使用泛型将Jackon json字符串转换为Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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