Skip to content

Commit 826f258

Browse files
authored
Merge pull request #1 from ChrisRega/0.3.1-dev
0.3.1 dev
2 parents 238ad5b + 6ab8672 commit 826f258

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json_diff_ng"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["ksceriath", "ChrisRega"]
55
edition = "2021"
66
license = "Unlicense"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Usage Example:
1313

1414
Option:
1515

16-
f : read input from json files
17-
d : read input from command line
16+
file : read input from json files
17+
direct : read input from command line
1818

1919
### Installation
2020

src/process.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,18 @@ pub fn match_json(value1: &Value, value2: &Value) -> Mismatch {
9999

100100
let mismatch: Vec<_> = replaced
101101
.into_iter()
102-
.flat_map(|(o, ol, n, _nl)| {
103-
(0..ol).map(move |i| (o + i, match_json(&a[o + i], &b[n + i]).keys_in_both))
102+
.flat_map(|(o, ol, n, nl)| {
103+
let max_length = ol.max(nl);
104+
(0..max_length).map(move |i| {
105+
(
106+
o + i,
107+
match_json(
108+
a.get(o + i).unwrap_or(&Value::Null),
109+
b.get(n + i).unwrap_or(&Value::Null),
110+
)
111+
.keys_in_both,
112+
)
113+
})
104114
})
105115
.collect();
106116

@@ -277,6 +287,22 @@ mod tests {
277287
assert_eq!(diff.left_only_keys, KeyNode::Nil);
278288
}
279289

290+
#[test]
291+
fn long_insertion_modification() {
292+
let data1 = r#"["a","b","a"]"#;
293+
let data2 = r#"["a","c","c","c","a"]"#;
294+
let diff = compare_jsons(data1, data2).unwrap();
295+
let diffs = diff.keys_in_both.absolute_keys_to_vec(None);
296+
// 1. is b!=c, second is a!=c, third is missing in data1 but c in data2
297+
assert_eq!(diffs.len(), 3);
298+
assert_eq!(
299+
diffs.last().unwrap().to_string(),
300+
r#"[l: 3] -> { null != "c" }"#
301+
);
302+
assert_eq!(diff.right_only_keys, KeyNode::Nil);
303+
assert_eq!(diff.left_only_keys, KeyNode::Nil);
304+
}
305+
280306
#[test]
281307
fn test_arrays_object_extra() {
282308
let data1 = r#"["a","b"]"#;

0 commit comments

Comments
 (0)