14

たとえば、次の json テキストがあるとします。

 object1{
     field1: value1;
     field2: value2;
     field3: value3;
 }

 object1{
     field1: value1;
     field2: newvalue2;
     field3: value3;
 }

そのファイルを読み取り、違いを示すC#の何かが必要です。つまり、次のオブジェクトを返すことができます:

differences {
    object1: { field: field2, old_value: value2, new_value: newvalue2}
}

これを行うための API または提案はありますか?

4

3 に答える 3

5

Weakly-Typed JSON Serializationを使用し、次のようにJsonObjectを使用するルーチンを作成することをお勧めします。

String JsonDifferenceReport(String objectName,
                            JsonObject first,
                            JsonObject second)
{
  if(String.IsNullOrEmpty(objectName))
    throw new ArgumentNullException("objectName");
  if(null==first)
    throw new ArgumentNullException("first");
  if(null==second)
    throw new ArgumentNullException("second");
  List<String> allKeys = new List<String>();
  foreach(String key in first.Keys)
    if (!allKeys.Any(X => X.Equals(key))) allKeys.Add(key);
  foreach(String key in second.Keys)
    if (!allKeys.Any(X => X.Equals(key))) allKeys.Add(key);
  String results = String.Empty;
  foreach(String key in allKeys)
  {
    JsonValue v1 = first[key];
    JsonValue v1 = second[key];
    if (((null==v1) != (null==v2)) || !v1.Equals(v2))
    {
      if(String.IsNullOrEmpty(results))
      {
         results = "differences: {\n";
      }
      results += "\t" + objectName + ": {\n";
      results += "\t\tfield: " + key + ",\n";
      results += "\t\toldvalue: " + (null==v1)? "null" : v1.ToString() + ",\n";
      results += "\t\tnewvalue: " + (null==v2)? "null" : v2.ToString() + "\n";
      results += "\t}\n";
    }
  }
  if(!String.IsNullOrEmpty(results))
  {
    results += "}\n";
  }
  return results;
}

JsonValueここで行ったように単に文字列表現を使用するのではなく、v1 と v2内で再帰的にレポートを取得するかどうかを選択します。

再帰的にしたい場合は、上記を次のように変更できます。

  if ((null==v1) || (v1.JsonType == JsonType.JsonPrimitive)
   || (null==v2) || (v2.JsonType == JsonType.JsonPrimitive))
  {
    results += "\t\tfield: " + key + ",\n";
    results += "\t\toldvalue: " + (null==v1) ? "null" : v1.ToString() + ",\n";
    results += "\t\tnewvalue: " + (null==v2) ? "null" : v2.ToString() + "\n";
  }
  else
  {
    results + JsonDifferenceReport(key, v1, v2);
  }

-ジェシー

于 2012-06-17T17:18:30.760 に答える
1

何らかの理由で、Web API プロジェクトで JsonObject を使用できませんでした。私は JSON.Net を使用しました。以下は差分を取得する方法です。違いの配列を提供します。

private static string GetJsonDiff(string action, string existing, string modified, string objectType)
    {
        // convert JSON to object
        JObject xptJson = JObject.Parse(modified);
        JObject actualJson = JObject.Parse(existing);

        // read properties
        var xptProps = xptJson.Properties().ToList();
        var actProps = actualJson.Properties().ToList();

        // find differing properties
        var auditLog = (from existingProp in actProps
            from modifiedProp in xptProps
            where modifiedProp.Path.Equals(existingProp.Path)
            where !modifiedProp.Value.Equals(existingProp.Value)
            select new AuditLog
            {
                Field = existingProp.Path,
                OldValue = existingProp.Value.ToString(),
                NewValue = modifiedProp.Value.ToString(),
                Action = action, ActionBy = GetUserName(),
                ActionDate = DateTime.UtcNow.ToLongDateString(),
                ObjectType = objectType
            }).ToList();

        return JsonConvert.SerializeObject(auditLog);
    }
于 2016-02-10T10:26:39.977 に答える