@@ -394,10 +394,7 @@ func GetString(data []byte, keys ...string) (val string, err error) {
394
394
return string (v ), nil
395
395
}
396
396
397
- var stackbuf [unescapeStackBufSize ]byte // stack-allocated array for allocation-free unescaping of small strings
398
- out , err := Unescape (v , stackbuf [:])
399
-
400
- return string (out ), err
397
+ return ParseString (v )
401
398
}
402
399
403
400
// GetFloat returns the value retrieved by `Get`, cast to a float64 if possible.
@@ -414,8 +411,7 @@ func GetFloat(data []byte, keys ...string) (val float64, err error) {
414
411
return 0 , fmt .Errorf ("Value is not a number: %s" , string (v ))
415
412
}
416
413
417
- val , err = parseFloat (& v )
418
- return
414
+ return ParseFloat (v )
419
415
}
420
416
421
417
// GetInt returns the value retrieved by `Get`, cast to a float64 if possible.
@@ -431,11 +427,7 @@ func GetInt(data []byte, keys ...string) (val int64, err error) {
431
427
return 0 , fmt .Errorf ("Value is not a number: %s" , string (v ))
432
428
}
433
429
434
- if val , ok := parseInt (v ); ! ok {
435
- return 0 , MalformedValueError
436
- } else {
437
- return val , nil
438
- }
430
+ return ParseInt (v )
439
431
}
440
432
441
433
// GetBoolean returns the value retrieved by `Get`, cast to a bool if possible.
@@ -452,40 +444,50 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
452
444
return false , fmt .Errorf ("Value is not a boolean: %s" , string (v ))
453
445
}
454
446
455
- if v [0 ] == 't' {
456
- val = true
457
- } else {
458
- val = false
459
- }
460
-
461
- return
447
+ return ParseBoolean (v )
462
448
}
463
449
464
450
// ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
465
- func ParseBoolean (vbytes []byte ) bool {
466
- return (vbytes [0 ] == 't' ) // assumes value is already validated by Get(), etc. as signaled by jtype == Boolean
451
+ func ParseBoolean (b []byte ) (bool , error ) {
452
+ switch b [0 ] {
453
+ case 't' :
454
+ return true , nil
455
+ case 'f' :
456
+ return false , nil
457
+ default :
458
+ return false , MalformedValueError
459
+ }
467
460
}
468
461
469
462
// ParseString parses a String ValueType into a Go []byte (the main parsing work is unescaping the JSON string)
470
- func ParseStringAsBytes ( vbytes []byte ) ([]byte , error ) {
463
+ func parseStringAsBytes ( b []byte ) ([]byte , error ) {
471
464
var stackbuf [unescapeStackBufSize ]byte // stack-allocated array for allocation-free unescaping of small strings (hopefully; the Go compiler might just always kick stackbuf[:] into the heap)
472
- return Unescape (vbytes , stackbuf [:])
465
+ return Unescape (b , stackbuf [:])
473
466
}
474
467
475
468
// ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
476
- func ParseString (vbytes []byte ) (string , error ) {
477
- if vbytesUnesc , err := ParseStringAsBytes ( vbytes ); err != nil {
469
+ func ParseString (b []byte ) (string , error ) {
470
+ if bU , err := parseStringAsBytes ( b ); err != nil {
478
471
return "" , nil
479
472
} else {
480
- return string (vbytesUnesc ), nil
473
+ return string (bU ), nil
481
474
}
482
475
}
483
476
484
477
// ParseNumber parses a Number ValueType into a Go float64
485
- func ParseNumber ( vbytes []byte ) (float64 , error ) {
486
- if v , err := parseFloat (& vbytes ); err != nil {
478
+ func ParseFloat ( b []byte ) (float64 , error ) {
479
+ if v , err := parseFloat (& b ); err != nil {
487
480
return 0 , MalformedValueError
488
481
} else {
489
482
return v , nil
490
483
}
491
484
}
485
+
486
+ // ParseNumber parses a Number ValueType into a Go float64
487
+ func ParseInt (b []byte ) (int64 , error ) {
488
+ if v , err := parseInt (b ); ! err {
489
+ return 0 , MalformedValueError
490
+ } else {
491
+ return v , nil
492
+ }
493
+ }
0 commit comments