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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
package json
import (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"time"
)
const JSON_DATE_FORMAT = "2006-01-02T15:04:05.000Z"
func (b BinData) MarshalJSON() ([]byte, error) {
data := fmt.Sprintf(`{ "$binary": "%v", "$type": "%0x" }`,
b.Base64, []byte{b.Type})
return []byte(data), nil
}
func (d128 Decimal128) MarshalJSON() ([]byte, error) {
s := d128.Decimal128.String()
return []byte(fmt.Sprintf(`{ "$numberDecimal" : "%s" }`, s)), nil
}
func (js JavaScript) MarshalJSON() ([]byte, error) {
data := []byte(fmt.Sprintf(`{ "$code": %q`, js.Code))
scopeChunk := []byte{}
var err error
if js.Scope != nil {
scopeChunk, err = Marshal(js.Scope)
if err != nil {
return nil, err
}
scopeChunk = []byte(fmt.Sprintf(`, "$scope": %v `, string(scopeChunk)))
}
scopeChunk = append(scopeChunk, '}')
data = append(data, scopeChunk...)
return data, nil
}
func (d Date) MarshalJSON() ([]byte, error) {
var data string
n := int64(d)
if d.isFormatable() {
t := time.Unix(n/1e3, n%1e3*1e6)
data = fmt.Sprintf(`{ "$date": "%v" }`, t.UTC().Format(JSON_DATE_FORMAT))
} else {
data = fmt.Sprintf(`{ "$date": { "$numberLong" : "%v" }}`, n)
}
return []byte(data), nil
}
func (d DBRef) MarshalJSON() ([]byte, error) {
// Convert the $id field to JSON
idChunk, err := Marshal(d.Id)
if err != nil {
return nil, err
}
// Need to form JSON like { "$ref": "REF", "$id": ID, "$db": "DB" }
// so piece chunks together since can only get $id field as bytes.
refChunk := []byte(fmt.Sprintf(`{ "$ref": "%v", "$id": `, d.Collection))
dbChunk := []byte{}
if d.Database != "" {
dbChunk = []byte(fmt.Sprintf(`, "$db": "%v" `, d.Database))
}
dbChunk = append(dbChunk, '}')
data := make([]byte, len(refChunk)+len(idChunk)+len(dbChunk))
copy(data, refChunk)
copy(data[len(refChunk):], idChunk)
copy(data[len(refChunk)+len(idChunk):], dbChunk)
return data, nil
}
func (d DBPointer) MarshalJSON() ([]byte, error) {
buffer := bytes.Buffer{}
// Convert the $id field to JSON
idChunk, err := Marshal(d.Id)
if err != nil {
return nil, err
}
buffer.Write([]byte(fmt.Sprintf(`{ "$ref": "%v", "$id": { "$oid" : `, d.Namespace)))
buffer.Write(idChunk)
buffer.Write([]byte("}}"))
return buffer.Bytes(), nil
}
func (_ MinKey) MarshalJSON() ([]byte, error) {
data := `{ "$minKey": 1 }`
return []byte(data), nil
}
func (_ MaxKey) MarshalJSON() ([]byte, error) {
data := `{ "$maxKey": 1 }`
return []byte(data), nil
}
func (n NumberInt) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(int64(n), 10)), nil
}
func (n NumberLong) MarshalJSON() ([]byte, error) {
data := fmt.Sprintf(`{ "$numberLong": "%v" }`, int64(n))
return []byte(data), nil
}
func (n NumberFloat) MarshalJSON() ([]byte, error) {
// check floats for infinity and return +Infinity or -Infinity if so
if math.IsInf(float64(n), 1) {
return []byte("+Infinity"), nil
}
if math.IsInf(float64(n), -1) {
return []byte("-Infinity"), nil
}
floatString := strconv.FormatFloat(float64(n), 'g', -1, 64)
// determine if the float has a decimal point and if not
// add one to maintain consistency when importing.
if _, d := math.Modf(float64(n)); d == 0 {
// check for 'e' to determine if the float's formatted in scientific notation
if strings.IndexByte(floatString, 'e') == -1 {
return []byte(floatString + ".0"), nil
}
}
return []byte(floatString), nil
}
// Assumes that o represents a valid ObjectId
// (composed of 24 hexadecimal characters).
func (o ObjectId) MarshalJSON() ([]byte, error) {
data := fmt.Sprintf(`{ "$oid": "%v" }`, string(o))
return []byte(data), nil
}
func (r RegExp) MarshalJSON() ([]byte, error) {
pattern, err := Marshal(r.Pattern)
if err != nil {
return nil, err
}
data := fmt.Sprintf(`{ "$regex": %v, "$options": "%v" }`,
string(pattern), r.Options)
return []byte(data), nil
}
func (t Timestamp) MarshalJSON() ([]byte, error) {
data := fmt.Sprintf(`{ "$timestamp": { "t": %v, "i": %v } }`,
t.Seconds, t.Increment)
return []byte(data), nil
}
func (_ Undefined) MarshalJSON() ([]byte, error) {
data := `{ "$undefined": true }`
return []byte(data), nil
}
|