Skip to content

Add GetString function #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,19 @@ func (v *Value) GetUint64(keys ...string) uint64 {
return fastfloat.ParseUint64BestEffort(v.s)
}

// GetString returns the string value by the given keys path.
//
// Array indexes may be represented as decimal numbers in keys.
//
// "" is returned for non-existing keys path or for invalid value type.
func (v *Value) GetString(keys ...string) string {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add an equivalent String function because that would collide with the existing one that returns a string representation of the value.

v = v.Get(keys...)
if v == nil || v.Type() != TypeString {
return ""
}
return v.s
}

// GetStringBytes returns string value by the given keys path.
//
// Array indexes may be represented as decimal numbers in keys.
Expand Down
27 changes: 27 additions & 0 deletions parser_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func ExampleValue_Get() {
vv := v.Get("foo", "0", "bar", "x")
fmt.Printf("foo[0].bar.x=%s\n", vv.GetStringBytes())

vv = v.Get("foo", "0", "bar", "x")
fmt.Printf("foo[0].bar.x=%s\n", vv.GetString())

vv = v.Get("qwe")
fmt.Printf("qwe=%v\n", vv.GetBool())

Expand All @@ -106,6 +109,7 @@ func ExampleValue_Get() {

// Output:
// foo[0].bar.x=434
// foo[0].bar.x=434
// qwe=true
// foo[1]=[null,false]
// foo[1][1]=false
Expand Down Expand Up @@ -181,6 +185,29 @@ func ExampleObject_Visit() {
// string "foobar"
}

func ExampleValue_GetString() {
s := `[
{"foo": "bar"},
[123, "baz"]
]`

var p fastjson.Parser
v, err := p.Parse(s)
if err != nil {
log.Fatalf("cannot parse json: %s", err)
}
fmt.Printf("v[0].foo = %q\n", v.GetString("0", "foo"))
fmt.Printf("v[1][1] = %q\n", v.GetString("1", "1"))
fmt.Printf("v[1][0] = %q\n", v.GetString("1", "0"))
fmt.Printf("v.foo.bar.baz = %q\n", v.GetString("foo", "bar", "baz"))

// Output:
// v[0].foo = "bar"
// v[1][1] = "baz"
// v[1][0] = ""
// v.foo.bar.baz = ""
}

func ExampleValue_GetStringBytes() {
s := `[
{"foo": "bar"},
Expand Down
24 changes: 24 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ func TestValueGetTyped(t *testing.T) {
if sb != nil {
t.Fatalf("unexpected value; got %q; want %q", sb, []byte(nil))
}
s := v.GetString("bar")
if s != "433" {
t.Fatalf("unexpected value; got %q; want %q", s, "443")
}
s = v.GetString("foo")
if s != "" {
t.Fatalf("unexpected value; got %q; want \"\"", s)
}
bv := v.GetBool("baz")
if !bv {
t.Fatalf("unexpected value; got %v; want %v", bv, true)
Expand Down Expand Up @@ -460,6 +468,14 @@ func TestValueGet(t *testing.T) {
if string(sb) != "" {
t.Fatalf("unexpected non-empty value: %q", sb)
}
s := v.GetString("")
if s != "empty-key" {
t.Fatalf("unexpected value for empty key; got %q; want %q", s, "empty-key")
}
s = v.GetString("empty-value")
if s != "" {
t.Fatalf("unexpected non-empty value: %q", s)
}

vv := v.Get("foo", "1")
if vv == nil {
Expand Down Expand Up @@ -545,6 +561,10 @@ func TestParserParse(t *testing.T) {
if string(sb) != `\fЗУ\\` {
t.Fatalf("unexpected string; got %q; want %q", sb, `\fЗУ\\`)
}
s := v.GetString("\\\"\u1234x")
if s != `\fЗУ\\` {
t.Fatalf("unexpected string; got %q; want %q", s, `\fЗУ\\`)
}
})

t.Run("invalid-string-escape", func(t *testing.T) {
Expand Down Expand Up @@ -666,6 +686,10 @@ func TestParserParse(t *testing.T) {
if string(sb) != `{"foo": 123}` {
t.Fatalf("unexpected string value; got %q; want %q", sb, `{"foo": 123}`)
}
s := v.GetString()
if s != `{"foo": 123}` {
t.Fatalf("unexpected string value; got %q; want %q", s, `{"foo": 123}`)
}
})

t.Run("incomplete-object", func(t *testing.T) {
Expand Down