Json.net JsonIgnore不适用于嵌套类

时间:2018-02-06 13:27:04

标签: c# json json.net

我正在尝试使用Json.net序列化一些第三方json,问题是他们开始将Ids作为Guids的字符串发送。所以我试图忽略序列化中的ID,但嵌套属性中似乎存在JsonIgnore不起作用的问题。出于这个原因,我决定添加自己的ID,但序列化本身似乎并没有忽略我需要它。

我的类用于序列化:

public class ItemA: General.Common
{
    [JsonIgnore]
    public new Guid Id { get; set; } //hiding Guid Id Common
    public Folder ParentFolder { get; set; }
    //...
}

public class Folder
{
    public string Id { get; set; }

    public string Path { get; set; }
}

public class ItemB: NotImportant
{
    //...
    public List<Folder> Folders { get; set; } = new List<Folder>();
    public List<ItemA> ItemAs{ get; set; } = new List<ItemA>();
}

我的代码:

        var jsonSettings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };

        string json = "some Json-that includes some Ids as strings";
        dynamic d = JsonConvert.DeserializeObject<ItemB>(json, jsonSettings);
        //serialization error here that cannot convert string to Guid in ItemAs 
        ((ItemB)d).ItemAs.ForEach(x => x.Id = new Guid());

编辑: 错误是这样的:

Error converting value "RgAAAAAD01CCe0GCRpDdKTQq2OCQBwAIuTruAfDrRZi9RPZnww3OAAAAAAEMAAAIuTruAfDrRZi9RPZnww3OAABE1hqaAAAA" to type 'System.Guid'...

2 个答案:

答案 0 :(得分:2)

有关于此的官方问题:

Issue #463 - JsonIgnore attribute on shadowed properties

它们是单独的属性,一个恰好隐藏另一个属性。通过忽略Derived上的属性,基础上的属性不再被隐藏,而是被序列化。如果你想忽略它们,那么在它们上面放置[JsonIgnore],或者如果你希望Derived类上的[JsonIgnore]忽略两者,那么将属性虚拟并在Derived上覆盖它。 - {{3 }}

答案 1 :(得分:1)

在反序列化期间可以忽略:基于代码的完整工作示例:(参见Ignore a property when deserializing using Json.Net with ItemRequired = Required.Always

using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
[JsonObject(ItemRequired = Required.Always)]
public class ItemA : General.Common
{
    [JsonIgnore]
    [JsonProperty(Required = Required.Default)]
    public new Guid Id { get; set; } //hiding Guid Id Common
    public Folder ParentFolder { get; set; }
    //...
}

public class Folder
{
    public string Id { get; set; }

    public string Path { get; set; }
}

public class ItemB : NotImportant
{
    //...
    public List<Folder> Folders { get; set; } = new List<Folder>();
    public List<ItemA> ItemAs { get; set; } = new List<ItemA>();
}
public class Test
{



    var jsonSettings = new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore,
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    };
   //ItemB b = new ItemB()
    //{
    //    Folders = new List<Folder>() {
    //        new Folder() { Id = "1", Path = "myPath1" },
    //        new Folder() { Id = "2", Path = "myPath2" },
    //        new Folder() { Id = "3", Path = "myPath3" } },
    //    ItemAs = new List<ItemA>() {
    //        new ItemA() { Id = Guid.NewGuid(), ParentFolder = new Folder()
    //        { Id = "p1", Path = "parentpath1" } },
    //new ItemA()
    //{ Id = Guid.NewGuid(),
    //    ParentFolder = new Folder()
    //{ Id = "p2", Path = "parentpath2" } }}
    //};
    //string json = JsonConvert.SerializeObject(b);
    string json = "{\"Folders\":[{\"Id\":\"1\",\"Path\":\"myPath1\"},{\"Id\":\"2\",\"Path\":\"myPath2\"},{\"Id\":\"3\",\"Path\":\"myPath3\"}],\"ItemAs\":[{\"Id\":\"RgAAAAAD01CCe0GCRpDdKTQq2OCQBwAIuTruAfDrRZi9RPZnww3OAAAAAAEMAAAIuTruAfDrRZi9RPZnww3OAABE1hqaAAAA\",\"ParentFolder\":{\"Id\":\"p1\",\"Path\":\"parentpath1\"}},{\"Id\":\"c0af33a9-3e6f-4405-a2d4-ff469cb67fce\",\"ParentFolder\":{\"Id\":\"p2\",\"Path\":\"parentpath2\"}}]}";
    dynamic d = JsonConvert.DeserializeObject<ItemB>(json, jsonSettings);
    //no serialization error 
    ((ItemB)d).ItemAs.ForEach(x => x.Id = Guid.NewGuid());

}

results screenshot