Compare Two JSON Strings in Rust
Do They Match?
This is how I'm comparing two json strings in Rust:
---
anyhow = "1.0.98"
serde_json = "1.0.140"
---
use Result;
use Value;
Output:
one and two match: true
one and three match: false
Notes
-
The
json_matches()
function does the comparison. -
It returns
true
orfalse
wrapped in a Result. - If either one of the JSON strings can't be parsed the Result is an Error.
- If they both parse, the comparison is made.
-
The order of the keys in the JSON Object doesn't matter. For example, these match even though the order is different:
-
I'm using
anyhow
for error handling. That's not required. It's just what I find easiest to deal with.
Comparison Testing
The impetus behind this post was setting up a way to get the output of the
-a
-- end of line --