Skip to content

[Feature]: JsonElement assertions #4178

@verdie-g

Description

@verdie-g

Problem Statement

I would like to compare JsonElement/Json with a detailed error pointing out the difference.

Currently, this code

var json1 = JsonElement.Parse("{ \"abc\": { \"def\" : 1.23 }  }");
var json2 = JsonElement.Parse("{ \"abc\": { \"def\": 1.23 } }");

await Assert.That(JsonElement.DeepEquals(json1, json2)).IsTrue(); // OK
await Assert.That(json1).IsEqualTo(json2);

fails with

Expected to be equal to { "abc": { "def": 1.23 } }
but found { "abc": { "def" : 1.23 }  }

at Assert.That(json1).IsEqualTo(json2)

Proposed Solution

I would like a behavior like this

var json1 = JsonElement.Parse("{ \"abc\": { \"def\" : 1.23 }  }");
var json2 = JsonElement.Parse("{ \"abc\": { \"def\": 1.23 } }");
var json3 = JsonElement.Parse("{ \"abc\": { \"def\": 4.56 } }");

await Assert.That(json1).IsEqualTo(json2); // Equivalent -> no throw
await Assert.That(json1).IsEqualTo(json3);
// Throws
// Expected to be equal to { "abc": { "def": 1.23 } }
//                        but found { "abc": { "def" : 4.56 }  }
// Differs at $.abc.def

Alternatives Considered

No response

Feature Category

Assertions

How important is this feature to you?

Nice to have - would improve my experience

Additional Context

No response

Contribution

  • I'm willing to submit a pull request for this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions