3

I feel like I'm taking crazy pills here... but how can I compare two JsonDocuments (or JsonElements or what have you) for JSON equality, i.e. same keys with same values?

To limit the scope, the ability to perform this comparison in a unit test is sufficient unto the day.

I have a unit test in which I want to compare the result of a function that returns a JsonDocument to some expected value. Things that don't work include using FluentAssertions.Json (because my type is not JObject), and comparing the value of GetRawText because I don't care about the whitespace.

I guess I could write the strings out and re-serialize them or something but this honestly feels like such a hack that I must be doing something wrong.

I understand the business logic of comparing them, I have seen the other questions like this and this. The first is much more in keeping with what I want, it's just an embarrassing result for C#... The second is not what I need at all.

Ben
  • 4,980
  • 3
  • 43
  • 84
  • only in the most disappointing sense. I don't have any difficulty understanding the business logic required to perform such a comparison. I just have difficulty understanding how it could be that homerolling the comparison logic is viewed as acceptable by the C# community. – Ben Jan 13 '22 at 18:29
  • 1
    related: https://github.com/fluentassertions/fluentassertions/issues/1212 – Christoph Lütjen Jan 13 '22 at 18:38
  • Very unclear what you are asking - clearly you are not looking for libraries (off-topic), you claim that essentially identical "deep equal" question is absolutely not what you are looking for... Please [edit] the question to clarify what you hope to see as answer. – Alexei Levenkov Jan 13 '22 at 18:40
  • @ChristophLütjen thanks, this looks very promising, and i do appreciate it – Ben Jan 13 '22 at 18:49
  • Haven't tried this, but could you `GetHash()` on the `JsonDocument` and then compare hashes? – Jonathan Jan 13 '22 at 18:49
  • @ChristophLütjen your linked issue perfectly solved the problem. If you care to write it up StackOverflow style, I can accept it. – Ben Jan 13 '22 at 18:51
  • Here is a library I found that has a `DeepEquals` method for comparing JsonDocument, JsonElement and JsonNode: https://github.com/weichch/system-text-json-jsondiffpatch – James Lieu Jan 11 '23 at 12:25

1 Answers1

2

Thanks to @ChristophLütjen for identifying the fix I needed in this issue

Given two JsonDocuments I can compare them using FluentAssertions like this:

Doc1.RootElement.Should().BeEquivalentTo(Doc2.RootElement, opt => opt.ComparingByMembers<JsonElement>());

I'm still bemused that there is not a more straightforward comparison method out there for the general case, but since my immediate use was unit testing, I'm satisfied with this result.

Ben
  • 4,980
  • 3
  • 43
  • 84
  • 4
    that didn't work for me. When testing bad values to ensure the test failed. My test did not fail – McFrank Nov 16 '22 at 20:57
  • **DO NOT USE THIS!** it just always returns true. try it yourself on a non-equal documents. – Rast Jun 28 '23 at 01:35