忽略 Json.NET 序列化中的基类属性 [英] Ignore Base Class Properties in Json.NET Serialization

查看:27
本文介绍了忽略 Json.NET 序列化中的基类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类结构:

[JsonObject]
public class Polygon : IEnumerable<Point>
{
    public List<Point> Vertices { get; set; }
    public AxisAlignedRectangle Envelope { get; set; }
}

public class AxisAlignedRectangle : Polygon {
    public double Left { get; set; }
    ...
}

我正在序列化 Polygon 类,但是当我这样做时,我收到一个 JsonSerializationException,并显示消息检测到属性 'Envelope' 的自引用循环,类型为 'MyNamespace.AxisAlignedRectangle'."如果我将 [JsonObject(IsReference = true)](如此处所述)添加到 AxisAlignedRectangle,代码运行良好,但我在 AxisAlignedRectangle 的每个实例中都会得到一个自动分配的 $id 字段,并且在重新引用该实例时会得到一个 $ref 字段.例如,当我序列化一个多边形时,我得到:

I am serializing the Polygon class, but when I do, I get a JsonSerializationException, with the message "Self referencing loop detected for property 'Envelope' with type 'MyNamespace.AxisAlignedRectangle'." If I add [JsonObject(IsReference = true)] (as described here) to AxisAlignedRectangle, the code runs fine, but I get an auto-assigned $id field in each instance of AxisAlignedRectangle, and a $ref field when that instance is re-referenced. For example, when I serialize a polygon, I get:

{
    Vertices: [ ... ],
    Envelope: {
        $id: '1',
        Left: -5,
        ...
        Vertices: [ ... ],
        Envelope: {
            $ref: '1'
        }
    }
}

我希望在序列化 AxisAlignedRectangle 时完全删除 Polygon 属性.我尝试将 DataContractAttribute 添加到 AxisAlignedRectangle 类(以及适当的 DataMemberAttribute 属性),但 Polygon 的所有属性仍在序列化.这是出乎意料的,因为 Json.NET 文档 中有一个示例这似乎表明这种方法应该有效.

My desire is to remove the Polygon properties entirely when I serialize an AxisAlignedRectangle. I tried adding a DataContractAttribute to the AxisAlignedRectangle class (along with appropriate DataMemberAttribute attributes), but all the properties of Polygon were still being serialized. This was unexpected, since there is an example in the Json.NET documentation that appears to indicate such an approach should work.

当被序列化的类型是 AxisAlignedRectangle 时,有谁知道从生成的 Json.NET 序列化中显式删除(最重要的是)Envelope 属性的方法?谢谢.

Does anyone know a way to explicitly remove (most importantly) the Envelope property from the resulting Json.NET serialization, when the type being serialized is AxisAlignedRectangle? Thanks.

推荐答案

您可以使用 条件属性序列化,通过像这样定义你的类:

You can use conditional property serialization, by defining your classes like this:

[JsonObject]
public class Polygon : IEnumerable<Point>
{
    public List<Point> Vertices { get; set; }
    public AxisAlignedRectangle Envelope { get; set; }

    public virtual bool ShouldSerializeEnvelope()
    {
        return true;
    }
}

public class AxisAlignedRectangle : Polygon
{
    public double Left { get; set; }
    ...

    public override bool ShouldSerializeEnvelope()
    {
        return false;
    }
}

我已在以下位置发布了完整的解决方案:https://github.com/thiagoavelino/VisualStudio_C/blob/master/VisualStudio_C/StackOverFlow/ParsingJason/EnvelopePolygonProblem.cs

I have posted the full solution at: https://github.com/thiagoavelino/VisualStudio_C/blob/master/VisualStudio_C/StackOverFlow/ParsingJason/EnvelopePolygonProblem.cs

这篇关于忽略 Json.NET 序列化中的基类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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