使用 google-gson JSON 序列化/反序列化通用类型

发布于 2024-10-02 16:20:23 字数 636 浏览 0 评论 0原文

好吧,我必须承认我不擅长 Java 中的泛型类型

,我已经使用 JavaScriptSerializer 在 C# 中编写了一个 JSON 序列化/反序列化类,

    private static JavaScriptSerializer js = new JavaScriptSerializer();

    public static T LoadFromJSONString<T>(string strRequest)
    {
        return js.Deserialize<T>(strRequest);
    }

    public static string DumpToJSONString<T>(T rq)
    {
        return js.Serialize(rq);
    }

它在 C# 中运行良好。现在我正在尝试转换,或者至少,用 Java 编写另一个 JSON 序列化/反序列化类。我尝试过 flexjsongoogle-gson 但我不知道如何指定;在爪哇。

这里有人可以帮助我吗?顺便说一句,我更喜欢 google-gson

Well, I have to confess I'm not good at generic type in Java

I've written a JSON serialize/deserialize class in C# using JavaScriptSerializer

    private static JavaScriptSerializer js = new JavaScriptSerializer();

    public static T LoadFromJSONString<T>(string strRequest)
    {
        return js.Deserialize<T>(strRequest);
    }

    public static string DumpToJSONString<T>(T rq)
    {
        return js.Serialize(rq);
    }

It works well in C#. Now I'm trying to convert, or atleast, write another JSON serialize/deserialize class in Java. I've tried flexjson and google-gson but I don't know how to specify <T> in Java.

Could someone here help me? Btw, I prefer google-gson

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黒涩兲箜 2024-10-09 16:20:24

这应该有效:

import com.google.gson.Gson;
public class GenericClass {

    private static Gson gson = new Gson(); 

    public static <T> T loadFromJSONString(String strRequest)
    {
        return (T) gson.fromJson(strRequest, T.class);
    }

    public static <T> String dumpToJSONString(T rq)
    {
        return gson.toJson(rq);
    }
}

This should work:

import com.google.gson.Gson;
public class GenericClass {

    private static Gson gson = new Gson(); 

    public static <T> T loadFromJSONString(String strRequest)
    {
        return (T) gson.fromJson(strRequest, T.class);
    }

    public static <T> String dumpToJSONString(T rq)
    {
        return gson.toJson(rq);
    }
}
左耳近心 2024-10-09 16:20:23

在 Java 中,您必须传递实际的类型擦除类,而不仅仅是类型参数,以便数据绑定器知道要使用什么类型;所以类似:

public static T LoadFromJSONString<T>(string strRequest, Class<T> type)
{
    Gson gson = new Gson();
    return gson.fromJson(strRequest, type);
}

但这通常只是反序列化所需要的;序列化时,您有一个带有库可以使用的类的实例。

顺便说一句,您可能想考虑使用的另一个优秀的 Java JSON 库是 Jackson;但是,对于这个特定的示例,您提到的所有库都应该有效。

In Java you must pass actual type-erased class, not just type parameter, so data binders know what type to use; so something like:

public static T LoadFromJSONString<T>(string strRequest, Class<T> type)
{
    Gson gson = new Gson();
    return gson.fromJson(strRequest, type);
}

but this is usually just needed for deserialization; when serializing, you have an instance with a class which libraries can use.

Btw, one other good Java JSON library you might want to consider using is Jackson; however, for this particular example all libraries you mention should work.

只是偏爱你 2024-10-09 16:20:23

经过思考之后,我认为没有办法像 C# 代码中那样完美地解决这个问题。这是因为 java 编译器执行了类型擦除。

对您来说最好的解决方案可能是在您知道需要序列化/反序列化的对象类型的位置使用 Gson 对象。

如果您不热衷于每次都创建 Gson 对象的实例,那么您当然可以至少保持该实例为静态,因为它在创建时不需要任何类型参数。

After thinking this through I don't think there is a way to solve this as pretty as it is done in your C# code. This because of the type erasure done by the java compiler.

The best solution for you would probably be to use the Gson object at the location where you know the type of the object you need to serialize/deserialize.

If you're not keen on making instances of the Gson object everytime you can of course keep that one static at least, since it doesn't need any type parameters when created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文