-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap.go
31 lines (27 loc) · 962 Bytes
/
wrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package simplejsonx
import (
"github.com/bitly/go-simplejson"
"github.com/pkg/errors"
)
// Load creates a simplejson.Json instance from bytes, same as simplejson.NewJson(data).
func Load(data []byte) (simpleJson *simplejson.Json, err error) {
simpleJson, err = simplejson.NewJson(data)
if err != nil {
return simplejson.New(), errors.WithMessage(err, "unable to parse JSON")
}
return simpleJson, nil
}
// Wrap creates a simplejson.Json instance with the provided value as the root element.
func Wrap(value interface{}) (simpleJson *simplejson.Json) {
simpleJson = simplejson.New()
simpleJson.SetPath([]string{}, value)
return simpleJson
}
// List converts a slice of items into a slice of simplejson.Json objects.
func List(elements []interface{}) (simpleJsons []*simplejson.Json) {
simpleJsons = make([]*simplejson.Json, 0, len(elements))
for _, elem := range elements {
simpleJsons = append(simpleJsons, Wrap(elem))
}
return simpleJsons
}