question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DeepEquals fails when order of array elements is different

See original GitHub issue

I am comparing two parsed strings and this has worked flawlessly for all of our tests so far. However, I have encountered a case where DeepEquals fails when the order of objects in an array isn’t identical. The test passes if I update one Json to match the other.

// data is a string (a .json file read as text)
JToken expectedJson = JToken.Parse(data).SelectToken(jPath);
// Content is a string (the response body as returned by RestSharp
JToken jsonResponse = JToken.Parse(Content);
return JToken.DeepEquals(expectedJson, jsonResponse);

Source/destination JSON

First, the JSON returned by the API:

{
  "isHohrResponse": true,
  "messages": [],
  "success": true,
  "data": {
    "totalPaid": 301,
    "requiresConfirmation": false,
    "jobs": [
      {
        "jobId": "600dea62-f084-4459-9e91-8e809884d130",
        "jobName": "Job I",
        "fee": 76,
        "totalPayed": 76,
        "paymentUserInfo": [
          {
            "id": "ab2b408d-1e44-408e-b8e4-df779e31e37d",
            "name": "Frederik25 D",
            "isStarted": true,
            "hasLiked": true
          }
        ],
        "requiresConfirmation": false
      },
      {
        "jobId": "ca0a14b4-0206-45ba-9c31-cd5fa7745058",
        "jobName": "Job H",
        "fee": 75,
        "totalPayed": 225,
        "paymentUserInfo": [
          {
            "id": "3a1a91cf-03f9-4f06-9679-c339ddc295d7",
            "name": "Frederik23 D",
            "isStarted": true,
            "hasLiked": true
          },
          {
            "id": "9e323b98-08dc-4bc0-854e-f42711258252",
            "name": "Frederik26 D",
            "isStarted": true,
            "hasLiked": true
          },
          {
            "id": "68ab2e64-6356-4263-a8cb-2953deb675b6",
            "name": "Frederik24 D",
            "isStarted": true,
            "hasLiked": true
          }
        ],
        "requiresConfirmation": false
      }
    ]
  }
}

Now, the JSON having the final paymentUserInfo objects in a different order:

{
  "isHohrResponse": true,
  "messages": [],
  "success": true,
  "data": {
    "totalPaid": 301,
    "requiresConfirmation": false,
    "jobs": [
      {
        "jobId": "600dea62-f084-4459-9e91-8e809884d130",
        "jobName": "Job I",
        "fee": 76,
        "totalPayed": 76,
        "paymentUserInfo": [
          {
            "id": "ab2b408d-1e44-408e-b8e4-df779e31e37d",
            "name": "Frederik25 D",
            "isStarted": true,
            "hasLiked": true
          }
        ],
        "requiresConfirmation": false
      },
      {
        "jobId": "ca0a14b4-0206-45ba-9c31-cd5fa7745058",
        "jobName": "Job H",
        "fee": 75,
        "totalPayed": 225,
        "paymentUserInfo": [
          {
            "id": "68ab2e64-6356-4263-a8cb-2953deb675b6",
            "name": "Frederik24 D",
            "isStarted": true,
            "hasLiked": true
          },
          {
            "id": "9e323b98-08dc-4bc0-854e-f42711258252",
            "name": "Frederik26 D",
            "isStarted": true,
            "hasLiked": true
          },
          {
            "id": "3a1a91cf-03f9-4f06-9679-c339ddc295d7",
            "name": "Frederik23 D",
            "isStarted": true,
            "hasLiked": true
          }
        ],
        "requiresConfirmation": false
      }
    ]
  }
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

11reactions
Exoowcommented, Sep 13, 2018

Made a custom difference checker that fires as soon as DeepEquals fails. Works like a charm. Still it would be nice to be able to ignore array order (boolean parameter to DeepEquals?)

4reactions
larenelgcommented, Sep 13, 2018

I found a workaround for now 😃 But it does involve creating a class to deserialize the json

var expectedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<SomeClass>(expected));
var mappedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<SomeClass>(mapped));
Assert.True(JToken.DeepEquals(expectedJson, mappedJson));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect Arrays to be equal ignoring order
The idea here is to first determine if the length of the two arrays are same, then check if all elements are in...
Read more >
equals() and deepEquals() Method to Compare two Arrays ...
deepEquals () method compare recursively if an array contains another array. Arrays.equals(Object[], Object[]). Syntax : public static boolean ...
Read more >
How to Compare Arrays in Java – Equals vs deepEquals ...
There are two ways to compare arrays in Java, for checking if they are equal or not, by using equals() or deepEquals() method...
Read more >
Testing Arrays and Objects with Chai.js | by Titus Stone
Deep equality is an excellent approach, but it enforces both order and contents when comparing arrays. Often the case arises in testing ...
Read more >
5 Different Ways to Deep Compare JavaScript Objects
We can compare the values of any of these types using an equality operator. However, comparing non-primitive types such as objects is tricky ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found