Skip to content
Closed
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
35 changes: 23 additions & 12 deletions gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ var (
ErrInvalidBuffer = errors.New("input buffer contained invalid JSON")
)

var (
r1 *strings.Replacer
r2 *strings.Replacer
)

func init() {
r1 = strings.NewReplacer("~1", "/", "~0", "~")
r2 = strings.NewReplacer("~1", ".", "~0", "~")
}

//------------------------------------------------------------------------------

// JSONPointerToSlice parses a JSON pointer path
Expand All @@ -97,9 +107,7 @@ func JSONPointerToSlice(path string) ([]string, error) {
}
hierarchy := strings.Split(path, "/")[1:]
for i, v := range hierarchy {
v = strings.Replace(v, "~1", "/", -1)
v = strings.Replace(v, "~0", "~", -1)
hierarchy[i] = v
hierarchy[i] = r1.Replace(v)
}
return hierarchy, nil
}
Expand All @@ -112,9 +120,7 @@ func JSONPointerToSlice(path string) ([]string, error) {
func DotPathToSlice(path string) []string {
hierarchy := strings.Split(path, ".")
for i, v := range hierarchy {
v = strings.Replace(v, "~1", ".", -1)
v = strings.Replace(v, "~0", "~", -1)
hierarchy[i] = v
hierarchy[i] = r2.Replace(v)
}
return hierarchy
}
Expand Down Expand Up @@ -148,17 +154,22 @@ func (g *Container) searchStrict(allowWildcard bool, hierarchy ...string) (*Cont
}
} else if marray, ok := object.([]interface{}); ok {
if allowWildcard && pathSeg == "*" {
tmpArray := []interface{}{}
for _, val := range marray {
if (target + 1) >= len(hierarchy) {
tmpArray = append(tmpArray, val)
} else if res := Wrap(val).Search(hierarchy[target+1:]...); res != nil {
tmpArray = append(tmpArray, res.Data())
var tmpArray []interface{}
if (target + 1) >= len(hierarchy) {
tmpArray = marray
} else {
tmpArray = make([]interface{}, 0, len(marray))
for _, val := range marray {
if res := Wrap(val).Search(hierarchy[target+1:]...); res != nil {
tmpArray = append(tmpArray, res.Data())
}
}
}

if len(tmpArray) == 0 {
return nil, nil
}

return &Container{tmpArray}, nil
}
index, err := strconv.Atoi(pathSeg)
Expand Down
19 changes: 19 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ dynamic approach.
*/

func BenchmarkStatic(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var jsonObj jsonStructure
json.Unmarshal(jsonContent, &jsonObj)
Expand All @@ -1595,6 +1596,7 @@ func BenchmarkStatic(b *testing.B) {
}

func BenchmarkDynamic(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jsonObj, err := ParseJSON(jsonContent)
if err != nil {
Expand Down Expand Up @@ -1904,3 +1906,20 @@ func TestFlattenIncludeEmpty(t *testing.T) {
}
}
}

func BenchmarkWildcardSearch(b *testing.B) {
sample := []byte(`{"test":[{"value":10},{"value":20}]}`)

val, err := ParseJSON(sample)
if err != nil {
b.Fatalf("Failed to parse: %v", err)
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
val.Search([]string{"test", "*"}...)
val.Search([]string{"test", "*", "value"}...)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/Jeffail/gabs/v2

go 1.18