| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"github.com/goccy/go-json/internal/errors" |
| 10 |
) |
| 11 |
|
| 12 |
type bytesDecoder struct { |
| 13 |
typ reflect.Type |
| 14 |
sliceDecoder Decoder |
| 15 |
stringDecoder *stringDecoder |
| 16 |
structName string |
| 17 |
fieldName string |
| 18 |
// sliceType is the type of the slice, which the type errors report. |
| 19 |
sliceType reflect.Type |
| 20 |
} |
| 21 |
|
| 22 |
func byteUnmarshalerSliceDecoder(typ reflect.Type, structName string, fieldName string) Decoder { |
| 23 |
var unmarshalDecoder Decoder |
| 24 |
switch { |
| 25 |
case reflect.PointerTo(typ).Implements(unmarshalJSONType): |
| 26 |
unmarshalDecoder = newUnmarshalJSONDecoder(reflect.PointerTo(typ), structName, fieldName) |
| 27 |
case reflect.PointerTo(typ).Implements(unmarshalTextType): |
| 28 |
unmarshalDecoder = newUnmarshalTextDecoder(reflect.PointerTo(typ), structName, fieldName) |
| 29 |
default: |
| 30 |
unmarshalDecoder, _ = compileUint8(typ, structName, fieldName) |
| 31 |
} |
| 32 |
return newSliceDecoder(unmarshalDecoder, typ, 1, structName, fieldName) |
| 33 |
} |
| 34 |
|
| 35 |
func newBytesDecoder(typ reflect.Type, structName string, fieldName string) *bytesDecoder { |
| 36 |
return &bytesDecoder{ |
| 37 |
typ: typ, |
| 38 |
sliceType: reflect.SliceOf(typ), |
| 39 |
sliceDecoder: byteUnmarshalerSliceDecoder(typ, structName, fieldName), |
| 40 |
stringDecoder: newStringDecoder(structName, fieldName), |
| 41 |
structName: structName, |
| 42 |
fieldName: fieldName, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func (d *bytesDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 47 |
cursor = skipWhiteSpace(ctx.Buf, cursor) |
| 48 |
start := cursor |
| 49 |
bytes, c, err := d.decodeBinary(ctx, cursor, depth, p) |
| 50 |
if err != nil { |
| 51 |
if c := ctx.Buf[cursor]; c != '[' && isOtherValue(c, stringValue) { |
| 52 |
return ctx.skipTypeError(cursor, depth, d.sliceType) |
| 53 |
} |
| 54 |
return 0, err |
| 55 |
} |
| 56 |
if bytes == nil { |
| 57 |
return c, nil |
| 58 |
} |
| 59 |
cursor = c |
| 60 |
decodedLen := base64.StdEncoding.DecodedLen(len(bytes)) |
| 61 |
b := make([]byte, decodedLen) |
| 62 |
n, err := base64.StdEncoding.Decode(b, bytes) |
| 63 |
if err != nil { |
| 64 |
ctx.base64Error(start, cursor, d.sliceType, err) |
| 65 |
return cursor, nil |
| 66 |
} |
| 67 |
*(*[]byte)(p) = b[:n] |
| 68 |
return cursor, nil |
| 69 |
} |
| 70 |
|
| 71 |
func (d *bytesDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 72 |
return nil, 0, fmt.Errorf("json: []byte decoder does not support decode path") |
| 73 |
} |
| 74 |
|
| 75 |
func (d *bytesDecoder) decodeBinary(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) ([]byte, int64, error) { |
| 76 |
buf := ctx.Buf |
| 77 |
cursor = skipWhiteSpace(buf, cursor) |
| 78 |
if buf[cursor] == '[' { |
| 79 |
if d.sliceDecoder == nil { |
| 80 |
return nil, 0, &errors.UnmarshalTypeError{ |
| 81 |
Type: d.typ, |
| 82 |
Offset: cursor, |
| 83 |
} |
| 84 |
} |
| 85 |
c, err := d.sliceDecoder.Decode(ctx, cursor, depth, p) |
| 86 |
if err != nil { |
| 87 |
return nil, 0, err |
| 88 |
} |
| 89 |
return nil, c, nil |
| 90 |
} |
| 91 |
return d.stringDecoder.decodeByte(buf, cursor) |
| 92 |
} |