Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
45 changes: 45 additions & 0 deletions simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ func (j *Json) GetIndex(index int) *Json {
return &Json{nil}
}

// GetPathAny is like GetPath, except it can also go through arrays
// using GetIndex().
//
// js.GetPathAny("top_level", "entries", 3, "dict")
func (j *Json) GetPathAny(branch ...interface{}) *Json {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not just update GetPath to behave like this, it won't be backwards incompatible...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually I tried that at first, but one of the tests gave an error about converting []string to []interface{}, so it's not fully compatible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see, when passing v... as the argument where v is []string, hmmmm

jin := j
for _, p := range branch {
switch p.(type) {
case string:
jin = jin.Get(p.(string))
case int:
jin = jin.GetIndex(p.(int))
default:
jin = &Json{nil}
}
}
return jin
}

// CheckGet returns a pointer to a new `Json` object and
// a `bool` identifying success or failure
//
Expand All @@ -171,6 +190,32 @@ func (j *Json) CheckGet(key string) (*Json, bool) {
return nil, false
}

// JsonMap returns a copy of a Json map, but with values as Jsons
func (j *Json) JsonMap() (map[string]*Json, error) {
m, err := j.Map()
if err != nil {
return nil, err
}
jm := make(map[string]*Json)
for key, val := range m {
jm[key] = &Json{val}
}
return jm, nil
}

// JsonArray returns a copy of an array, but with each value as a Json
func (j *Json) JsonArray() ([]*Json, error) {
a, err := j.Array()
if err != nil {
return nil, err
}
ja := make([]*Json, len(a))
for key, val := range a {
ja[key] = &Json{val}
}
return ja, nil
}

// Map type asserts to `map`
func (j *Json) Map() (map[string]interface{}, error) {
if m, ok := (j.data).(map[string]interface{}); ok {
Expand Down
16 changes: 16 additions & 0 deletions simplejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ func TestSimplejson(t *testing.T) {
gp2, _ := js.GetPath("test", "int").Int()
assert.Equal(t, 10, gp2)

gpa, _ := js.GetPathAny("test", "string_array", 0).String()
assert.Equal(t, "asdf", gpa)

gpa2, _ := js.GetPathAny("test", "arraywithsubs", 1, "subkeythree").Int()
assert.Equal(t, 3, gpa2)

jm, err := js.Get("test").JsonMap()
assert.Equal(t, err, nil)
jmbool, _ := jm["bool"].Bool()
assert.Equal(t, true, jmbool)

ja, err := js.GetPath("test", "string_array").JsonArray()
assert.Equal(t, err, nil)
jastr, _ := ja[0].String()
assert.Equal(t, "asdf", jastr)

assert.Equal(t, js.Get("test").Get("bool").MustBool(), true)

js.Set("float2", 300.0)
Expand Down