| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"unsafe" |
| 7 |
|
| 8 |
"github.com/goccy/go-json/internal/errors" |
| 9 |
) |
| 10 |
|
| 11 |
type boolDecoder struct { |
| 12 |
structName string |
| 13 |
fieldName string |
| 14 |
// typ is the type of the value, which the type errors report. |
| 15 |
typ reflect.Type |
| 16 |
} |
| 17 |
|
| 18 |
func newBoolDecoder(structName, fieldName string) *boolDecoder { |
| 19 |
return &boolDecoder{structName: structName, fieldName: fieldName, typ: reflect.TypeOf(false)} |
| 20 |
} |
| 21 |
|
| 22 |
func (d *boolDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 23 |
buf := ctx.Buf |
| 24 |
cursor = skipWhiteSpace(buf, cursor) |
| 25 |
switch buf[cursor] { |
| 26 |
case 't': |
| 27 |
if err := validateTrue(buf, cursor); err != nil { |
| 28 |
return 0, err |
| 29 |
} |
| 30 |
cursor += 4 |
| 31 |
**(**bool)(unsafe.Pointer(&p)) = true |
| 32 |
return cursor, nil |
| 33 |
case 'f': |
| 34 |
if err := validateFalse(buf, cursor); err != nil { |
| 35 |
return 0, err |
| 36 |
} |
| 37 |
cursor += 5 |
| 38 |
**(**bool)(unsafe.Pointer(&p)) = false |
| 39 |
return cursor, nil |
| 40 |
case 'n': |
| 41 |
if err := validateNull(buf, cursor); err != nil { |
| 42 |
return 0, err |
| 43 |
} |
| 44 |
cursor += 4 |
| 45 |
return cursor, nil |
| 46 |
} |
| 47 |
return d.decodeOther(ctx, cursor, depth) |
| 48 |
} |
| 49 |
|
| 50 |
// decodeOther skips the value at cursor, which is not a bool: a value of another kind is a type error, and |
| 51 |
// anything else a syntax error. It is a function of its own, so that Decode keeps the size it had. |
| 52 |
// |
| 53 |
//go:noinline |
| 54 |
func (d *boolDecoder) decodeOther(ctx *RuntimeContext, cursor, depth int64) (int64, error) { |
| 55 |
if isOtherValue(ctx.Buf[cursor], boolValue) { |
| 56 |
return ctx.skipTypeError(cursor, depth, d.typ) |
| 57 |
} |
| 58 |
return 0, errors.ErrUnexpectedEndOfJSON("bool", cursor) |
| 59 |
} |
| 60 |
|
| 61 |
func (d *boolDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 62 |
return nil, 0, fmt.Errorf("json: bool decoder does not support decode path") |
| 63 |
} |