https://github.com/romnn/bsonpb
serialize/deserialize golang protobuf messages into/from bson documents.
https://github.com/romnn/bsonpb
bson converter deserialize golang mongodb protobuf serialize
Last synced: 2 months ago
JSON representation
serialize/deserialize golang protobuf messages into/from bson documents.
- Host: GitHub
- URL: https://github.com/romnn/bsonpb
- Owner: romnn
- License: mit
- Created: 2020-02-27T09:51:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-01T18:35:57.000Z (over 4 years ago)
- Last Synced: 2025-04-03T16:15:21.581Z (3 months ago)
- Topics: bson, converter, deserialize, golang, mongodb, protobuf, serialize
- Language: Go
- Homepage:
- Size: 340 KB
- Stars: 6
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## bsonpb
[](https://travis-ci.com/romnn/bsonpb)
[](https://github.com/romnn/bsonpb)
[](https://godoc.org/github.com/romnn/bsonpb)
[](https://codecov.io/gh/romnn/bsonpb)
[](https://github.com/romnn/bsonpb/releases/latest)This package allows to serialize/deserialize golang `protobuf` messages into/from `bson` documents.
**Important notes**:
- This implementation has transitioned from the old [github.com/golang/protobuf](https://github.com/golang/protobuf) to the new [google.golang.org/protobuf](https://github.com/protocolbuffers/protobuf-go) API. The v1 implementation had various bazel related conflicts with the protobug dependency and is now abandoned under the `v1` branch.```go
import "github.com/romnn/bsonpb/v2" // only works with google.golang.org/protobuf, NOT github.com/golang/protobuf
```#### Usage (v2)
###### Marshaling
```golang
import "github.com/romnn/bsonpb/v2"myProto := &pb.Message{Name: "Test", Hilarity: pb.Message_SLAPSTICK}
opts := bsonpb.MarshalOptions{}
marshaled, err := opts.Marshal(someProto)
if err != nil {
log.Fatal(err)
}
log.Infof("Marshaled: %v", marshaled)
```###### Unmarshaling
```golang
import "github.com/romnn/bsonpb/v2"var myProto pb.Message
inputBson := bson.D{{Key: "Name", Value: "Test"}}
if err := bsonpb.Unmarshal(inputBson, &myProto); err != nil {
log.Fatal(err)
}
log.Infof("Unmarshaled: %v", myProto)
```If you want to try it, you can run the provided example with
```bash
bazel run //examples/v2:example
```#### Tests
```bash
bazel test //:go_default_test
bazel test //v2:go_default_test # v2 only
```#### Acknowledgements
- The v1 implementation was inspired by the official [github.com/golang/protobuf/jsonpb](https://github.com/golang/protobuf/tree/master/jsonpb) implementation.
- The v2 implementation was inspired by the official [google.golang.org/protobuf/encoding/protojson](https://github.com/protocolbuffers/protobuf-go/blob/master/encoding/protojson) implementation.