Java:Gson自定义串行器和解串器的特殊要求 [英] Java: Gson custom serializer and deserializer special requirement

查看:93
本文介绍了Java:Gson自定义串行器和解串器的特殊要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何实现JsonDeserializer和JsonSerializer,但这里是我想要做的:



这是设置:

  public class classA 
{
public String a;
public classB clazzB;
}

公共类classB
{
public String c;
public String d;
public String e;
public String f;

$ / code>

我想序列化(和反序列化模拟)thisA: / b>


  • 在序列化classA时,它应该序列化classB的一部分以及变量c和d



    • 因为我知道如何做到这一点(对e和f使用瞬态),下面是catch:


      • c和d(应该用classA序列化的classB的部分)不应该在JSONObject中,它们应该使用来自classA的变量INLINE



      本质上,结果应该如下所示:

        {a:某物,c:某物,d:某物} 

      我在这里使用JsonSerializer是有2个变量(可能更多),我必须从classB序列化。如果只有一个,那么我可以返回一个变量的JSONPrimitive。我正在考虑在序列化方法中使用JsonTreeWriter,但我怀疑这是可以做到的。



      最简单的方法是什么?

      $ b $因为你基本上说你不想将你的对象序列化到实际的JSON等价物中,所以除了你做错了之外..

。你不要序列化 classB ,因为那不是你想要的。你想要一个JSON对象来自两个对象的字段。

您可以为A编写自定义的Deserializer和Serializer,并创建您尝试创建的JSON。

  class Class 
{
public String a =This is a;
public ClassB clazzB = new ClassB();
}

class ClassB
{
public String c =This is c;
public String d =这是d;
public String e;
public String f;
}


class ASerializer实现了JsonSerializer< ClassA>
{

public JsonElement serialize(ClassA t,Type type,JsonSerializationContext jsc)
{
JsonObject jo = new JsonObject();
jo.addProperty(a,t.a);
jo.addProperty(c,t.clazzB.c);
jo.addProperty(d,t.clazzB.d);
返回jo;




public class Test
{
pubic static void main(String [] args)
{
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(ClassA.class,new ASerializer());
String json = gson.create()。toJson(new ClassA());
System.out.println(json);


$ / code $ / pre
$ b $输出:


{a:This is a,c:This is c,d:This is d}


您也可以在该序列化器中进行序列化 ClassB 的路由,将元素添加到 ClassA 并返回。

I know how to implement JsonDeserializer and JsonSerializer, but here is what I want to do:

This is the setup:

public class classA
{
public String a;
public classB clazzB;
}

public class classB
{
public String c;
public String d;
public String e;
public String f;
}

I want to serialize (and deserialize analog to this) classA like this:

  • when serializing classA it should serialize a part of classB with it aswell, say variables c and d

Since I know how to do that (using transient for e and f) here is the catch:

  • c and d (the parts of classB that should be serialized with classA) should NOT be in a JSONObject, they should rather appear INLINE with the variable from classA

In essence the result should look something like this:

{"a":"something","c":"something", "d":"something"}

The problem I am having with JsonSerializer here is that there are 2 variables (could be more) I have to serialize from classB. If it were only one then I could just return a JSONPrimitive of the variable. I was thinking about using JsonTreeWriter in the serialize method but I doubt that can be done.

What is the most elegant way to do this?

解决方案

Other than "You're doing it wrong" since you're basically saying you don't want to serialize your Objects into the actual JSON equivalent... you don't serialize classB, because that's not what you want. You want a single JSON object with fields from two of your Objects.

You can just write a custom Deserializer and Serializer for A and create the JSON you're trying to create.

class ClassA
{
    public String a = "This is a";
    public ClassB clazzB = new ClassB();
}

class ClassB
{
    public String c = "This is c";
    public String d = "This is d";
    public String e;
    public String f;
}


class ASerializer implements JsonSerializer<ClassA>
{

    public JsonElement serialize(ClassA t, Type type, JsonSerializationContext jsc)
    {
        JsonObject jo = new JsonObject();
        jo.addProperty("a", t.a);
        jo.addProperty("c", t.clazzB.c);
        jo.addProperty("d", t.clazzB.d);
        return jo;
    }

}

public class Test 
{
    pubic static void main(String[] args) 
    { 
        GsonBuilder gson = new GsonBuilder();
        gson.registerTypeAdapter(ClassA.class, new ASerializer());
        String json = gson.create().toJson(new ClassA());
        System.out.println(json);
    }
}

Output:

{"a":"This is a","c":"This is c","d":"This is d"}

You could also go the route of serializing ClassB in that serializer, adding the elements in ClassA and returning that.

这篇关于Java:Gson自定义串行器和解串器的特殊要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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