我正在尝试使用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!
}
我可以正确地对OperationCreate
和OperationUpdate
进行序列化和反序列化,但是我在OperationAssign
上收到此错误。
System.NotSupportedException: Collection was of a fixed size.
我不明白为什么或如何进一步调试它。 OperationAssign
基本上只是将参数传递给基类(OperationCreate
和OperationUpdate
),这些基类可以成功序列化和反序列化。
如何更正此问题?
序列化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" })
{
}
答案 0 :(得分:3)
问题是您正在序列化然后尝试反序列化Attributes
OperationAssign
的集合 - 但Json.NET无法反序列化它,因为集合是只读的,属性是不可公开设定。
Json.NET能够成功反序列化基类OperationUpdate
,因为它有一个参数化的构造函数,并且具有与属性相同的名称(模数大小写)的参数,即attributes
。在这种情况下,Json.NET将调用构造函数并传入从JSON文件反序列化的“属性”的值。不幸的是,派生类省略了这个构造函数,因此反序列化失败。
您可以通过以下几种方法解决此问题:
使用适当的参数添加构造函数,并使用[JsonConstructor]
标记。它可能是私人的:
public class OperationAssign : OperationUpdate
{
[JsonConstructor]
OperationAssign(string entity, params string[] attributes) : this(entity)
{
}
public OperationAssign(string entity)
: base(entity, "ownerid")
{
}
}
如果需要,您可以忽略反序列化属性的值。该参数只需要存在。
使用conditional property serialization禁止对派生类中的属性进行序列化:
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; }
}
基类必须通过虚拟ShouldSerializeAttributes()
方法支持此功能。
在基类中使用[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;
}
}
显示选项的示例fiddle。
答案 1 :(得分:0)
我需要一个额外的构造函数来从Json.net获取数组输入。
[JsonConstructor]
public OperationAssign(string entity, params string[] attributes)
: base(entity, attributes)
{
}