Json.net集合具有固定的大小

本文关键字:net 集合 Json | 更新日期: 2023-09-27 18:05:41

我正在尝试使用Json.net序列化和反序列化以下类。

public class OperationBase
{
}
public class OperationCreate : OperationBase
{
    public string Entity
    {
        get;
        private set;
    }
    public IReadOnlyCollection<string> Attributes
    {
        get;
        private set;
    }
    public OperationCreate(string entity, params string[] attributes)
    {
        Contract.Requires(entity != null);
        Contract.Requires(attributes != null);
        Entity = entity;
        Attributes = attributes;
    }
}
public class OperationUpdate : OperationCreate
{
    public OperationUpdate(string entity, params string[] attributes)
        : base(entity, attributes)
    {
    }
}
public class OperationAssign : OperationUpdate
{
    public OperationAssign(string entity)
        : base(entity, "ownerid")
    {   
    }
}

使用以下代码:

public void SerialiseTest<T>(T t)
{
    string serialised = JsonConvert.SerializeObject(t, Formatting.Indented, new JsonSerializerSettings()
    {
        TypeNameHandling = TypeNameHandling.All,
    });
    ITraceWriter traceWriter = new MemoryTraceWriter();
    T deserialised = JsonConvert.DeserializeObject<T>(serialised, new JsonSerializerSettings()
    {
        TraceWriter = traceWriter,
        TypeNameHandling = TypeNameHandling.All,
    });
    deserialised.ShouldBeEquivalentTo(t);
}
[TestMethod]
public void Test()
{
    OperationCreate create = new OperationCreate("Create", new string[] { "ownerid" });
    OperationUpdate update = new OperationUpdate("Update", new string[] { "ownerid" });
    OperationAssign assign = new OperationAssign("Test");
    SerialiseTest(create);
    SerialiseTest(update);
    SerialiseTest(assign); //Exception!
}

我可以正确地序列化和反序列化OperationCreateOperationUpdate,但是我在OperationAssign上收到此错误。

System.NotSupportedException: Collection was of a fixed size.

我不明白为什么或者如何进一步调试它。OperationAssign基本上只是将参数传递给基类(OperationCreateOperationUpdate),可以成功地序列化和反序列化。

如何纠正这个问题?

序列化OperationAssign

{
  "$type": "Woodswork.Crm.Documenter.Data.Operations.OperationAssign, Woodswork.Crm.Documenter",
  "Entity": "Test",
  "Attributes": {
    "$type": "System.String[], mscorlib",
    "$values": [
      "ownerid"
    ]
  }
}

TraceWriter

2016-09-05T22:14:44.813 Verbose Resolved type 'Woodswork.Crm.Documenter.Data.Operations.OperationAssign, Woodswork.Crm.Documenter' to Woodswork.Crm.Documenter.Data.Operations.OperationAssign. Path '$type', line 2, position 95.
2016-09-05T22:14:44.813 Info Deserializing Woodswork.Crm.Documenter.Data.Operations.OperationAssign using creator with parameters: Entity. Path 'Entity', line 3, position 11.
2016-09-05T22:14:44.813 Verbose Resolved type 'System.String[], mscorlib' to System.String[]. Path 'Attributes.$type', line 5, position 40.
2016-09-05T22:14:44.813 Info Started deserializing System.String[]. Path 'Attributes.$values', line 6, position 16.
2016-09-05T22:14:44.813 Info Finished deserializing System.String[]. Path 'Attributes.$values', line 8, position 5.
2016-09-05T22:14:44.815 Info Started deserializing Woodswork.Crm.Documenter.Data.Operations.OperationAssign. Path '', line 10, position 1.
2016-09-05T22:14:44.824 Error Error deserializing Woodswork.Crm.Documenter.Data.Operations.OperationAssign. Collection was of a fixed size. Path '', line 10, position 1.

备选OperationAssign构造函数

这也会导致相同的错误。

public OperationAssign(string entity)
    : base(entity, new string[] { "ownerid" })
{   
}

Json.net集合具有固定的大小

问题是您正在序列化然后试图反序列化OperationAssignAttributes集合-但Json。NET无法对其进行反序列化,因为该集合是只读的,并且该属性不可公开设置。

Json。. NET能够成功地反序列化基类OperationUpdate,因为它有一个参数化的构造函数,并且具有与属性同名(模情况)的参数,即attributes。在这种情况下Json。. NET将调用构造函数并传入从JSON文件反序列化的"attributes"的值。不幸的是,派生类省略了此构造函数,因此反序列化失败。

有几种方法可以解决这个问题:

  1. 添加一个带有适当参数的构造函数,并用[JsonConstructor]标记。可以是private:

    public class OperationAssign : OperationUpdate
    {
        [JsonConstructor]
        OperationAssign(string entity, params string[] attributes) : this(entity)
        {
        }
        public OperationAssign(string entity)
            : base(entity, "ownerid")
        {   
        }
    }
    

    如果需要,可以忽略反序列化属性的

  2. 使用条件属性序列化来抑制派生类中属性的序列化:

    public class OperationCreate : OperationBase
    {
        public string Entity
        {
            get;
            private set;
        }
        public IReadOnlyCollection<string> Attributes
        {
            get;
            private set;
        }
        public virtual bool ShouldSerializeAttributes() { return true; }
        public OperationCreate(string entity, params string[] attributes)
        {
            Contract.Requires(entity != null);
            Contract.Requires(attributes != null);
            Entity = entity;
            Attributes = attributes;
        }
    }
    public class OperationAssign : OperationUpdate
    {
        public OperationAssign(string entity)
            : base(entity, "ownerid")
        {   
        }
        public override bool ShouldSerializeAttributes() { return false; }
    }
    
  3. 基类必须通过一个虚拟的ShouldSerializeAttributes()方法来支持。
  4. 在基类中用[JsonProperty]标记属性。这将强制调用私有setter:

    public class OperationCreate : OperationBase
    {
        public string Entity
        {
            get;
            private set;
        }
        [JsonProperty]
        public IReadOnlyCollection<string> Attributes
        {
            get;
            private set;
        }
        public OperationCreate(string entity, params string[] attributes)
        {
            Contract.Requires(entity != null);
            Contract.Requires(attributes != null);
            Entity = entity;
            Attributes = attributes;
        }
    }
    

显示选项的示例小提琴

我需要一个额外的构造函数来接受来自Json.net的数组输入。

[JsonConstructor]
public OperationAssign(string entity, params string[] attributes)
    : base(entity, attributes)
{
}