json.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package bson
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "fmt"
  6. "gopkg.in/mgo.v2/internal/json"
  7. "strconv"
  8. "time"
  9. )
  10. // UnmarshalJSON unmarshals a JSON value that may hold non-standard
  11. // syntax as defined in BSON's extended JSON specification.
  12. func UnmarshalJSON(data []byte, value interface{}) error {
  13. d := json.NewDecoder(bytes.NewBuffer(data))
  14. d.Extend(&jsonExt)
  15. return d.Decode(value)
  16. }
  17. // MarshalJSON marshals a JSON value that may hold non-standard
  18. // syntax as defined in BSON's extended JSON specification.
  19. func MarshalJSON(value interface{}) ([]byte, error) {
  20. var buf bytes.Buffer
  21. e := json.NewEncoder(&buf)
  22. e.Extend(&jsonExt)
  23. err := e.Encode(value)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return buf.Bytes(), nil
  28. }
  29. // jdec is used internally by the JSON decoding functions
  30. // so they may unmarshal functions without getting into endless
  31. // recursion due to keyed objects.
  32. func jdec(data []byte, value interface{}) error {
  33. d := json.NewDecoder(bytes.NewBuffer(data))
  34. d.Extend(&funcExt)
  35. return d.Decode(value)
  36. }
  37. var jsonExt json.Extension
  38. var funcExt json.Extension
  39. // TODO
  40. // - Shell regular expressions ("/regexp/opts")
  41. func init() {
  42. jsonExt.DecodeUnquotedKeys(true)
  43. jsonExt.DecodeTrailingCommas(true)
  44. funcExt.DecodeFunc("BinData", "$binaryFunc", "$type", "$binary")
  45. jsonExt.DecodeKeyed("$binary", jdecBinary)
  46. jsonExt.DecodeKeyed("$binaryFunc", jdecBinary)
  47. jsonExt.EncodeType([]byte(nil), jencBinarySlice)
  48. jsonExt.EncodeType(Binary{}, jencBinaryType)
  49. funcExt.DecodeFunc("ISODate", "$dateFunc", "S")
  50. funcExt.DecodeFunc("new Date", "$dateFunc", "S")
  51. jsonExt.DecodeKeyed("$date", jdecDate)
  52. jsonExt.DecodeKeyed("$dateFunc", jdecDate)
  53. jsonExt.EncodeType(time.Time{}, jencDate)
  54. funcExt.DecodeFunc("Timestamp", "$timestamp", "t", "i")
  55. jsonExt.DecodeKeyed("$timestamp", jdecTimestamp)
  56. jsonExt.EncodeType(MongoTimestamp(0), jencTimestamp)
  57. funcExt.DecodeConst("undefined", Undefined)
  58. jsonExt.DecodeKeyed("$regex", jdecRegEx)
  59. jsonExt.EncodeType(RegEx{}, jencRegEx)
  60. funcExt.DecodeFunc("ObjectId", "$oidFunc", "Id")
  61. jsonExt.DecodeKeyed("$oid", jdecObjectId)
  62. jsonExt.DecodeKeyed("$oidFunc", jdecObjectId)
  63. jsonExt.EncodeType(ObjectId(""), jencObjectId)
  64. funcExt.DecodeFunc("DBRef", "$dbrefFunc", "$ref", "$id")
  65. jsonExt.DecodeKeyed("$dbrefFunc", jdecDBRef)
  66. funcExt.DecodeFunc("NumberLong", "$numberLongFunc", "N")
  67. jsonExt.DecodeKeyed("$numberLong", jdecNumberLong)
  68. jsonExt.DecodeKeyed("$numberLongFunc", jdecNumberLong)
  69. jsonExt.EncodeType(int64(0), jencNumberLong)
  70. jsonExt.EncodeType(int(0), jencInt)
  71. funcExt.DecodeConst("MinKey", MinKey)
  72. funcExt.DecodeConst("MaxKey", MaxKey)
  73. jsonExt.DecodeKeyed("$minKey", jdecMinKey)
  74. jsonExt.DecodeKeyed("$maxKey", jdecMaxKey)
  75. jsonExt.EncodeType(orderKey(0), jencMinMaxKey)
  76. jsonExt.DecodeKeyed("$undefined", jdecUndefined)
  77. jsonExt.EncodeType(Undefined, jencUndefined)
  78. jsonExt.Extend(&funcExt)
  79. }
  80. func fbytes(format string, args ...interface{}) []byte {
  81. var buf bytes.Buffer
  82. fmt.Fprintf(&buf, format, args...)
  83. return buf.Bytes()
  84. }
  85. func jdecBinary(data []byte) (interface{}, error) {
  86. var v struct {
  87. Binary []byte `json:"$binary"`
  88. Type string `json:"$type"`
  89. Func struct {
  90. Binary []byte `json:"$binary"`
  91. Type int64 `json:"$type"`
  92. } `json:"$binaryFunc"`
  93. }
  94. err := jdec(data, &v)
  95. if err != nil {
  96. return nil, err
  97. }
  98. var binData []byte
  99. var binKind int64
  100. if v.Type == "" && v.Binary == nil {
  101. binData = v.Func.Binary
  102. binKind = v.Func.Type
  103. } else if v.Type == "" {
  104. return v.Binary, nil
  105. } else {
  106. binData = v.Binary
  107. binKind, err = strconv.ParseInt(v.Type, 0, 64)
  108. if err != nil {
  109. binKind = -1
  110. }
  111. }
  112. if binKind == 0 {
  113. return binData, nil
  114. }
  115. if binKind < 0 || binKind > 255 {
  116. return nil, fmt.Errorf("invalid type in binary object: %s", data)
  117. }
  118. return Binary{Kind: byte(binKind), Data: binData}, nil
  119. }
  120. func jencBinarySlice(v interface{}) ([]byte, error) {
  121. in := v.([]byte)
  122. out := make([]byte, base64.StdEncoding.EncodedLen(len(in)))
  123. base64.StdEncoding.Encode(out, in)
  124. return fbytes(`{"$binary":"%s","$type":"0x0"}`, out), nil
  125. }
  126. func jencBinaryType(v interface{}) ([]byte, error) {
  127. in := v.(Binary)
  128. out := make([]byte, base64.StdEncoding.EncodedLen(len(in.Data)))
  129. base64.StdEncoding.Encode(out, in.Data)
  130. return fbytes(`{"$binary":"%s","$type":"0x%x"}`, out, in.Kind), nil
  131. }
  132. const jdateFormat = "2006-01-02T15:04:05.999Z"
  133. func jdecDate(data []byte) (interface{}, error) {
  134. var v struct {
  135. S string `json:"$date"`
  136. Func struct {
  137. S string
  138. } `json:"$dateFunc"`
  139. }
  140. _ = jdec(data, &v)
  141. if v.S == "" {
  142. v.S = v.Func.S
  143. }
  144. if v.S != "" {
  145. for _, format := range []string{jdateFormat, "2006-01-02"} {
  146. t, err := time.Parse(format, v.S)
  147. if err == nil {
  148. return t, nil
  149. }
  150. }
  151. return nil, fmt.Errorf("cannot parse date: %q", v.S)
  152. }
  153. var vn struct {
  154. Date struct {
  155. N int64 `json:"$numberLong,string"`
  156. } `json:"$date"`
  157. Func struct {
  158. S int64
  159. } `json:"$dateFunc"`
  160. }
  161. err := jdec(data, &vn)
  162. if err != nil {
  163. return nil, fmt.Errorf("cannot parse date: %q", data)
  164. }
  165. n := vn.Date.N
  166. if n == 0 {
  167. n = vn.Func.S
  168. }
  169. return time.Unix(n/1000, n%1000*1e6).UTC(), nil
  170. }
  171. func jencDate(v interface{}) ([]byte, error) {
  172. t := v.(time.Time)
  173. return fbytes(`{"$date":%q}`, t.Format(jdateFormat)), nil
  174. }
  175. func jdecTimestamp(data []byte) (interface{}, error) {
  176. var v struct {
  177. Func struct {
  178. T int32 `json:"t"`
  179. I int32 `json:"i"`
  180. } `json:"$timestamp"`
  181. }
  182. err := jdec(data, &v)
  183. if err != nil {
  184. return nil, err
  185. }
  186. return MongoTimestamp(uint64(v.Func.T)<<32 | uint64(uint32(v.Func.I))), nil
  187. }
  188. func jencTimestamp(v interface{}) ([]byte, error) {
  189. ts := uint64(v.(MongoTimestamp))
  190. return fbytes(`{"$timestamp":{"t":%d,"i":%d}}`, ts>>32, uint32(ts)), nil
  191. }
  192. func jdecRegEx(data []byte) (interface{}, error) {
  193. var v struct {
  194. Regex string `json:"$regex"`
  195. Options string `json:"$options"`
  196. }
  197. err := jdec(data, &v)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return RegEx{v.Regex, v.Options}, nil
  202. }
  203. func jencRegEx(v interface{}) ([]byte, error) {
  204. re := v.(RegEx)
  205. type regex struct {
  206. Regex string `json:"$regex"`
  207. Options string `json:"$options"`
  208. }
  209. return json.Marshal(regex{re.Pattern, re.Options})
  210. }
  211. func jdecObjectId(data []byte) (interface{}, error) {
  212. var v struct {
  213. Id string `json:"$oid"`
  214. Func struct {
  215. Id string
  216. } `json:"$oidFunc"`
  217. }
  218. err := jdec(data, &v)
  219. if err != nil {
  220. return nil, err
  221. }
  222. if v.Id == "" {
  223. v.Id = v.Func.Id
  224. }
  225. return ObjectIdHex(v.Id), nil
  226. }
  227. func jencObjectId(v interface{}) ([]byte, error) {
  228. return fbytes(`{"$oid":"%s"}`, v.(ObjectId).Hex()), nil
  229. }
  230. func jdecDBRef(data []byte) (interface{}, error) {
  231. // TODO Support unmarshaling $ref and $id into the input value.
  232. var v struct {
  233. Obj map[string]interface{} `json:"$dbrefFunc"`
  234. }
  235. // TODO Fix this. Must not be required.
  236. v.Obj = make(map[string]interface{})
  237. err := jdec(data, &v)
  238. if err != nil {
  239. return nil, err
  240. }
  241. return v.Obj, nil
  242. }
  243. func jdecNumberLong(data []byte) (interface{}, error) {
  244. var v struct {
  245. N int64 `json:"$numberLong,string"`
  246. Func struct {
  247. N int64 `json:",string"`
  248. } `json:"$numberLongFunc"`
  249. }
  250. var vn struct {
  251. N int64 `json:"$numberLong"`
  252. Func struct {
  253. N int64
  254. } `json:"$numberLongFunc"`
  255. }
  256. err := jdec(data, &v)
  257. if err != nil {
  258. err = jdec(data, &vn)
  259. v.N = vn.N
  260. v.Func.N = vn.Func.N
  261. }
  262. if err != nil {
  263. return nil, err
  264. }
  265. if v.N != 0 {
  266. return v.N, nil
  267. }
  268. return v.Func.N, nil
  269. }
  270. func jencNumberLong(v interface{}) ([]byte, error) {
  271. n := v.(int64)
  272. f := `{"$numberLong":"%d"}`
  273. if n <= 1<<53 {
  274. f = `{"$numberLong":%d}`
  275. }
  276. return fbytes(f, n), nil
  277. }
  278. func jencInt(v interface{}) ([]byte, error) {
  279. n := v.(int)
  280. f := `{"$numberLong":"%d"}`
  281. if int64(n) <= 1<<53 {
  282. f = `%d`
  283. }
  284. return fbytes(f, n), nil
  285. }
  286. func jdecMinKey(data []byte) (interface{}, error) {
  287. var v struct {
  288. N int64 `json:"$minKey"`
  289. }
  290. err := jdec(data, &v)
  291. if err != nil {
  292. return nil, err
  293. }
  294. if v.N != 1 {
  295. return nil, fmt.Errorf("invalid $minKey object: %s", data)
  296. }
  297. return MinKey, nil
  298. }
  299. func jdecMaxKey(data []byte) (interface{}, error) {
  300. var v struct {
  301. N int64 `json:"$maxKey"`
  302. }
  303. err := jdec(data, &v)
  304. if err != nil {
  305. return nil, err
  306. }
  307. if v.N != 1 {
  308. return nil, fmt.Errorf("invalid $maxKey object: %s", data)
  309. }
  310. return MaxKey, nil
  311. }
  312. func jencMinMaxKey(v interface{}) ([]byte, error) {
  313. switch v.(orderKey) {
  314. case MinKey:
  315. return []byte(`{"$minKey":1}`), nil
  316. case MaxKey:
  317. return []byte(`{"$maxKey":1}`), nil
  318. }
  319. panic(fmt.Sprintf("invalid $minKey/$maxKey value: %d", v))
  320. }
  321. func jdecUndefined(data []byte) (interface{}, error) {
  322. var v struct {
  323. B bool `json:"$undefined"`
  324. }
  325. err := jdec(data, &v)
  326. if err != nil {
  327. return nil, err
  328. }
  329. if !v.B {
  330. return nil, fmt.Errorf("invalid $undefined object: %s", data)
  331. }
  332. return Undefined, nil
  333. }
  334. func jencUndefined(v interface{}) ([]byte, error) {
  335. return []byte(`{"$undefined":true}`), nil
  336. }