https://github.com/everfore/bconv
Bson & Json Bytes Convert
https://github.com/everfore/bconv
Last synced: 5 months ago
JSON representation
Bson & Json Bytes Convert
- Host: GitHub
- URL: https://github.com/everfore/bconv
- Owner: everfore
- License: apache-2.0
- Created: 2015-07-31T02:35:47.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-04-02T02:37:06.000Z (about 8 years ago)
- Last Synced: 2025-08-13T20:21:44.905Z (11 months ago)
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bconv [](http://gocover.io/github.com/everfore/bconv)
Bson & Json Bytes Convert
----------------------------------
## Get
go get github.com/everfore/bconv
### 3rd party pkg
go get labix.org/v2/mgo/bson
## Define
json represents for interface{}, for instance struct, map, slice.
bson represents for bson.M, actually which is a map[string]interface{}.
## Convert Conclusion
### json <----> bson
json to bson, using bson.Marshal
bson to json, using json.Marshal
### jsonbytes <--X--> bsonbytes
Well, jsonbytes do not equal bsonbytes. They are different []byte.
We can not use string(bsonbytes) as string.
## Usage
### Init
var testBson = &bson.M{
"Name": "Golang",
}
b := NewBson(testBson)
type Lang struct {
Name string
}
var testJson = &Lang{
Name: "Golang ?",
}
j := NewJson(testJson)
### Bson
b := NewBson(testBson)
bbbs := b.BBytes()
t.Log(bbbs)
bjbs := b.JBytes()
t.Log(bjbs)
bj := b.Json()
t.Log(bj)
### Json
j := NewJson(testJson)
jbbs := j.BBytes()
t.Log(jbbs)
jjbs := j.JBytes()
t.Log(jjbs)
jb := j.Bson()
t.Log(jb)
### Conv
type Java struct {
Name string
}
var java Java
1. Json Conv
err := j.Conv(&java)
err := b.Json().Conv(&java)
2. Bson Conv
err := j.Bson().Conv(&java)
err := b.Conv(&java)