-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
104 lines (89 loc) · 1.98 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package bconv
import (
"encoding/json"
"fmt"
"gopkg.in/mgo.v2/bson"
)
func Bson2BsonBytes(m *bson.M) (b []byte, err error) {
return bson.Marshal(m)
}
func BsonBytes2Bson(b []byte) (ret *bson.M, err error) {
err = bson.Unmarshal(b, &ret)
return
}
func Bson2JsonBytes(m *bson.M) (b []byte, err error) {
return json.Marshal(m)
}
func JsonBytes2Bson(b []byte) (ret *bson.M, err error) {
err = json.Unmarshal(b, &ret)
return
}
// JsonBytes to Json,Interface{}
func JsonBytes2Json(b []byte, i interface{}) (err error) {
return json.Unmarshal(b, &i)
}
// Json,Interface to JsonBytes
func Json2JsonBytes(i interface{}) (b []byte, err error) {
return json.Marshal(i)
}
// can not conv BsonBytes to Json, also can not conv Json to BsonBytes.
// failed
func _Json2BsonBytes(i interface{}) (b []byte, err error) {
return bson.Marshal(i)
}
// failed
func _BsonBytes2Json(b []byte, i interface{}) (err error) {
return bson.Unmarshal(b, &i)
}
// Bson to Json,InterfaceP{} by JsonBytes
func Bson2Json(m *bson.M, i interface{}) error {
b, err := json.Marshal(m)
if isError(err) {
return err
}
return json.Unmarshal(b, &i)
}
// Bson to Json,InterfaceP{} by BsonBytes
// failed
func _Bson2Json(m *bson.M, i interface{}) error {
b, err := bson.Marshal(m)
if isError(err) {
return err
}
return bson.Unmarshal(b, &i)
}
// Json,Interface{} to Bson by JsonBytes
// failed
// Document is corrupted
func _Json2Bson(i interface{}) (m *bson.M, err error) {
b, err := json.Marshal(i)
if isError(err) {
return
}
err = bson.Unmarshal(b, &m)
return
}
// Json,Interface{} to Bson by BsonBytes
func Json2Bson(i interface{}) (m *bson.M, err error) {
b, err := bson.Marshal(i)
if isError(err) {
return
}
err = bson.Unmarshal(b, &m)
return
}
// Interface{} to Interface{}
func I2I(j interface{}, i interface{}) error {
b, err := json.Marshal(j)
if isError(err) {
return err
}
return json.Unmarshal(b, &i)
}
func isError(err error) bool {
if nil != err {
fmt.Println(err)
return true
}
return false
}