Click here to Skip to main content
15,561,246 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I could use any help with this problem.. I am using JsonDiffer GitHub - aminm-net/JsonDiffer.Netstandard: A lightweight utility to compare JSON objects and hence practically any serialize-able entity[^]

to compare two json. The two Json is the same and comes from the same API call. I am using just applying the Json to map to objects and the Deserializing it and serializing and stored as expected. Then I am using IRestResponse and getting string json that is my acutal. After that I am using JsonDiff I get the Json are different.. I do not understand why. Please take a look at all the info below I have provided everything to get a clear picture. Thank you :)

//Here is my method for comparison
public class JsonHelper<TModel>
{

 public static void ValidateTwoEqualJsonContent(IRestResponse response, TModel modelobj)
        {
             var data = JsonConvert.DeserializeObject<TModel>(response.Content);
            string expected = JsonConvert.SerializeObject(data);
            var acutal = response.Content;
            var a = JToken.Parse(expected);
            var b = JToken.Parse(acutal);
            JToken res = JsonDifferentiator.Differentiate(a, b);
            string result = "";
            if (res != null)
                result = res.ToString();
            Assert.IsTrue(res == null, "Differerence of expected and actual are: " + result);


        }
}



//Json response
{
  "canEdit": true,
  "allowedTagTypes": [
    0,
    1,
    4
  ],
  "item": {
    "query": null,
    "type": 0,
    "filter": null,
    "ldapPaths": null,
    "editPermissions": 0,
    "endpointsCount": 0,
    "id": 404,
    "disallowed": false,
    "name": "Tag"
  },
  "filterConfigData": {
    "config": [
      {
        "type": 5,
        "id": "Endpoints.ID.SearchInProgress",
        "extraData": null,
        "title": "Search in Progress"
      },
      {
        "source": {
          "definition": "dbo.func_Reports_GetPoliciesForUser(N'~USER_ID~', N'~USERS_SERVICE_KEY~')",
          "keyColumn": "Id",
          "displayColumn": "Name",
          "type": 1,
          "name": "Policies"
        },
        "data": null,
        "type": 3,
        "id": "Endpoints.ID.Policies",
        "extraData": null,
        "title": "Policies"
      },
      {
        "type": 1,
        "id": "Endpoints.ID.MACs",
        "extraData": null,
        "title": "MAC Addresses"
      },
      {
        "source": {
          "definition": "ClientActivityState",
          "keyColumn": "Id",
          "displayColumn": "Name",
          "type": 2,
          "name": "ClientActivityState"
        },
        "data": [
          {
            "name": "Endpoint Closed",
            "value": 11
          },
          {
            "name": "Endpoint Completed",
            "value": 15
          },
          {
            "name": "Endpoint Opened",
            "value": 10
          },
          {
            "name": "Endpoint Paused",
            "value": 13
          },
          {
            "name": "Endpoint Searching",
            "value": 12
          },
          {
            "name": "Endpoint Stopped",
            "value": 14
          },
          {
            "name": "Executed",
            "value": 4
          },
          {
            "name": "Failed",
            "value": 5
          },
          {
            "name": "None",
            "value": 0
          },
          {
            "name": "Offline",
            "value": 19
          },
          {
            "name": "Search Canceled",
            "value": 8
          },
          {
            "name": "Search Completed",
            "value": 9
          },
          {
            "name": "Search Paused",
            "value": 7
          },
          {
            "name": "Search Started",
            "value": 6
          },
          {
            "name": "Skipped",
            "value": 3
          },
          {
            "name": "Task Acknowledged",
            "value": 1
          },
          {
            "name": "Task Initiated",
            "value": 2
          },
          {
            "name": "Task Paused",
            "value": 20
          },
          {
            "name": "Upgrade Delayed",
            "value": 17
          },
          {
            "name": "Upgrade Failed",
            "value": 18
          },
          {
            "name": "Upgrade Successful",
            "value": 16
          }
        ],
        "type": 3,
        "id": "Endpoints.ID.ClientActivityState",
        "extraData": null,
        "title": "Client Activity State"
      },
      {
        "source": {
          "definition": "Import.AceType",
          "keyColumn": "Id",
          "displayColumn": "Name",
          "type": 2,
          "name": "AceType"
        },
        "data": [
          {
            "name": "Allow",
            "value": 0
          },
          {
            "name": "Deny",
            "value": 1
          },
          {
            "name": "System Alarm",
            "value": 3
          },
          {
            "name": "System Audit",
            "value": 2
          }
        ],
        "type": 3,
        "id": "Endpoints.ID.AceType",
        "extraData": null,
        "title": "ACL: ACE Type"
      },
      {
        "type": 1,
        "id": "Endpoints.ID.AceWho",
        "extraData": null,
        "title": "ACL: Trustee"
      },
      {
        "source": {
          "definition": "Import.AceRights",
          "keyColumn": "Id",
          "displayColumn": "Name",
          "type": 2,
          "editor": "Endpoints.AceRightsFilterSelectorViewModel",
          "name": "AceRights"
        },
        "data": [
          {
            "name": "Append Data",
            "value": 4
          },
          {
            "name": "Delete",
            "value": 65536
          },
          {
            "name": "Execute",
            "value": 32
          },
          {
            "name": "Full Control",
            "value": 2032127
          },
          {
            "name": "Generic Execute",
            "value": 1179808
          },
          {
            "name": "Generic Read",
            "value": 1179785
          },
          {
            "name": "Generic Write",
            "value": 1179926
          },
          {
            "name": "No Access",
            "value": 0
          },
          {
            "name": "Read Acl",
            "value": 64
          },
          {
            "name": "Read Attributes",
            "value": 128
          },
          {
            "name": "Read Control",
            "value": 131072
          },
          {
            "name": "Read Data",
            "value": 1
          },
          {
            "name": "Read Extended Attributes",
            "value": 8
          },
          {
            "name": "Synchronize",
            "value": 1048576
          },
          {
            "name": "Take Ownership",
            "value": 524288
          },
          {
            "name": "Write Acl",
            "value": 262144
          },
          {
            "name": "Write Attributes",
            "value": 256
          },
          {
            "name": "Write Data",
            "value": 2
          },
          {
            "name": "Write Extended Attributes",
            "value": 16
          }
        ],
        "type": 3,
        "id": "Endpoints.ID.AceRights",
        "extraData": null,
        "title": "ACL: Authorization"
      },
      {
        "type": 1,
        "id": "Endpoints.ID.FilterTagName",
        "extraData": null,
        "title": "Tag Name"
      },
      {
        "source": {
          "definition": "dbo.func_Reports_GetTagsForUser(N'~USER_ID~', N'~USERS_SERVICE_KEY~')",
          "keyColumn": "Id",
          "displayColumn": "Name",
          "type": 1,
          "name": "Tags"
        },
        "data": null,
        "type": 3,
        "id": "Endpoints.ID.FilterTags",
        "extraData": null,
        "title": "Tags"
      },
      {
        "type": 1,
        "id": "Endpoints.Name.EndpointName",
        "extraData": null,
        "title": "Endpoint Name"
      },
      {
        "type": 1,
        "id": "Endpoints.Version.Version",
        "extraData": null,
        "title": "Endpoint Version"
      },
      {
        "type": 1,
        "id": "Endpoints.Platform.Platform",
        "extraData": null,
        "title": "Endpoint Platform"
      },
      {
        "source": {
          "definition": "PlatformType",
          "keyColumn": "Id",
          "displayColumn": "Name",
          "type": 2,
          "name": "PlatformType"
        },
        "data": [
          {
            "name": "Desktop",
            "value": 1
          },
          {
            "name": "Server",
            "value": 2
          },
          {
            "name": "Unknown",
            "value": 0
          }
        ],
        "type": 3,
        "id": "Endpoints.PlatformType.PlatformType",
        "extraData": null,
        "title": "Platform Type"
      },
      {
        "type": 2,
        "id": "Endpoints.LastPoll.LastPoll",
        "extraData": null,
        "title": "Last Poll Time"
      },
      {
        "type": 1,
        "id": "Endpoints.EndpointGuid.EndpointGuid",
        "extraData": null,
        "title": "Endpoint GUID"
      },
      {
        "type": 0,
        "id": "90be0e17-f727-4438-8118-dcf3d3e58c08",
        "extraData": null,
        "title": "Match Quantity"
      },
      {
        "type": 0,
        "id": "b140b2d4-9de0-4492-acb7-a4f22cdcf394",
        "extraData": null,
        "title": "Protected Quantity"
      },
      {
        "type": 0,
        "id": "4496ca24-f4a3-4769-b212-fd6b4c029da1",
        "extraData": null,
        "title": "Unprotected Quantity"
      },
      {
        "type": 0,
        "id": "8add87db-5d70-433e-a885-7bf4296f3405",
        "extraData": null,
        "title": "Number of Searches"
      },
      {
        "type": 2,
        "id": "4aa90777-a1c5-4d87-890d-01a62469e34a",
        "extraData": null,
        "title": "Search Date/Time"
      }
    ],
    "pagedListItems": null
  },
  "forests": null,
  "adRoots": null
}



//Here is my model class
public class GetTagByIdDTO 
        {
            public static string ExpectedData(IRestResponse response)
            {
                var data = JsonConvert.DeserializeObject<GetTagByIdDTO>(response.Content);
                var json = JsonConvert.SerializeObject(data);
                return json;
            }

            public class Datum
            {
                public static IRestResponse response;
                public string name { get; set; }
                public int value { get; set; }

            }

            public class Source
            {
                public string definition { get; set; }
                public string displayColumn { get; set; }
                public string keyColumn { get; set; }
                public string name { get; set; }
                public int type { get; set; }
                public string editor { get; set; }

            }

            public class Config
            {
                public object extraData { get; set; }
                public string id { get; set; }
                public string title { get; set; }
                public int type { get; set; }
                public List<Datum> data { get; set; }
                public Source source { get; set; }

            }

            public class FilterConfigData
            {
                public List<Config> config { get; set; }
                public object pagedListItems { get; set; }

            }

            public class Item
            {
                public bool disallowed { get; set; }
                public int editPermissions { get; set; }
                public int endpointsCount { get; set; }
                public object filter { get; set; }
                public int id { get; set; }
                public object ldapPaths { get; set; }
                public string name { get; set; }
                public object query { get; set; }
                public int type { get; set; }

            }


            public bool canEdit { get; set; }
            public List<int> allowedTagTypes { get; set; }
            public Item item { get; set; }
            public FilterConfigData filterConfigData { get; set; }
            public object forets { get; set; }
            public object adRoots { get; set; }


        }


//Usage
JsonHelper<GetTagByIdDTO>.ValidateTwoEqualJsonContent(restResponse, GetTagByIdDTO.ExpectedData(restResponse));



//Here is the difference
"*filterConfigData": {
        "*config": [
          {
            "-data": null,
            "-source": null
          },
          {
            "*source": {
              "-editor": null
            }
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "*source": {
              "-editor": null
            }
          },
          {
            "*source": {
              "-editor": null
            }
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "*source": {
              "-editor": null
            }
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "*source": {
              "-editor": null
            }
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          },
          {
            "-data": null,
            "-source": null
          }
        ]
      },
      "-forets": null,
      "+forests": null
    }


What I have tried:

I have tried another library JsonDiffPath from nuget same issue.
Posted
Updated 23-Aug-20 23:42pm
v3
Comments
Richard MacCutchan 21-Jul-20 3:56am    
Use the debugger to examine the actual content of the two objects (a and b) to see why they are different.

1 solution

Hi,

I hope it is not too late,The reason you get the following:

Quote:
{
"-data": null,
"-source": null
},


Is probably due to your sterilizing settings,checkout: NullValueHandling

ANd for the last part there is a typo detected:
Quote:
"-forets": null,
"+forests": null
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900