@@ -65,7 +65,13 @@ const _parseJSON = (jsonString: string, allow: number) => {
65
65
index += 3 ;
66
66
return NaN ;
67
67
}
68
- return parseNum ( ) ;
68
+ // Check if we have a valid number character before calling parseNum
69
+ const char = jsonString [ index ] ;
70
+ if ( char === "-" || ( char >= "0" && char <= "9" ) ) {
71
+ return parseNum ( ) ;
72
+ }
73
+ // If we get here, it's an invalid token
74
+ throwMalformedError ( `Unexpected token '${ char } '` ) ;
69
75
} ;
70
76
71
77
const parseStr : ( ) => string = ( ) => {
@@ -108,13 +114,21 @@ const _parseJSON = (jsonString: string, allow: number) => {
108
114
const value = parseAny ( ) ;
109
115
obj [ key ] = value ;
110
116
} catch ( e ) {
117
+ // If it's a malformed JSON error, let it bubble up
118
+ if ( e instanceof MalformedJSON ) {
119
+ throw e ;
120
+ }
111
121
if ( Allow . OBJ & allow ) return obj ;
112
122
else throw e ;
113
123
}
114
124
skipBlank ( ) ;
115
125
if ( jsonString [ index ] === "," ) index ++ ; // skip comma
116
126
}
117
127
} catch ( e ) {
128
+ // If it's a malformed JSON error, let it bubble up
129
+ if ( e instanceof MalformedJSON ) {
130
+ throw e ;
131
+ }
118
132
if ( Allow . OBJ & allow ) return obj ;
119
133
else markPartialJSON ( "Expected '}' at end of object" ) ;
120
134
}
@@ -124,16 +138,22 @@ const _parseJSON = (jsonString: string, allow: number) => {
124
138
125
139
const parseArr = ( ) => {
126
140
index ++ ; // skip initial bracket
141
+ skipBlank ( ) ; // skip whitespace at start of array
127
142
const arr = [ ] ;
128
143
try {
129
144
while ( jsonString [ index ] !== "]" ) {
130
145
arr . push ( parseAny ( ) ) ;
131
146
skipBlank ( ) ;
132
147
if ( jsonString [ index ] === "," ) {
133
148
index ++ ; // skip comma
149
+ skipBlank ( ) ; // skip whitespace after comma
134
150
}
135
151
}
136
152
} catch ( e ) {
153
+ // If it's a malformed JSON error, let it bubble up
154
+ if ( e instanceof MalformedJSON ) {
155
+ throw e ;
156
+ }
137
157
if ( Allow . ARR & allow ) {
138
158
return arr ;
139
159
}
@@ -168,11 +188,19 @@ const _parseJSON = (jsonString: string, allow: number) => {
168
188
return JSON . parse ( jsonString . substring ( start , index ) ) ;
169
189
} catch ( e ) {
170
190
if ( jsonString . substring ( start , index ) === "-" ) markPartialJSON ( "Not sure what '-' is" ) ;
171
- try {
172
- return JSON . parse ( jsonString . substring ( start , jsonString . lastIndexOf ( "e" ) ) ) ;
173
- } catch ( e ) {
174
- throwMalformedError ( String ( e ) ) ;
191
+ // If the number is partial and we allow partial numbers, try to parse up to last 'e'
192
+ if ( Allow . NUM & allow ) {
193
+ const numberStr = jsonString . substring ( start , index ) ;
194
+ const lastE = numberStr . lastIndexOf ( "e" ) ;
195
+ if ( lastE > 0 ) {
196
+ try {
197
+ return JSON . parse ( numberStr . substring ( 0 , lastE ) ) ;
198
+ } catch ( e2 ) {
199
+ // Still invalid, fall through to error
200
+ }
201
+ }
175
202
}
203
+ throwMalformedError ( String ( e ) ) ;
176
204
}
177
205
} ;
178
206
0 commit comments