Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Text.Json;
- var p = new JsonDifferenceDetector();
- JsonDifferenceDetector.Main(new string[]{});
- public class JsonDifferenceDetector
- {
- public static void Main(string[] args)
- {
- var x = new Example(){a = 1, b = "2", c = DateTime.Now};
- var y = new Example(){a = 1, b = "two", c = DateTime.Now.AddDays(1)};
- Compare(x, y);
- }
- public static void Compare(Example x, Example y)
- {
- var jso = new JsonSerializerOptions(){WriteIndented = true};
- string xs = JsonSerializer.Serialize(x, jso);
- string ys = JsonSerializer.Serialize(y, jso);
- // Console.WriteLine(xs);
- // Console.WriteLine(ys);
- Regex keyValue = new Regex("\"(?<key>\\w+)\": (?<val>.+)(,|)$");
- string[] xArr = xs.Split("\n");
- string[] yArr = ys.Split("\n");
- for (int i = 0; i < xArr.Length; i++)
- {
- var xLine = xArr[i];
- var yLine = yArr[i];
- if (xLine == yLine)
- {
- continue;
- }
- Match xm = keyValue.Match(xLine);
- Match ym = keyValue.Match(yLine);
- string key = xm.Groups["key"].Value;
- string xv = xm.Groups["val"].Value;
- string yv = ym.Groups["val"].Value;
- Console.WriteLine($"{key}: {xv} -> {yv}");
- }
- }
- }
- [Serializable]
- public class Example
- {
- public int a { get; set; }
- public string b { get; set; }
- public DateTime c { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement