@@ -23,6 +23,7 @@ pub fn valid(input : String) -> Bool {
23
23
}
24
24
25
25
///|
26
+ /// Parse a JSON input string into a Json value, with an optional maximum nesting depth (default is 1024)
26
27
pub fn parse (
27
28
input : String ,
28
29
max_nesting_depth ? : Int = 1024 ,
@@ -62,14 +63,14 @@ fn ParseContext::parse_value2(
62
63
63
64
///|
64
65
fn ParseContext ::parse_object (ctx : ParseContext ) -> Json raise ParseError {
65
- if ctx .depth >= ctx .max_nesting_depth {
66
+ if ctx .remaining_available_depth <= 0 {
66
67
raise DepthLimitExceeded
67
68
}
68
- ctx .depth + = 1
69
+ ctx .remaining_available_depth - = 1
69
70
let map = Map ::new ()
70
71
loop ctx .lex_property_name () {
71
72
RBrace => {
72
- ctx .depth - = 1
73
+ ctx .remaining_available_depth + = 1
73
74
Json ::object (map )
74
75
}
75
76
String (name ) => {
@@ -78,7 +79,7 @@ fn ParseContext::parse_object(ctx : ParseContext) -> Json raise ParseError {
78
79
match ctx .lex_after_object_value () {
79
80
Comma => continue ctx .lex_property_name2 ()
80
81
RBrace => {
81
- ctx .depth - = 1
82
+ ctx .remaining_available_depth + = 1
82
83
Json ::object (map )
83
84
}
84
85
_ => abort ("unreachable" )
@@ -90,14 +91,14 @@ fn ParseContext::parse_object(ctx : ParseContext) -> Json raise ParseError {
90
91
91
92
///|
92
93
fn ParseContext ::parse_array (ctx : ParseContext ) -> Json raise ParseError {
93
- if ctx .depth >= ctx .max_nesting_depth {
94
+ if ctx .remaining_available_depth <= 0 {
94
95
raise DepthLimitExceeded
95
96
}
96
- ctx .depth + = 1
97
+ ctx .remaining_available_depth - = 1
97
98
let vec = []
98
99
loop ctx .lex_value (allow_rbracket = true ) {
99
100
RBracket => {
100
- ctx .depth - = 1
101
+ ctx .remaining_available_depth + = 1
101
102
Json ::array (vec )
102
103
}
103
104
tok => {
@@ -106,7 +107,7 @@ fn ParseContext::parse_array(ctx : ParseContext) -> Json raise ParseError {
106
107
match tok2 {
107
108
Comma => continue ctx .lex_value (allow_rbracket = false )
108
109
RBracket => {
109
- ctx .depth - = 1
110
+ ctx .remaining_available_depth + = 1
110
111
Json ::array (vec )
111
112
}
112
113
_ => abort ("unreachable" )
0 commit comments