bconv

package module
v0.0.0-...-c557983 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2018 License: Apache-2.0 Imports: 3 Imported by: 0

README

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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bson2BsonBytes

func Bson2BsonBytes(m *bson.M) (b []byte, err error)

func Bson2Json

func Bson2Json(m *bson.M, i interface{}) error

Bson to Json,InterfaceP{} by JsonBytes

func Bson2JsonBytes

func Bson2JsonBytes(m *bson.M) (b []byte, err error)

func BsonBytes2Bson

func BsonBytes2Bson(b []byte) (ret *bson.M, err error)

func I2I

func I2I(j interface{}, i interface{}) error

Interface{} to Interface{}

func Json2Bson

func Json2Bson(i interface{}) (m *bson.M, err error)

Json,Interface{} to Bson by BsonBytes

func Json2JsonBytes

func Json2JsonBytes(i interface{}) (b []byte, err error)

Json,Interface to JsonBytes

func JsonBytes2Bson

func JsonBytes2Bson(b []byte) (ret *bson.M, err error)

func JsonBytes2Json

func JsonBytes2Json(b []byte, i interface{}) (err error)

JsonBytes to Json,Interface{}

Types

type Bson

type Bson struct {
	Bv *bson.M
}

func NewBson

func NewBson(v *bson.M) *Bson

func (*Bson) BBytes

func (b *Bson) BBytes() []byte

func (*Bson) Conv

func (b *Bson) Conv(i interface{}) error

func (*Bson) JBytes

func (b *Bson) JBytes() []byte

func (*Bson) Json

func (b *Bson) Json() *Json

func (*Bson) String

func (b *Bson) String() string

type Json

type Json struct {
	Jv interface{}
}

func NewJson

func NewJson(v interface{}) *Json

func (*Json) BBytes

func (j *Json) BBytes() []byte

func (*Json) Bson

func (j *Json) Bson() *Bson

func (*Json) Conv

func (j *Json) Conv(i interface{}) error

func (*Json) JBytes

func (j *Json) JBytes() []byte

return JsonBytes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL