Skip to content

Commit

Permalink
Add Bytes() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Oct 25, 2015
1 parent 95f0b32 commit 5624c4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
4 changes: 4 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5624c4a

Please sign in to comment.