@@ -107,7 +107,7 @@ const isUndefined = (u8: Uint8Array, x: number) =>
107107
108108const fromCharCode = String . fromCharCode ;
109109
110- const readShortUtf8StrAndUnescape = ( reader : Reader ) : string => {
110+ export const readKey = ( reader : Reader ) : string => {
111111 const buf = reader . uint8 ;
112112 const len = buf . length ;
113113 const points : number [ ] = [ ] ;
@@ -202,10 +202,8 @@ export class JsonDecoder implements BinaryJsonDecoder {
202202 const uint8 = reader . uint8 ;
203203 const char = uint8 [ x ] ;
204204 switch ( char ) {
205- case 34 : {
206- // "
207- if ( uint8 [ x + 1 ] === 0x64 ) {
208- // d
205+ case 34 /* " */ : {
206+ if ( uint8 [ x + 1 ] === 0x64 /* d */ ) {
209207 const bin = this . tryReadBin ( ) ;
210208 if ( bin ) return bin ;
211209 if ( isUndefined ( uint8 , x + 2 ) ) {
@@ -215,18 +213,18 @@ export class JsonDecoder implements BinaryJsonDecoder {
215213 }
216214 return this . readStr ( ) ;
217215 }
218- case 91 : // [
216+ case 91 /* [ */ :
219217 return this . readArr ( ) ;
220- case 102 : // f
218+ case 102 /* f */ :
221219 return this . readFalse ( ) ;
222- case 110 : // n
220+ case 110 /* n */ :
223221 return this . readNull ( ) ;
224- case 116 : // t
222+ case 116 /* t */ :
225223 return this . readTrue ( ) ;
226- case 123 : // {
224+ case 123 /* { */ :
227225 return this . readObj ( ) ;
228226 default :
229- if ( ( char >= 48 && char <= 57 ) || char === 45 ) return this . readNum ( ) ;
227+ if ( ( char >= 48 /* 0 */ && char <= 57 ) /* 9 */ || char === 45 /* - */ ) return this . readNum ( ) ;
230228 throw new Error ( 'Invalid JSON' ) ;
231229 }
232230 }
@@ -239,10 +237,10 @@ export class JsonDecoder implements BinaryJsonDecoder {
239237 while ( true ) {
240238 char = uint8 [ x ] ;
241239 switch ( char ) {
242- case 32 : // space
243- case 9 : // tab
244- case 10 : // line feed
245- case 13 : // carriage return
240+ case 32 /* < space> */ :
241+ case 9 /* < tab> */ :
242+ case 10 /* < line feed> */ :
243+ case 13 /* < carriage return> */ :
246244 x ++ ;
247245 continue ;
248246 default :
@@ -253,27 +251,27 @@ export class JsonDecoder implements BinaryJsonDecoder {
253251 }
254252
255253 public readNull ( ) : null {
256- if ( this . reader . u32 ( ) !== 0x6e756c6c ) throw new Error ( 'Invalid JSON' ) ;
254+ if ( this . reader . u32 ( ) !== 0x6e756c6c /* null */ ) throw new Error ( 'Invalid JSON' ) ;
257255 return null ;
258256 }
259257
260258 public readTrue ( ) : true {
261- if ( this . reader . u32 ( ) !== 0x74727565 ) throw new Error ( 'Invalid JSON' ) ;
259+ if ( this . reader . u32 ( ) !== 0x74727565 /* true */ ) throw new Error ( 'Invalid JSON' ) ;
262260 return true ;
263261 }
264262
265263 public readFalse ( ) : false {
266264 const reader = this . reader ;
267- if ( reader . u8 ( ) !== 0x66 || reader . u32 ( ) !== 0x616c7365 ) throw new Error ( 'Invalid JSON' ) ;
265+ if ( reader . u8 ( ) !== 0x66 /* f */ || reader . u32 ( ) !== 0x616c7365 /* alse */ ) throw new Error ( 'Invalid JSON' ) ;
268266 return false ;
269267 }
270268
271269 public readBool ( ) : unknown {
272270 const reader = this . reader ;
273271 switch ( reader . uint8 [ reader . x ] ) {
274- case 102 : // f
272+ case 102 /* f */ :
275273 return this . readFalse ( ) ;
276- case 116 : // t
274+ case 116 /* t */ :
277275 return this . readTrue ( ) ;
278276 default :
279277 throw new Error ( 'Invalid JSON' ) ;
@@ -642,42 +640,44 @@ export class JsonDecoder implements BinaryJsonDecoder {
642640
643641 public readArr ( ) : unknown [ ] {
644642 const reader = this . reader ;
645- if ( reader . u8 ( ) !== 0x5b ) throw new Error ( 'Invalid JSON' ) ;
643+ if ( reader . u8 ( ) !== 0x5b /* [ */ ) throw new Error ( 'Invalid JSON' ) ;
646644 const arr : unknown [ ] = [ ] ;
647645 const uint8 = reader . uint8 ;
646+ let first = true ;
648647 while ( true ) {
649648 this . skipWhitespace ( ) ;
650649 const char = uint8 [ reader . x ] ;
651- if ( char === 0x5d ) return reader . x ++ , arr ; // ]
652- if ( char === 0x2c ) {
653- reader . x ++ ;
654- continue ;
655- } // ,
650+ if ( char === 0x5d /* ] */ ) return reader . x ++ , arr ;
651+ if ( char === 0x2c /* , */ ) reader . x ++ ;
652+ else if ( ! first ) throw new Error ( 'Invalid JSON' ) ;
653+ this . skipWhitespace ( ) ;
656654 arr . push ( this . readAny ( ) ) ;
655+ first = false ;
657656 }
658657 }
659658
660659 public readObj ( ) : PackValue | Record < string , unknown > | unknown {
661660 const reader = this . reader ;
662- if ( reader . u8 ( ) !== 0x7b ) throw new Error ( 'Invalid JSON' ) ;
661+ if ( reader . u8 ( ) !== 0x7b /* { */ ) throw new Error ( 'Invalid JSON' ) ;
663662 const obj : Record < string , unknown > = { } ;
664663 const uint8 = reader . uint8 ;
664+ let first = true ;
665665 while ( true ) {
666666 this . skipWhitespace ( ) ;
667667 let char = uint8 [ reader . x ] ;
668- if ( char === 0x7d ) return reader . x ++ , obj ; // }
669- if ( char === 0x2c ) {
670- reader . x ++ ;
671- continue ;
672- } // ,
668+ if ( char === 0x7d /* } */ ) return reader . x ++ , obj ;
669+ if ( char === 0x2c /* , */ ) reader . x ++ ;
670+ else if ( ! first ) throw new Error ( 'Invalid JSON' ) ;
671+ this . skipWhitespace ( ) ;
673672 char = uint8 [ reader . x ++ ] ;
674- if ( char !== 0x22 ) throw new Error ( 'Invalid JSON' ) ;
675- const key = readShortUtf8StrAndUnescape ( reader ) ;
673+ if ( char !== 0x22 /* " */ ) throw new Error ( 'Invalid JSON' ) ;
674+ const key = readKey ( reader ) ;
676675 if ( key === '__proto__' ) throw new Error ( 'Invalid JSON' ) ;
677676 this . skipWhitespace ( ) ;
678- if ( reader . u8 ( ) !== 0x3a ) throw new Error ( 'Invalid JSON' ) ;
677+ if ( reader . u8 ( ) !== 0x3a /* : */ ) throw new Error ( 'Invalid JSON' ) ;
679678 this . skipWhitespace ( ) ;
680679 obj [ key ] = this . readAny ( ) ;
680+ first = false ;
681681 }
682682 }
683683}
0 commit comments