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

查看:212
本文介绍了忽略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,消息为检测到类型'MyNamespace.AxisAlignedRectangle'的属性'Envelope'的自引用循环.如果我在AxisAlignedRectangle中添加[JsonObject(IsReference = true)](如此处所述),则代码可以正常运行,但是会自动分配每个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.

推荐答案

您可以使用条件属性序列化:

[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天全站免登陆