Skip to content

Commit 94f464d

Browse files
committed
fix: correct column number in error message (#178)
1 parent 4708122 commit 94f464d

5 files changed

Lines changed: 42 additions & 31 deletions

File tree

src/error.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -521,73 +521,73 @@ mod test {
521521
let err = from_str::<Foo>("{ \"b\":[]}").unwrap_err();
522522
assert_eq!(
523523
format!("{err}"),
524-
"missing field `a` at line 1 column 8\n\n\t{ \"b\":[]}\n\t........^\n"
524+
"missing field `a` at line 1 column 9\n\n\t{ \"b\":[]}\n\t........^\n"
525525
);
526526

527527
let err = from_str::<Foo>("{\"a\": [1, 2x, 3, 4, 5]}").unwrap_err();
528528
println!("{err}");
529529
assert_eq!(
530530
format!("{err}"),
531531
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
532-
11\n\n\t\": [1, 2x, 3, 4,\n\t........^.......\n"
532+
12\n\n\t\": [1, 2x, 3, 4,\n\t........^.......\n"
533533
);
534534

535535
let err = from_str::<Foo>("{\"a\": null}").unwrap_err();
536536
assert_eq!(
537537
format!("{err}"),
538-
"invalid type: null, expected a sequence at line 1 column 9\n\n\t\"a\": \
538+
"invalid type: null, expected a sequence at line 1 column 10\n\n\t\"a\": \
539539
null}\n\t........^.\n"
540540
);
541541

542542
let err = from_str::<Foo>("{\"a\": [1,2,3 }").unwrap_err();
543543
assert_eq!(
544544
format!("{err}"),
545545
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
546-
14\n\n\t[1,2,3 }\n\t........^\n"
546+
15\n\n\t[1,2,3 }\n\t........^\n"
547547
);
548548

549549
let err = from_str::<Foo>("{\"a\": [\"123\"]}").unwrap_err();
550550
assert_eq!(
551551
format!("{err}"),
552-
"invalid type: string \"123\", expected i32 at line 1 column 11\n\n\t\": \
552+
"invalid type: string \"123\", expected i32 at line 1 column 12\n\n\t\": \
553553
[\"123\"]}\n\t........^..\n"
554554
);
555555

556556
let err = from_str::<Foo>("{\"a\": [").unwrap_err();
557557
assert_eq!(
558558
format!("{err}"),
559-
"EOF while parsing at line 1 column 6\n\n\t{\"a\": [\n\t......^\n"
559+
"EOF while parsing at line 1 column 7\n\n\t{\"a\": [\n\t......^\n"
560560
);
561561

562562
let err = from_str::<Foo>("{\"a\": [000]}").unwrap_err();
563563
assert_eq!(
564564
format!("{err}"),
565565
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
566-
8\n\n\t{\"a\": [000]}\n\t........^...\n"
566+
9\n\n\t{\"a\": [000]}\n\t........^...\n"
567567
);
568568

569569
let err = from_str::<Foo>("{\"a\": [-]}").unwrap_err();
570570
assert_eq!(
571571
format!("{err}"),
572-
"Invalid number at line 1 column 7\n\n\t{\"a\": [-]}\n\t.......^..\n"
572+
"Invalid number at line 1 column 8\n\n\t{\"a\": [-]}\n\t.......^..\n"
573573
);
574574

575575
let err = from_str::<Foo>("{\"a\": [-1.23e]}").unwrap_err();
576576
assert_eq!(
577577
format!("{err}"),
578-
"Invalid number at line 1 column 12\n\n\t: [-1.23e]}\n\t........^..\n"
578+
"Invalid number at line 1 column 13\n\n\t: [-1.23e]}\n\t........^..\n"
579579
);
580580

581581
let err = from_str::<Foo>("{\"c\": \"哈哈哈哈哈哈}").unwrap_err();
582582
assert_eq!(
583583
format!("{err}"),
584-
"EOF while parsing at line 1 column 25\n\n\t哈哈哈}\n\t.........^\n"
584+
"EOF while parsing at line 1 column 26\n\n\t哈哈哈}\n\t.........^\n"
585585
);
586586

587587
let err = from_slice::<Foo>(b"{\"b\":\"\x80\"}").unwrap_err();
588588
assert_eq!(
589589
format!("{err}"),
590-
"Invalid UTF-8 characters in json at line 1 column 6\n\n\t{\"b\":\"\"}\n\t......^..\n"
590+
"Invalid UTF-8 characters in json at line 1 column 7\n\n\t{\"b\":\"\"}\n\t......^..\n"
591591
);
592592
}
593593

@@ -599,4 +599,15 @@ mod test {
599599
"NaN or Infinity is not a valid JSON value"
600600
);
601601
}
602+
603+
#[test]
604+
fn test_error_column() {
605+
let json_str = r#"
606+
{
607+
"key": [, 1, 2, 3]
608+
}
609+
"#;
610+
let err = from_str::<crate::Value>(json_str).unwrap_err();
611+
assert_eq!(err.column(), 13);
612+
}
602613
}

src/lazyvalue/iterator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ mod test {
541541
assert_eq!(
542542
item.err().unwrap().to_string(),
543543
"Invalid UTF-8 characters in json at line 1 column \
544-
5\n\n\t[\"\0\0\0��\"]\n\t.....^...\n"
544+
6\n\n\t[\"\0\0\0��\"]\n\t.....^...\n"
545545
);
546546
}
547547

@@ -553,7 +553,7 @@ mod test {
553553
assert_eq!(
554554
item.err().unwrap().to_string(),
555555
"Invalid UTF-8 characters in json at line 1 column \
556-
5\n\n\t{\"\0\0\0��\":\"\"}\n\t.....^......\n"
556+
6\n\n\t{\"\0\0\0��\":\"\"}\n\t.....^......\n"
557557
);
558558
}
559559
}

src/reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ impl Position {
1919
pub(crate) fn from_index(mut i: usize, data: &[u8]) -> Self {
2020
// i must not exceed the length of data
2121
i = i.min(data.len());
22-
let mut position = Position { line: 1, column: 0 };
22+
let mut position = Position { line: 1, column: 1 };
2323
for ch in &data[..i] {
2424
match *ch {
2525
b'\n' => {
2626
position.line += 1;
27-
position.column = 0;
27+
position.column = 1;
2828
}
2929
_ => {
3030
position.column += 1;

src/serde/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ mod test {
351351
let value: crate::Result<String> = from_slice(&data);
352352
assert_eq!(
353353
value.err().unwrap().to_string(),
354-
"Invalid UTF-8 characters in json at line 1 column 4\n\n\t\"\0\0\0��\"\n\t....^..\n"
354+
"Invalid UTF-8 characters in json at line 1 column 5\n\n\t\"\0\0\0��\"\n\t....^..\n"
355355
);
356356

357357
#[derive(Debug, Deserialize, Serialize, PartialEq)]
@@ -364,7 +364,7 @@ mod test {
364364
let value: crate::Result<char> = from_slice(&data);
365365
assert_eq!(
366366
value.err().unwrap().to_string(),
367-
"Invalid UTF-8 characters in json at line 1 column 1\n\n\t\"\"\n\t.^.\n"
367+
"Invalid UTF-8 characters in json at line 1 column 2\n\n\t\"\"\n\t.^.\n"
368368
);
369369
}
370370

@@ -609,35 +609,35 @@ mod test {
609609
{ "invalid utf8 here": "invalid utf8 here",
610610
"bytes": [1,2,3],
611611
"string": "hello",
612-
612+
613613
"true": true,
614-
614+
615615
"1": 1,
616616
"123": 123,
617617
"-123": -123,
618618
"12345": 12345,
619619
"1": 1,
620-
620+
621621
"1": 1,
622622
"123": 123,
623623
"123": -123,
624624
"12345": 12345,
625625
"1": 1,
626-
626+
627627
"12345": 12345,
628628
"1": 1,
629-
629+
630630
"1.23e+2": 1.23,
631631
"-1.23": -1.23,
632-
632+
633633
"123": "option",
634-
634+
635635
"wrapper": {},
636-
636+
637637
"Zero": "enum",
638-
638+
639639
"A": "char",
640-
640+
641641
"ignored": "ignored"
642642
}
643643
"#

src/value/node.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub struct Value {
8383

8484
#[rustfmt::skip]
8585
// A compact and mutable JSON Value.
86-
//
86+
//
8787
// Thera are three kind nodes into the Value:
8888
// - Static Node: no need drop
8989
// - Owned Node : mutable
@@ -1923,7 +1923,7 @@ mod test {
19231923
let ret: Result<Value> = from_slice(&data);
19241924
assert_eq!(
19251925
ret.err().unwrap().to_string(),
1926-
"Invalid UTF-8 characters in json at line 1 column 1\n\n\t\"��\"\n\t.^..\n"
1926+
"Invalid UTF-8 characters in json at line 1 column 2\n\n\t\"��\"\n\t.^..\n"
19271927
);
19281928

19291929
let dom: Result<Value> = unsafe { from_slice_unchecked(&data) };
@@ -1933,14 +1933,14 @@ mod test {
19331933
let dom: Result<Value> = from_slice(&data);
19341934
assert_eq!(
19351935
dom.err().unwrap().to_string(),
1936-
"Invalid UTF-8 characters in json at line 1 column 2\n\n\t\"\"\n\t..^\n"
1936+
"Invalid UTF-8 characters in json at line 1 column 3\n\n\t\"\"\n\t..^\n"
19371937
);
19381938

19391939
let data = [0x80, b'"', b'"'];
19401940
let dom: Result<Value> = unsafe { from_slice_unchecked(&data) };
19411941
assert_eq!(
19421942
dom.err().unwrap().to_string(),
1943-
"Invalid JSON value at line 1 column 0\n\n\t\"\"\n\t^..\n"
1943+
"Invalid JSON value at line 1 column 1\n\n\t\"\"\n\t^..\n"
19441944
);
19451945
}
19461946

0 commit comments

Comments
 (0)