Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JSON Marshalling bson.Raw #107

Closed
SteelPhase opened this issue Feb 16, 2018 · 1 comment
Closed

Add support for JSON Marshalling bson.Raw #107

SteelPhase opened this issue Feb 16, 2018 · 1 comment

Comments

@SteelPhase
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants