| 1 |
package bson_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"gopkg.in/mgo.v2/bson" |
| 5 |
|
| 6 |
. "gopkg.in/check.v1" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
type jsonTest struct { |
| 13 |
a interface{} // value encoded into JSON (optional) |
| 14 |
b string // JSON expected as output of <a>, and used as input to <c> |
| 15 |
c interface{} // Value expected from decoding <b>, defaults to <a> |
| 16 |
e string // error string, if decoding (b) should fail |
| 17 |
} |
| 18 |
|
| 19 |
var jsonTests = []jsonTest{ |
| 20 |
// $binary |
| 21 |
{ |
| 22 |
a: []byte("foo"), |
| 23 |
b: `{"$binary":"Zm9v","$type":"0x0"}`, |
| 24 |
}, { |
| 25 |
a: bson.Binary{Kind: 2, Data: []byte("foo")}, |
| 26 |
b: `{"$binary":"Zm9v","$type":"0x2"}`, |
| 27 |
}, { |
| 28 |
b: `BinData(2,"Zm9v")`, |
| 29 |
c: bson.Binary{Kind: 2, Data: []byte("foo")}, |
| 30 |
}, |
| 31 |
|
| 32 |
// $date |
| 33 |
{ |
| 34 |
a: time.Date(2016, 5, 15, 1, 2, 3, 4000000, time.UTC), |
| 35 |
b: `{"$date":"2016-05-15T01:02:03.004Z"}`, |
| 36 |
}, { |
| 37 |
b: `{"$date": {"$numberLong": "1002"}}`, |
| 38 |
c: time.Date(1970, 1, 1, 0, 0, 1, 2e6, time.UTC), |
| 39 |
}, { |
| 40 |
b: `ISODate("2016-05-15T01:02:03.004Z")`, |
| 41 |
c: time.Date(2016, 5, 15, 1, 2, 3, 4000000, time.UTC), |
| 42 |
}, { |
| 43 |
b: `new Date(1000)`, |
| 44 |
c: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), |
| 45 |
}, { |
| 46 |
b: `new Date("2016-05-15")`, |
| 47 |
c: time.Date(2016, 5, 15, 0, 0, 0, 0, time.UTC), |
| 48 |
}, |
| 49 |
|
| 50 |
// $timestamp |
| 51 |
{ |
| 52 |
a: bson.MongoTimestamp(4294967298), |
| 53 |
b: `{"$timestamp":{"t":1,"i":2}}`, |
| 54 |
}, { |
| 55 |
b: `Timestamp(1, 2)`, |
| 56 |
c: bson.MongoTimestamp(4294967298), |
| 57 |
}, |
| 58 |
|
| 59 |
// $regex |
| 60 |
{ |
| 61 |
a: bson.RegEx{"pattern", "options"}, |
| 62 |
b: `{"$regex":"pattern","$options":"options"}`, |
| 63 |
}, |
| 64 |
|
| 65 |
// $oid |
| 66 |
{ |
| 67 |
a: bson.ObjectIdHex("0123456789abcdef01234567"), |
| 68 |
b: `{"$oid":"0123456789abcdef01234567"}`, |
| 69 |
}, { |
| 70 |
b: `ObjectId("0123456789abcdef01234567")`, |
| 71 |
c: bson.ObjectIdHex("0123456789abcdef01234567"), |
| 72 |
}, |
| 73 |
|
| 74 |
// $ref (no special type) |
| 75 |
{ |
| 76 |
b: `DBRef("name", "id")`, |
| 77 |
c: map[string]interface{}{"$ref": "name", "$id": "id"}, |
| 78 |
}, |
| 79 |
|
| 80 |
// $numberLong |
| 81 |
{ |
| 82 |
a: 123, |
| 83 |
b: `123`, |
| 84 |
}, { |
| 85 |
a: int64(9007199254740992), |
| 86 |
b: `{"$numberLong":9007199254740992}`, |
| 87 |
}, { |
| 88 |
a: int64(1<<53 + 1), |
| 89 |
b: `{"$numberLong":"9007199254740993"}`, |
| 90 |
}, { |
| 91 |
a: 1<<53 + 1, |
| 92 |
b: `{"$numberLong":"9007199254740993"}`, |
| 93 |
c: int64(9007199254740993), |
| 94 |
}, { |
| 95 |
b: `NumberLong(9007199254740992)`, |
| 96 |
c: int64(1 << 53), |
| 97 |
}, { |
| 98 |
b: `NumberLong("9007199254740993")`, |
| 99 |
c: int64(1<<53 + 1), |
| 100 |
}, |
| 101 |
|
| 102 |
// $minKey, $maxKey |
| 103 |
{ |
| 104 |
a: bson.MinKey, |
| 105 |
b: `{"$minKey":1}`, |
| 106 |
}, { |
| 107 |
a: bson.MaxKey, |
| 108 |
b: `{"$maxKey":1}`, |
| 109 |
}, { |
| 110 |
b: `MinKey`, |
| 111 |
c: bson.MinKey, |
| 112 |
}, { |
| 113 |
b: `MaxKey`, |
| 114 |
c: bson.MaxKey, |
| 115 |
}, { |
| 116 |
b: `{"$minKey":0}`, |
| 117 |
e: `invalid $minKey object: {"$minKey":0}`, |
| 118 |
}, { |
| 119 |
b: `{"$maxKey":0}`, |
| 120 |
e: `invalid $maxKey object: {"$maxKey":0}`, |
| 121 |
}, |
| 122 |
|
| 123 |
{ |
| 124 |
a: bson.Undefined, |
| 125 |
b: `{"$undefined":true}`, |
| 126 |
}, { |
| 127 |
b: `undefined`, |
| 128 |
c: bson.Undefined, |
| 129 |
}, { |
| 130 |
b: `{"v": undefined}`, |
| 131 |
c: struct{ V interface{} }{bson.Undefined}, |
| 132 |
}, |
| 133 |
|
| 134 |
// Unquoted keys and trailing commas |
| 135 |
{ |
| 136 |
b: `{$foo: ["bar",],}`, |
| 137 |
c: map[string]interface{}{"$foo": []interface{}{"bar"}}, |
| 138 |
}, |
| 139 |
} |
| 140 |
|
| 141 |
func (s *S) TestJSON(c *C) { |
| 142 |
for i, item := range jsonTests { |
| 143 |
c.Logf("------------ (#%d)", i) |
| 144 |
c.Logf("A: %#v", item.a) |
| 145 |
c.Logf("B: %#v", item.b) |
| 146 |
|
| 147 |
if item.c == nil { |
| 148 |
item.c = item.a |
| 149 |
} else { |
| 150 |
c.Logf("C: %#v", item.c) |
| 151 |
} |
| 152 |
if item.e != "" { |
| 153 |
c.Logf("E: %s", item.e) |
| 154 |
} |
| 155 |
|
| 156 |
if item.a != nil { |
| 157 |
data, err := bson.MarshalJSON(item.a) |
| 158 |
c.Assert(err, IsNil) |
| 159 |
c.Logf("Dumped: %#v", string(data)) |
| 160 |
c.Assert(strings.TrimSuffix(string(data), "\n"), Equals, item.b) |
| 161 |
} |
| 162 |
|
| 163 |
var zero interface{} |
| 164 |
if item.c == nil { |
| 165 |
zero = &struct{}{} |
| 166 |
} else { |
| 167 |
zero = reflect.New(reflect.TypeOf(item.c)).Interface() |
| 168 |
} |
| 169 |
err := bson.UnmarshalJSON([]byte(item.b), zero) |
| 170 |
if item.e != "" { |
| 171 |
c.Assert(err, NotNil) |
| 172 |
c.Assert(err.Error(), Equals, item.e) |
| 173 |
continue |
| 174 |
} |
| 175 |
c.Assert(err, IsNil) |
| 176 |
zerov := reflect.ValueOf(zero) |
| 177 |
value := zerov.Interface() |
| 178 |
if zerov.Kind() == reflect.Ptr { |
| 179 |
value = zerov.Elem().Interface() |
| 180 |
} |
| 181 |
c.Logf("Loaded: %#v", value) |
| 182 |
c.Assert(value, DeepEquals, item.c) |
| 183 |
} |
| 184 |
} |