gpt4 book ai didi

java - Gson 错误?反序列化器的存在会损害 Gson 中的序列化过程

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:28 26 4
gpt4 key购买 nike

在下面的示例中,我尝试序列化我的自定义类Couple,其中包含Point2D 类型的字段。

我发现,SErialzing 的过程取决于 DEserializer 的存在。

此外,如果存在反序列化器,则序列化过程会出错。

解串器如何影响逆向过程以及如何损坏逆向过程?

未使用Point2DInstanceCreator类。

package tests;

import java.awt.geom.Point2D;
import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;


public class Try07_Point2D {

static class Couple {
Point2D p1, p2;

public Couple() {
p1 = new Point2D.Double();
p2 = new Point2D.Double();
}

public Couple(double x1, double y1, double x2, double y2) {
p1 = new Point2D.Double(x1, y1);
p2 = new Point2D.Double(x2, y2);
}

void set(double x1, double y1, double x2, double y2) {
p1.setLocation(x1, y1);
p2.setLocation(x2, y2);
}

@Override
public boolean equals(Object object) {
if( object instanceof Couple) {
Couple ano = (Couple) object;
return
(p1 == null && ano.p1 == null || p1 != null && p1.equals(ano.p1)) &&
(p2 == null && ano.p2 == null || p2 != null && p2.equals(ano.p2));
}
return false;
}
}

static class Point2DInstanceCreator implements InstanceCreator<Point2D>{

@Override
public Point2D createInstance(Type type) {
return new Point2D.Double();
}

}

static class Point2DDeserializer implements JsonDeserializer<Point2D> {

@Override
public Point2D deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {

Point2D.Double ans = new Point2D.Double();
JsonObject obj = json.getAsJsonObject();

ans.x = obj.get("x").getAsDouble();
ans.y = obj.get("y").getAsDouble();

return ans;
}

}

public static void main(String[] args) {

GsonBuilder gb;
Gson gson0;

gb = new GsonBuilder();

gb = gb
.enableComplexMapKeySerialization()
.serializeNulls()
.setPrettyPrinting()
.setVersion(1.0)

// .registerTypeAdapter(Point2D.class, new Point2DInstanceCreator())
// .registerTypeAdapter(Point2D.class, new Point2DDeserializer())
;

gson0 = gb.create();

Couple value1;

value1 = new Couple(12, 13, 14, 15);



System.out.println("Version without deserializer:");
System.out.println( gson0.toJson(value1) );

gb = new GsonBuilder();

gb = gb
.enableComplexMapKeySerialization()
.serializeNulls()
.setPrettyPrinting()
.setVersion(1.0)

// .registerTypeAdapter(Point2D.class, new Point2DInstanceCreator())
.registerTypeAdapter(Point2D.class, new Point2DDeserializer())
;

gson0 = gb.create();



System.out.println("Version with deserializer:");
System.out.println( gson0.toJson(value1) );


}

}

输出如下:

Version without deserializer:
{
"p1": {
"x": 12.0,
"y": 13.0
},
"p2": {
"x": 14.0,
"y": 15.0
}
}
Version with deserializer:
{
"p1": {},
"p2": {}
}

最佳答案

这看起来有点像 Gson 中的一个错误。如果您注册一个 TypeAdaptor,它似乎同时用于序列化和反序列化。

要解决此问题,请让您的 Point2DDeserializer 实现 JsonDeserializer 和 JsonSerializer:

static class Point2DDeserializer implements JsonDeserializer<Point2D>, JsonSerializer<Point2D> {

@Override
public Point2D deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {

Point2D.Double ans = new Point2D.Double();
JsonObject obj = json.getAsJsonObject();

ans.x = obj.get("x").getAsDouble();
ans.y = obj.get("y").getAsDouble();

return ans;
}

@Override
public JsonElement serialize(final Point2D point2D, final Type type, final JsonSerializationContext jsonSerializationContext) {
return new Gson().toJsonTree(point2D);
}
}

关于java - Gson 错误?反序列化器的存在会损害 Gson 中的序列化过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com