@@ -65,7 +65,13 @@ const _parseJSON = (jsonString: string, allow: number) => {
6565 index += 3 ;
6666 return NaN ;
6767 }
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 } '` ) ;
6975 } ;
7076
7177 const parseStr : ( ) => string = ( ) => {
@@ -108,13 +114,21 @@ const _parseJSON = (jsonString: string, allow: number) => {
108114 const value = parseAny ( ) ;
109115 obj [ key ] = value ;
110116 } catch ( e ) {
117+ // If it's a malformed JSON error, let it bubble up
118+ if ( e instanceof MalformedJSON ) {
119+ throw e ;
120+ }
111121 if ( Allow . OBJ & allow ) return obj ;
112122 else throw e ;
113123 }
114124 skipBlank ( ) ;
115125 if ( jsonString [ index ] === "," ) index ++ ; // skip comma
116126 }
117127 } catch ( e ) {
128+ // If it's a malformed JSON error, let it bubble up
129+ if ( e instanceof MalformedJSON ) {
130+ throw e ;
131+ }
118132 if ( Allow . OBJ & allow ) return obj ;
119133 else markPartialJSON ( "Expected '}' at end of object" ) ;
120134 }
@@ -124,16 +138,22 @@ const _parseJSON = (jsonString: string, allow: number) => {
124138
125139 const parseArr = ( ) => {
126140 index ++ ; // skip initial bracket
141+ skipBlank ( ) ; // skip whitespace at start of array
127142 const arr = [ ] ;
128143 try {
129144 while ( jsonString [ index ] !== "]" ) {
130145 arr . push ( parseAny ( ) ) ;
131146 skipBlank ( ) ;
132147 if ( jsonString [ index ] === "," ) {
133148 index ++ ; // skip comma
149+ skipBlank ( ) ; // skip whitespace after comma
134150 }
135151 }
136152 } catch ( e ) {
153+ // If it's a malformed JSON error, let it bubble up
154+ if ( e instanceof MalformedJSON ) {
155+ throw e ;
156+ }
137157 if ( Allow . ARR & allow ) {
138158 return arr ;
139159 }
@@ -168,11 +188,19 @@ const _parseJSON = (jsonString: string, allow: number) => {
168188 return JSON . parse ( jsonString . substring ( start , index ) ) ;
169189 } catch ( e ) {
170190 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+ }
175202 }
203+ throwMalformedError ( String ( e ) ) ;
176204 }
177205 } ;
178206
0 commit comments