Convert bson []byte to json []byte

I am trying to unmarshal bson into an interface{} that can be type checked to native types (no primitives or bson.A or bson.Es), and/or convert a bson []byte to a json []byte.

One of the central issues I’m having with the go driver is that it pushes mongo’s type system into the business logic when using generic types like interface{}. If I write code to type check mongodb types, it won’t work if I input standard json unmarshaled map[string]interface{} fields.

I would like to do these things via the UnmarshalBSON / UnmarshalBSONValue interfaces so that I’m not messing with the Default registry. Is this possible?

Also, it would be awesome to see the driver move in this direction, or at least add an escape hatch. If the driver decodes generically to go native types other tools/code won’t have issues…and I won’t have to write a struct and unmarshaler for every weird type a javascript programmer decides to push to mongo :wink:

Hi @Dustin_Currie,

We’re definitely open to ideas that would make this kind of thing easier for users. Can you provide an example that shows what you’d like the driver to do? We don’t necessarily need a code sample, but it’d be helpful to have an example of a document in the database and some specific information about how you’d like the driver to store it after querying and what you want to do with it in your code.

– Divjot

1 Like