Introduction

The gobson project offers Go programs a flexible serializer and deserializer for BSON documents.

gobson was implemented to support the mgo MongoDB driver for Go.

Highlights

Here are some of the features of the gobson package:

  • Standard Marshal/Unmarshal interface, as established by the standard json package
  • Standard field renaming via tags
  • Best effort type mapping when unmarshalling values (e.g. 1 on bool == true, maps like map[int]string work, etc)
  • Conditional fields (see docs), enabling zero values to be excluded from the mapping
  • Comfortable Getter and Setter interfaces, for custom (un)marshalling
  • gobson.D{A, B, ...} type, for ordered documents
  • Very thorough test suite

API documentation

The API documentation is currently available at:

Mailing list

Discussion related to the use and development of gobson and mgo is held in the mgo-users mailing list.

Installing

To install gobson, simply run:

go get gopkg.in/mgo.v2/bson

Example

Here is a very simple example which dumps a Person struct. Note that the Phone field will not be included in the serialization, since its tag enables the conditional flag (",omitempty"), and the value is zeroed.

package main import ( "fmt" "gopkg.in/mgo.v2/bson" ) type Person struct { Name string Phone string ",omitempty" } func main() { data, err := bson.Marshal(&Person{Name:"Bob"}) if err != nil { panic(err) } fmt.Printf("%q", data) }

License

gobson is made available under the Simplified BSD License.

Releases