小编Nok*_*tai的帖子

反序列化数组中的项目时忽略自定义 JsonConverter

编辑:制作了一个更简单、更透明的示例案例

我正在尝试反序列化一组组件(属于一个实体)。其中一个组件是 Sprite 组件,它保存纹理和动画信息。我为此实现了一个 CustomConverter,因为原始精灵类有点臃肿,而且没有无参数构造函数(该类来自单独的库,因此我无法修改它)。

实际用例有点复杂,但我在下面添加了一个类似的示例。我测试了代码,出现了同样的问题。反序列化时从不使用 ReadJson。但是当序列化 WriteJson 时确实被调用得非常好。

这些是组件和它的自定义转换器;

 public class ComponentSample
{
    int entityID;
}


public class Rect
{
    public int x;
    public int y;
    public int width;
    public int height;

    public Rect( int x, int y, int width, int height )
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
}

//this class would normally have a texture and a bunch of other data that is hard to serialize
//so we will …
Run Code Online (Sandbox Code Playgroud)

c# serialization json json.net deserialization

5
推荐指数
1
解决办法
957
查看次数

标签 统计

c# ×1

deserialization ×1

json ×1

json.net ×1

serialization ×1