Skip to content

Commit eff9638

Browse files
committed
feat: implement JSON Pointer format for JsonPath Show trait
- Rewrite Show implementation to follow RFC 6901 JSON Pointer specification - Replace JSONPath syntax ($, ., []) with JSON Pointer syntax (/, escaping) - Add proper escaping for special characters: - ~ becomes ~0 - / becomes ~1 - Root path is now empty string "" instead of "$" - Add comprehensive test suites: - Basic JSON Pointer format tests - Special character escaping tests - RFC 6901 compliance examples - Update all affected tests to match new format - Maintain backward compatibility of public API
1 parent a2c1fcc commit eff9638

File tree

4 files changed

+181
-51
lines changed

4 files changed

+181
-51
lines changed

json/README.mbt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ test "json path" {
130130
panic()
131131
} catch {
132132
@json.JsonDecodeError((path, msg)) => {
133-
inspect(path, content="$[1]")
133+
inspect(path, content="/1")
134134
inspect(msg, content="Int::from_json: expected number")
135135
}
136136
}

json/from_json_test.mbt

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test {
3535
inspect(
3636
v,
3737
content=(
38-
#|Err(JsonDecodeError(($[1], "Int::from_json: expected number")))
38+
#|Err(JsonDecodeError((/1, "Int::from_json: expected number")))
3939
),
4040
)
4141
}
@@ -50,7 +50,7 @@ test {
5050
inspect(
5151
v,
5252
content=(
53-
#|Err(JsonDecodeError(($.y.yy, "Double::from_json: expected number")))
53+
#|Err(JsonDecodeError((/y/yy, "Double::from_json: expected number")))
5454
),
5555
)
5656
}
@@ -276,7 +276,7 @@ test "tuple failure" {
276276
inspect(
277277
v.unwrap_err(),
278278
content=(
279-
#|JsonDecodeError(($, "expected tuple of size 3"))
279+
#|JsonDecodeError((, "expected tuple of size 3"))
280280
),
281281
)
282282
let u = ((1, 2), (3, 4))
@@ -286,7 +286,7 @@ test "tuple failure" {
286286
inspect(
287287
v.unwrap_err(),
288288
content=(
289-
#|JsonDecodeError(($[1], "Int::from_json: expected number"))
289+
#|JsonDecodeError((/1, "Int::from_json: expected number"))
290290
),
291291
)
292292
}
@@ -426,13 +426,17 @@ test "uint" {
426426
let neg_res : Result[UInt, _] = try? @json.from_json(Json::number(-1))
427427
inspect(
428428
neg_res,
429-
content="Err(JsonDecodeError(($, \"UInt::from_json: overflow\")))",
429+
content=(
430+
#|Err(JsonDecodeError((, "UInt::from_json: overflow")))
431+
),
430432
)
431433
// overflow should error
432434
let ovf_res : Result[UInt, _] = try? @json.from_json(Json::number(4294967296))
433435
inspect(
434436
ovf_res,
435-
content="Err(JsonDecodeError(($, \"UInt::from_json: overflow\")))",
437+
content=(
438+
#|Err(JsonDecodeError((, "UInt::from_json: overflow")))
439+
),
436440
)
437441
}
438442

@@ -510,7 +514,7 @@ test "unicode" {
510514
inspect(
511515
v,
512516
content=(
513-
#|Err(JsonDecodeError(($, "Char::from_json: invalid surrogate pair")))
517+
#|Err(JsonDecodeError((, "Char::from_json: invalid surrogate pair")))
514518
),
515519
)
516520
}
@@ -563,19 +567,25 @@ test "Bool from_json error handling" {
563567
let result : Result[Bool, _] = try? @json.from_json(json_null)
564568
inspect(
565569
result,
566-
content="Err(JsonDecodeError(($, \"Bool::from_json: expected boolean\")))",
570+
content=(
571+
#|Err(JsonDecodeError((, "Bool::from_json: expected boolean")))
572+
),
567573
)
568574
let json_number = Json::number(42)
569575
let result : Result[Bool, _] = try? @json.from_json(json_number)
570576
inspect(
571577
result,
572-
content="Err(JsonDecodeError(($, \"Bool::from_json: expected boolean\")))",
578+
content=(
579+
#|Err(JsonDecodeError((, "Bool::from_json: expected boolean")))
580+
),
573581
)
574582
let json_string = Json::string("true")
575583
let result : Result[Bool, _] = try? @json.from_json(json_string)
576584
inspect(
577585
result,
578-
content="Err(JsonDecodeError(($, \"Bool::from_json: expected boolean\")))",
586+
content=(
587+
#|Err(JsonDecodeError((, "Bool::from_json: expected boolean")))
588+
),
579589
)
580590
}
581591

@@ -585,13 +595,17 @@ test "Int64 from_json error handling" {
585595
let result : Result[Int64, _] = try? @json.from_json(json_number)
586596
inspect(
587597
result,
588-
content="Err(JsonDecodeError(($, \"Int64::from_json: expected number in string representation\")))",
598+
content=(
599+
#|Err(JsonDecodeError((, "Int64::from_json: expected number in string representation")))
600+
),
589601
)
590602
let json_invalid_string = Json::string("not a number")
591603
let result : Result[Int64, _] = try? @json.from_json(json_invalid_string)
592604
inspect(
593605
result,
594-
content="Err(JsonDecodeError(($, \"Int64::from_json: parsing failure invalid syntax\")))",
606+
content=(
607+
#|Err(JsonDecodeError((, "Int64::from_json: parsing failure invalid syntax")))
608+
),
595609
)
596610
}
597611

@@ -601,7 +615,9 @@ test "UInt from_json error handling" {
601615
let result : Result[UInt, _] = try? @json.from_json(json_string)
602616
inspect(
603617
result,
604-
content="Err(JsonDecodeError(($, \"UInt::from_json: expected number\")))",
618+
content=(
619+
#|Err(JsonDecodeError((, "UInt::from_json: expected number")))
620+
),
605621
)
606622
}
607623

@@ -611,13 +627,17 @@ test "UInt64 from_json error handling" {
611627
let result : Result[UInt64, _] = try? @json.from_json(json_number)
612628
inspect(
613629
result,
614-
content="Err(JsonDecodeError(($, \"UInt64::from_json: expected number in string representation\")))",
630+
content=(
631+
#|Err(JsonDecodeError((, "UInt64::from_json: expected number in string representation")))
632+
),
615633
)
616634
let json_invalid_string = Json::string("not a number")
617635
let result : Result[UInt64, _] = try? @json.from_json(json_invalid_string)
618636
inspect(
619637
result,
620-
content="Err(JsonDecodeError(($, \"UInt64::from_json: parsing failure invalid syntax\")))",
638+
content=(
639+
#|Err(JsonDecodeError((, "UInt64::from_json: parsing failure invalid syntax")))
640+
),
621641
)
622642
}
623643

@@ -627,7 +647,9 @@ test "String from_json error handling" {
627647
let result : Result[String, _] = try? @json.from_json(json_number)
628648
inspect(
629649
result,
630-
content="Err(JsonDecodeError(($, \"String::from_json: expected string\")))",
650+
content=(
651+
#|Err(JsonDecodeError((, "String::from_json: expected string")))
652+
),
631653
)
632654
}
633655

@@ -637,7 +659,9 @@ test "String View from_json error handling" {
637659
let result : Result[StringView, _] = try? @json.from_json(json_number)
638660
inspect(
639661
result,
640-
content="Err(JsonDecodeError(($, \"View::from_json: expected string\")))",
662+
content=(
663+
#|Err(JsonDecodeError((, "View::from_json: expected string")))
664+
),
641665
)
642666
}
643667

@@ -647,19 +671,25 @@ test "Char from_json error handling" {
647671
let result : Result[Char, _] = try? @json.from_json(json_number)
648672
inspect(
649673
result,
650-
content="Err(JsonDecodeError(($, \"Char::from_json: expected string\")))",
674+
content=(
675+
#|Err(JsonDecodeError((, "Char::from_json: expected string")))
676+
),
651677
)
652678
let json_empty_string = Json::string("")
653679
let result : Result[Char, _] = try? @json.from_json(json_empty_string)
654680
inspect(
655681
result,
656-
content="Err(JsonDecodeError(($, \"Char::from_json: expected single character\")))",
682+
content=(
683+
#|Err(JsonDecodeError((, "Char::from_json: expected single character")))
684+
),
657685
)
658686
let json_long_string = Json::string("abc")
659687
let result : Result[Char, _] = try? @json.from_json(json_long_string)
660688
inspect(
661689
result,
662-
content="Err(JsonDecodeError(($, \"Char::from_json: expected single character\")))",
690+
content=(
691+
#|Err(JsonDecodeError((, "Char::from_json: expected single character")))
692+
),
663693
)
664694
}
665695

@@ -670,7 +700,7 @@ test "BigInt from_json error handling" {
670700
inspect(
671701
result,
672702
content=(
673-
#|Err(JsonDecodeError(($, "BigInt::from_json: expected number in string representation")))
703+
#|Err(JsonDecodeError((, "BigInt::from_json: expected number in string representation")))
674704
),
675705
)
676706
}
@@ -681,7 +711,9 @@ test "Array from_json error handling" {
681711
let result : Result[Array[Int], _] = try? @json.from_json(json_string)
682712
inspect(
683713
result,
684-
content="Err(JsonDecodeError(($, \"Array::from_json: expected array\")))",
714+
content=(
715+
#|Err(JsonDecodeError((, "Array::from_json: expected array")))
716+
),
685717
)
686718
}
687719

@@ -691,7 +723,9 @@ test "FixedArray from_json error handling" {
691723
let result : Result[FixedArray[Int], _] = try? @json.from_json(json_string)
692724
inspect(
693725
result,
694-
content="Err(JsonDecodeError(($, \"FixedArray::from_json: expected array\")))",
726+
content=(
727+
#|Err(JsonDecodeError((, "FixedArray::from_json: expected array")))
728+
),
695729
)
696730
}
697731

@@ -701,7 +735,9 @@ test "Map from_json error handling" {
701735
let result : Result[Map[String, Int], _] = try? @json.from_json(json_array)
702736
inspect(
703737
result,
704-
content="Err(JsonDecodeError(($, \"Map::from_json: expected object\")))",
738+
content=(
739+
#|Err(JsonDecodeError((, "Map::from_json: expected object")))
740+
),
705741
)
706742
}
707743

@@ -711,7 +747,9 @@ test "Option from_json error handling" {
711747
let result : Result[Int?, _] = try? @json.from_json(json_string)
712748
inspect(
713749
result,
714-
content="Err(JsonDecodeError(($, \"Option::from_json: expected array or null\")))",
750+
content=(
751+
#|Err(JsonDecodeError((, "Option::from_json: expected array or null")))
752+
),
715753
)
716754
}
717755

@@ -723,23 +761,29 @@ test "Result from_json error handling" {
723761
)
724762
inspect(
725763
result,
726-
content="Err(JsonDecodeError(($, \"Result::from_json: expected object\")))",
764+
content=(
765+
#|Err(JsonDecodeError((, "Result::from_json: expected object")))
766+
),
727767
)
728768
let json_empty_obj = Json::object({})
729769
let result : Result[Result[Int, String], _] = try? @json.from_json(
730770
json_empty_obj,
731771
)
732772
inspect(
733773
result,
734-
content="Err(JsonDecodeError(($, \"Result::from_json: expected object with one field\")))",
774+
content=(
775+
#|Err(JsonDecodeError((, "Result::from_json: expected object with one field")))
776+
),
735777
)
736778
let json_invalid_obj = Json::object({ "Invalid": Json::number(1) })
737779
let result : Result[Result[Int, String], _] = try? @json.from_json(
738780
json_invalid_obj,
739781
)
740782
inspect(
741783
result,
742-
content="Err(JsonDecodeError(($, \"Result::from_json: expected object with Ok or Err field\")))",
784+
content=(
785+
#|Err(JsonDecodeError((, "Result::from_json: expected object with Ok or Err field")))
786+
),
743787
)
744788
}
745789

@@ -749,7 +793,9 @@ test "Unit from_json error handling" {
749793
let result : Result[Unit, _] = try? @json.from_json(json_string)
750794
inspect(
751795
result,
752-
content="Err(JsonDecodeError(($, \"Unit::from_json: expected null\")))",
796+
content=(
797+
#|Err(JsonDecodeError((, "Unit::from_json: expected null")))
798+
),
753799
)
754800
}
755801

0 commit comments

Comments
 (0)