考虑我们有一个类型描述参与 AB 测试的结果
public class ABTest
{
[JsonProperty(Required = Required.Always)]
public ulong CountOfAs { get; set; }
[JsonProperty(Required = Required.Always)]
public ulong CountOfBs { get; set; }
[JsonProperty(Required = Required.Always)]
public ulong TotalCount { get; set; }
[JsonProperty(Required = Required.Always)]
[JsonConverter(typeof(SomeCustomTypeJsonConverter))]
public SomeCustomType SomeOtherField { get; set; }
[JsonModelValidation]
public bool IsValid() => CountOfAs + CountOfBs == TotalCount;
}
因此,每次ABTest反序列化实例时,我们都希望验证 A 组中的人数加上 B 组中的人数等于参与测试的总人数。
如何在 JSON.Net 中表达它?外部方法不太适合,因为此模型可能会在多个层次结构的任何地方找到。因此,它不能仅在两个单独的步骤中进行反序列化和验证。此外,我实际上并没有可能处于无效状态的反序列化对象,因此它应该是默认反序列化的一部分。
慕运维8079593
相关分类