json.go 8.8 KB

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