diff --git a/gabs.go b/gabs.go index ba60e0c..aad8612 100644 --- a/gabs.go +++ b/gabs.go @@ -431,6 +431,18 @@ func (g *Container) ArrayCountP(path string) (int, error) { /*--------------------------------------------------------------------------------------------------- */ +/* +Bytes - Converts the contained object back to a JSON []byte blob. +*/ +func (g *Container) Bytes() []byte { + if g.object != nil { + if bytes, err := json.Marshal(g.object); err == nil { + return bytes + } + } + return []byte("{}") +} + /* String - Converts the contained object back to a JSON formatted string. */ diff --git a/gabs_test.go b/gabs_test.go index 8b26ffc..c600e7c 100644 --- a/gabs_test.go +++ b/gabs_test.go @@ -34,6 +34,10 @@ func TestBasic(t *testing.T) { } else { t.Errorf("Didn't find test2") } + + if result := val.Bytes(); string(result) != string(sample) { + t.Errorf("Wrong []byte conversion: %s != %s", result, sample) + } } func TestFindArray(t *testing.T) {