globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

Add support for JSON Marshalling bson.Raw #107

Closed SteelPhase closed 6 years ago

SteelPhase commented 6 years ago

The default behavior when JSON marshalling a bson.Raw field doesn't print the data represented by the bson.Raw field cleanly. Can a MarshalJSON method be added to bson.Raw which would allow the JSON marshaller to cleanly print a value in bson.Raw?

Current:

{
  "Kind": 4,
  "Data": "{something}"
}

Suggested:

{
  "field": "value"
},

Example MarshalJSON:

// MarshalJSON serializes raw bson into JSON
func (raw Raw) MarshalJSON() ([]byte, error) {
    var rawInterface interface{}

    err := raw.Unmarshal(&rawInterface)
    if err != nil {
        return nil, errors.New("unable to unmarshal BSON data into interface")
    }

    return json.Marshal(rawInterface)
}
domodwyer commented 6 years ago

Hi @SteelPhase

See the comments on the PR - totally up for this addition. I'm going to close this ticket given #108 should land pretty soon.

Thanks! Dom