| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
// errStringOptionRest tells that the value of a string of the string option is followed by more bytes. |
| 14 |
errStringOptionRest = errors.New("json: value followed by more bytes in a string of the string option") |
| 15 |
// errMapKeyType is returned by the decoder of a key of a map which is not of the type of the keys, whose type |
| 16 |
// error is recorded: the map decodes the value of the entry, and drops the entry, as encoding/json does. |
| 17 |
errMapKeyType = errors.New("json: map key of another type") |
| 18 |
) |
| 19 |
|
| 20 |
type wrappedStringDecoder struct { |
| 21 |
typ reflect.Type |
| 22 |
dec Decoder |
| 23 |
stringDecoder *stringDecoder |
| 24 |
structName string |
| 25 |
fieldName string |
| 26 |
isPtrType bool |
| 27 |
// isMapKey is set for the decoder of the keys of a map, whose errors are the ones of the keys. |
| 28 |
isMapKey bool |
| 29 |
// numberKind is the kind of a number which the string has, read by strconv as encoding/json reads it ( see |
| 30 |
// decodeNumber ), or reflect.Invalid for a value of any other type. |
| 31 |
numberKind reflect.Kind |
| 32 |
// numberType is the type of the number, which typ is or points to. |
| 33 |
numberType reflect.Type |
| 34 |
// isJSONNumber is set for a json.Number, or a pointer to one, whose bytes are checked as encoding/json of the Go |
| 35 |
// version checks them ( see stringOptionNumber ). |
| 36 |
isJSONNumber bool |
| 37 |
} |
| 38 |
|
| 39 |
func newWrappedStringDecoder(typ reflect.Type, dec Decoder, structName, fieldName string) *wrappedStringDecoder { |
| 40 |
d := &wrappedStringDecoder{ |
| 41 |
typ: typ, |
| 42 |
dec: dec, |
| 43 |
stringDecoder: newStringDecoder(structName, fieldName), |
| 44 |
structName: structName, |
| 45 |
fieldName: fieldName, |
| 46 |
isPtrType: typ.Kind() == reflect.Ptr, |
| 47 |
} |
| 48 |
numberType := typ |
| 49 |
if numberType.Kind() == reflect.Pointer { |
| 50 |
numberType = numberType.Elem() |
| 51 |
} |
| 52 |
d.isJSONNumber = numberType == jsonNumberType |
| 53 |
switch numberType.Kind() { |
| 54 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 55 |
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, |
| 56 |
reflect.Float32, reflect.Float64: |
| 57 |
if numberType.Implements(unmarshalJSONType) || reflect.PointerTo(numberType).Implements(unmarshalJSONType) || |
| 58 |
reflect.PointerTo(numberType).Implements(unmarshalJSONContextType) || |
| 59 |
reflect.PointerTo(numberType).Implements(unmarshalTextType) { |
| 60 |
// a number decoded by its own unmarshaler |
| 61 |
break |
| 62 |
} |
| 63 |
d.numberKind, d.numberType = numberType.Kind(), numberType |
| 64 |
} |
| 65 |
return d |
| 66 |
} |
| 67 |
|
| 68 |
func (d *wrappedStringDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 69 |
buf := ctx.Buf |
| 70 |
cursor = skipWhiteSpace(buf, cursor) |
| 71 |
start := cursor |
| 72 |
if c := buf[cursor]; c != 'n' && isOtherValue(c, stringValue) { |
| 73 |
// a value which is not in a string |
| 74 |
if d.isPtrType && stringOptionUnquotedAllocates { |
| 75 |
d.allocate(p) |
| 76 |
} |
| 77 |
return ctx.stringOptionUnquoted(cursor, depth, d.typ) |
| 78 |
} |
| 79 |
bytes, c, err := d.stringDecoder.decodeByte(buf, cursor) |
| 80 |
if err != nil { |
| 81 |
return 0, err |
| 82 |
} |
| 83 |
if bytes == nil { |
| 84 |
if d.isPtrType { |
| 85 |
*(*unsafe.Pointer)(p) = nil |
| 86 |
} |
| 87 |
return c, nil |
| 88 |
} |
| 89 |
if d.isJSONNumber && !d.isMapKey { |
| 90 |
if stringOptionNumber(bytes) { |
| 91 |
*(*json.Number)(d.target(p)) = json.Number(ctx.makeString(bytes)) |
| 92 |
return c, nil |
| 93 |
} |
| 94 |
if !stringOptionNumberDecoded { |
| 95 |
return d.stringError(ctx, start, c, bytes, p) |
| 96 |
} |
| 97 |
} |
| 98 |
if d.numberKind != reflect.Invalid && len(bytes) > 0 { |
| 99 |
if d.decodeNumber(bytes, p) { |
| 100 |
return c, nil |
| 101 |
} |
| 102 |
return d.stringError(ctx, start, c, bytes, p) |
| 103 |
} |
| 104 |
if len(bytes) != 0 && (isWhiteSpace[bytes[0]] || isWhiteSpace[bytes[len(bytes)-1]]) { |
| 105 |
// encoding/json reads the bytes of the string as a value with nothing around it |
| 106 |
return d.stringError(ctx, start, c, bytes, p) |
| 107 |
} |
| 108 |
// The value is decoded from a copy of its bytes, which nothing else uses: its strings may refer to it. A type |
| 109 |
// error of it is the one of the string, which is made after it ( see stringOptionError ). |
| 110 |
saved := ctx.typeError |
| 111 |
ctx.typeError = nil |
| 112 |
oldBuf, oldOrigin := ctx.Buf, ctx.origin |
| 113 |
ctx.Buf, ctx.origin = NewInput(bytes), nil |
| 114 |
next, err := d.dec.Decode(ctx, 0, depth, p) |
| 115 |
if err == nil && skipWhiteSpace(ctx.Buf, next) != int64(len(bytes)) { |
| 116 |
// the value is followed by more bytes in the string |
| 117 |
err = errStringOptionRest |
| 118 |
} |
| 119 |
ctx.Buf, ctx.origin = oldBuf, oldOrigin |
| 120 |
failed := err != nil || ctx.typeError != nil |
| 121 |
ctx.typeError = saved |
| 122 |
if failed || len(bytes) == 0 { |
| 123 |
return d.stringError(ctx, start, c, bytes, p) |
| 124 |
} |
| 125 |
return c, nil |
| 126 |
} |
| 127 |
|
| 128 |
// stringError records or returns the error of the string between start and end, whose bytes are not a value of |
| 129 |
// the type. |
| 130 |
func (d *wrappedStringDecoder) stringError(ctx *RuntimeContext, start, end int64, bytes []byte, p unsafe.Pointer) (int64, error) { |
| 131 |
if d.isMapKey { |
| 132 |
ctx.keyTypeError(start, end, bytes, d.elemType()) |
| 133 |
return end, errMapKeyType |
| 134 |
} |
| 135 |
if d.isPtrType && stringOptionAllocates(bytes) { |
| 136 |
d.allocate(p) |
| 137 |
} |
| 138 |
return ctx.stringOptionError(start, end, bytes, d.typ) |
| 139 |
} |
| 140 |
|
| 141 |
// decodeNumber reads the number which the bytes of the string are, as encoding/json reads the number of a string of |
| 142 |
// the string option or of a key of a map, by strconv: a number which it fails, or which is out of the range of the |
| 143 |
// type, is not stored. It reports whether the number is stored. |
| 144 |
func (d *wrappedStringDecoder) decodeNumber(bytes []byte, p unsafe.Pointer) bool { |
| 145 |
if !d.isMapKey && !stringOptionNumberStart(bytes[0]) { |
| 146 |
return false |
| 147 |
} |
| 148 |
s := *(*string)(unsafe.Pointer(&bytes)) |
| 149 |
bits := int(d.numberType.Size() * 8) |
| 150 |
switch d.numberKind { |
| 151 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 152 |
n, err := strconv.ParseInt(s, 10, bits) |
| 153 |
if err != nil { |
| 154 |
return false |
| 155 |
} |
| 156 |
storeInt(d.target(p), bits, uint64(n)) |
| 157 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 158 |
n, err := strconv.ParseUint(s, 10, bits) |
| 159 |
if err != nil { |
| 160 |
return false |
| 161 |
} |
| 162 |
storeInt(d.target(p), bits, n) |
| 163 |
default: |
| 164 |
n, err := strconv.ParseFloat(s, bits) |
| 165 |
if err != nil { |
| 166 |
return false |
| 167 |
} |
| 168 |
if bits == 32 { |
| 169 |
*(*float32)(d.target(p)) = float32(n) |
| 170 |
} else { |
| 171 |
*(*float64)(d.target(p)) = n |
| 172 |
} |
| 173 |
} |
| 174 |
return true |
| 175 |
} |
| 176 |
|
| 177 |
// storeInt stores the lowest bits of n, an integer of the size of bits, at p. |
| 178 |
func storeInt(p unsafe.Pointer, bits int, n uint64) { |
| 179 |
switch bits { |
| 180 |
case 8: |
| 181 |
*(*uint8)(p) = uint8(n) |
| 182 |
case 16: |
| 183 |
*(*uint16)(p) = uint16(n) |
| 184 |
case 32: |
| 185 |
*(*uint32)(p) = uint32(n) |
| 186 |
default: |
| 187 |
*(*uint64)(p) = n |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// target returns where the number is stored: at p, or where the pointer at p points to, which is allocated if it |
| 192 |
// is nil. |
| 193 |
func (d *wrappedStringDecoder) target(p unsafe.Pointer) unsafe.Pointer { |
| 194 |
if !d.isPtrType { |
| 195 |
return p |
| 196 |
} |
| 197 |
d.allocate(p) |
| 198 |
return *(*unsafe.Pointer)(p) |
| 199 |
} |
| 200 |
|
| 201 |
// elemType returns the type of the value, which the pointers of the type point to. |
| 202 |
func (d *wrappedStringDecoder) elemType() reflect.Type { |
| 203 |
typ := d.typ |
| 204 |
for typ.Kind() == reflect.Pointer { |
| 205 |
typ = typ.Elem() |
| 206 |
} |
| 207 |
return typ |
| 208 |
} |
| 209 |
|
| 210 |
// allocate sets the pointer at p, if it is nil, to a zero value, as encoding/json does for a value it fails to |
| 211 |
// decode. |
| 212 |
func (d *wrappedStringDecoder) allocate(p unsafe.Pointer) { |
| 213 |
if *(*unsafe.Pointer)(p) == nil { |
| 214 |
*(*unsafe.Pointer)(p) = newValue(d.typ.Elem()) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func (d *wrappedStringDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 219 |
return nil, 0, fmt.Errorf("json: wrapped string decoder does not support decode path") |
| 220 |
} |