Skip to content

Commit c6f8080

Browse files
Add Value.GetNumberAsStringBytes
Useful when needing to get the original representation of the number. This is needed when wanting to make an exact copy of a Value.
1 parent c284b91 commit c6f8080

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

parser.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package fastjson
22

33
import (
44
"fmt"
5-
"github.com/aperturerobotics/fastjson/fastfloat"
65
"strconv"
76
"strings"
87
"unicode/utf16"
8+
9+
"github.com/aperturerobotics/fastjson/fastfloat"
910
)
1011

1112
// Parser parses JSON.
@@ -899,6 +900,19 @@ func (v *Value) GetUint64(keys ...string) uint64 {
899900
return fastfloat.ParseUint64BestEffort(v.s)
900901
}
901902

903+
// GetNumberAsStringBytes returns string representation of the numeric value by the given keys path.
904+
//
905+
// Array indexes may be represented as decimal numbers in keys.
906+
//
907+
// nil is returned for non-existing keys path or for invalid value type.
908+
func (v *Value) GetNumberAsStringBytes(keys ...string) []byte {
909+
v = v.Get(keys...)
910+
if v == nil || v.Type() != TypeNumber {
911+
return nil
912+
}
913+
return s2b(v.s)
914+
}
915+
902916
// GetStringBytes returns string value by the given keys path.
903917
//
904918
// Array indexes may be represented as decimal numbers in keys.

parser_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ func TestValueGetTyped(t *testing.T) {
314314
"zero_float2": -0e123,
315315
"inf_float": Inf,
316316
"minus_inf_float": -Inf,
317-
"nan": nan
317+
"nan": nan,
318+
"tiny_number": 0.0000000005
318319
}`)
319320
if err != nil {
320321
t.Fatalf("unexpected error: %s", err)
@@ -360,7 +361,7 @@ func TestValueGetTyped(t *testing.T) {
360361
t.Fatalf("unexpected value; got %d; want %d", n, 123)
361362
}
362363
n64 := v.GetInt64("foo")
363-
if n != 123 {
364+
if n64 != 123 {
364365
t.Fatalf("unexpected value; got %d; want %d", n64, 123)
365366
}
366367
un := v.GetUint("foo")
@@ -371,22 +372,30 @@ func TestValueGetTyped(t *testing.T) {
371372
if un != 123 {
372373
t.Fatalf("unexpected value; got %d; want %d", un64, 123)
373374
}
375+
nstr := v.GetNumberAsStringBytes("foo")
376+
if b2s(nstr) != "123" {
377+
t.Fatalf("unexpected value; got %s; want %s", nstr, "123")
378+
}
374379
n = v.GetInt("bar")
375380
if n != 0 {
376381
t.Fatalf("unexpected non-zero value; got %d", n)
377382
}
378383
n64 = v.GetInt64("bar")
379-
if n != 0 {
384+
if n64 != 0 {
380385
t.Fatalf("unexpected non-zero value; got %d", n64)
381386
}
382387
un = v.GetUint("bar")
383-
if n != 0 {
388+
if un != 0 {
384389
t.Fatalf("unexpected non-zero value; got %d", un)
385390
}
386391
un64 = v.GetUint64("bar")
387-
if n != 0 {
392+
if un64 != 0 {
388393
t.Fatalf("unexpected non-zero value; got %d", un64)
389394
}
395+
nstr = v.GetNumberAsStringBytes("bar")
396+
if nstr != nil {
397+
t.Fatalf("unexpected non-nil value; got %d", nstr)
398+
}
390399
f := v.GetFloat64("foo")
391400
if f != 123.0 {
392401
t.Fatalf("unexpected value; got %f; want %f", f, 123.0)
@@ -464,6 +473,11 @@ func TestValueGetTyped(t *testing.T) {
464473
if !math.IsNaN(nanf) {
465474
t.Fatalf("unexpected nan value: %f. Expecting %f", nanf, math.NaN())
466475
}
476+
477+
tiny_number := v.GetNumberAsStringBytes("tiny_number")
478+
if b2s(tiny_number) != "0.0000000005" {
479+
t.Fatalf("unexpected tiny_number value; got %s; want %s", tiny_number, "0.0000000005")
480+
}
467481
}
468482

469483
func TestVisitNil(t *testing.T) {

0 commit comments

Comments
 (0)