Search code examples
c#json.netdiff

JsonDiffpatch().Diff() cannot compare email addresses and strings longer than 50 chars?


I am trying to compare two JSON strings using JsonDiffpatch().Diff().

var oldJson = {"Author":"admin","Description":"","LastEditedBy":"admin","LastModifiedDate":"8/3/2023 8:53:09 AM","Name":"Export-5","RecipientEmails":"aa@1.com,a@1.com,aaq@1.com,ad@1.com,af@1.com,a@1a.com,a@1v.com,za@1.com,ra@1.com,a@11.co"};

var currentJson = {"Author":"admin","Description":"","LastEditedBy":"admin","LastModifiedDate":"8/3/2023 8:58:14 AM","Name":"Export-5","RecipientEmails":"aa@1.co,a@1.com,aaq@1.com,ad@1.com,af@1.com,a@1a.com,a@1v.com,za@1.com,ra@1.com,a@11.co"}

JToken diff = new JsonDiffPatch().Diff(JToken.Parse(oldJson), JToken.Parse(currentJson));

It works fine when there is no email address in the string. But, with a list of email addresses, I get the difference the following:

"LastModifiedDate": [
  "8/3/2023 8:53:09 AM",
  "8/3/2023 8:58:14 AM"
]
"RecipientEmails": [
  "@@ -1,20 +1,19 @@\n aa@1.co\n-m\n ,a@1.com,aaq\n",
  0,
  2
]


Why am I getting the extra characters in the output? How can I get rid of them and give an array of differences?

Here is the code sample:

using System;
using JsonDiffPatchDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        string oldJson = "{\"RecipientEmails\":\"aa@1.com,a@1.com,aaq@1.com,ad@1.com,af@1.com,a@1a.com,a@1v.com,za@1.com,ra@1.com,a@11.co\"}";
        string currentJson = "{\"RecipientEmails\":\"aa@1.co,a@1.com,aaq@1.com,ad@1.com,af@1.com,a@1a.com,a@1v.com,za@1.com,ra@1.com,a@11.co\"}";
        JToken diff = new JsonDiffPatch().Diff(JToken.Parse(oldJson), JToken.Parse(currentJson));
        Console.WriteLine(diff);
    }
}

Solution

  • Use the simple text diff mode:

    var diff = new JsonDiffPatch(new Options
        {
            TextDiff = TextDiffMode.Simple
        })
        .Diff(JToken.Parse(oldJson), JToken.Parse(currentJson));
    

    The problem is not emails but the length of RecipientEmails, it seems that default mode (TextDiffMode.Efficient) generates git-style diff for long strings.