模糊深度对象比较

时间:2014-03-07 07:02:34

标签: c# json deep-copy object-comparison

我有一个返回一个包含很多字段的巨大对象的方法。像这样:

{
    Success: true,
    Timestamp: "07.03.2014",
    Items:
    [
        {
            Name: "A",
            Price: 13.37,
            OtherData: 123
        },
        {
            Name: "B",
            Price: 42,
            OtherData: 312
        }
    ]
}

我想将其返回值与.NET应用程序中的测试的参考值进行比较。但是,这里有一些问题:

  • 每次Timestamp字段都会更改
  • Price字段中的舍入也可能有所不同
  • Items的顺序并不重要

我想以最灵活的方式定义参考对象:

  • 省略不必要的字段
  • 不仅可以指定值,还可以指定规则(字符串的正则表达式,数字的范围/舍入等)

以下是参考定义应如何显示的示例:

{
    Success: true,
    Items:
    [
        {
            Name: "A",
            Price: "13.37",
        },
        {
            Name: "B",
            Price: 42,
        }
    ]
}

是否有任何类型的.NET库可以进行这样的比较?

1 个答案:

答案 0 :(得分:2)

最后我决定自己实施比较。这是代码,也许对某人有用:

public static class Utils
{
    public static bool SequencesMatch<TSource, TPattern>(IEnumerable<TSource> sequence, IEnumerable<TPattern> patterns, Func<TSource, TPattern, bool> matcher)
    {
        var items = sequence.Select(x => new SequenceItem<TSource>(x)).ToArray();
        var pats = patterns.Select(x => new SequenceItem<TPattern>(x)).ToArray();

        foreach (var item in items)
        {
            foreach (var pat in pats)
            {
                if (pat.Matched) continue;
                if (matcher(item.Value, pat.Value))
                {
                    item.Matched = pat.Matched = true;
                    break;
                }
            }
        }

        return items.All(x => x.Matched) && pats.All(x => x.Matched);
    }

    public static bool JsonObjectsMatch(JToken data, JToken reference)
    {
        if (reference.Type == JTokenType.Array)
            return SequencesMatch(data, reference, JsonObjectsMatch);

        if (reference.Type == JTokenType.Object)
        {
            var dataObj = data as JObject;
            var refObj = reference as JObject;

            if (dataObj == null || refObj == null)
                Assert.Fail("DataObject = '{0}', ReferenceObject = '{1}'", dataObj, refObj);

            foreach (var pty in refObj)
            {
                var dataValue = dataObj[pty.Key];
                if (dataValue == null || !JsonObjectsMatch(dataValue, pty.Value))
                    Assert.Fail("Objects differ at {0}: DataValue = '{1}', RefValue = '{2}'", pty.Key, dataValue, pty.Value);
            }

            return true;
        }

        if (reference.Type == JTokenType.Float)
        {
            var refFloat = reference.ToObject<float>();
            var dataFloat = data.ToObject<float>();

            if(Math.Abs(dataFloat - refFloat) > 0.001)
                Assert.Fail("Objects differ: DataValue = '{0}', RefValue = '{1}'", dataFloat, refFloat);

            return true;
        }

        return JToken.DeepEquals(data, reference);
    }

    private class SequenceItem<T>
    {
        public T Value { get; set; }
        public bool Matched { get; set; }

        public SequenceItem(T value)
        {
            Value = value;
        }
    }
}

一旦我有足够的时间,我可能应该把它变成一个合适的库并在GitHub上发布。