开发者

GSON: Typing JSON data to Java class where class name is stored in string

开发者 https://www.devze.com 2023-03-19 03:00 出处:网络
I\'m using Google\'s GSON Library to convert JSON data to Java objects. The issue is that class name of the Java objet is being passed in the JSON data as well, so it is available only as a string. I\

I'm using Google's GSON Library to convert JSON data to Java objects. The issue is that class name of the Java objet is being passed in the JSON data as well, so it is available only as a string. I'm not too familiar with Java so I don't understand how to declare an object when the class name is stored as a string.

Also, the same is the case for the method that I will be calling after the data is initialized.

Here's the relevant code so far:

开发者_开发百科
request = gson.fromJson( rawData.toString(), JSONRequest.class );

String method = request.getMethod();
String data = request.getData();
String dataClass = request.getDataClass();

// convert data into an object of dataClass and execute method by passing dataObject
dataClass dataObject = gson.fromJson( data, dataClass.class );
result = method( dataObject );

This seems like a very crude way to accomplish the data to object conversion. Is there a better way? If not, how can I fix the code?


With the example code in the original question, dataClass.class will always be String.class, which I doubt is the correct target type to deserialize to.

I think the following is what was intended to be demonstrated. (I'm not sure what implementation of JSONRequest was used in the original question. I'll just make one up.)

If the target type is really not known, and it's not a sub-type of some known parent type, then reflection would be necessary to invoke the method. (This assumes that the target method does not require any parameters.)

import java.io.FileReader;
import java.lang.reflect.Method;

import com.google.gson.Gson;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();

    // input JSON:
    // {
    //   "dataClass":"com.stackoverflow.q6647866.MyObject",
    //   "method":"myMethod",
    //   "data":"{\"name\":\"fubar\"}"
    // }
    JSONRequest request = gson.fromJson(new FileReader("input.json"), JSONRequest.class);

    Class targetClass = Class.forName(request.dataClass);
    System.out.println("targetClass: " + targetClass);
    Object dataObject = gson.fromJson(request.data, targetClass);

    Method method = targetClass.getMethod(request.method);
    method.invoke(dataObject);
  }
}

class JSONRequest
{
  String method;
  String data;
  String dataClass;
}

class MyObject
{
  String name;
  public void myMethod()
  {
    System.out.println("running my method...");
  }
}

This seems like a very crude way to accomplish the data to object conversion. Is there a better way?

That's about as good as it gets, if the target data type and method to invoke are both completely unknown and only provided in the incoming JSON.

0

精彩评论

暂无评论...
验证码 换一张
取 消