bool.go (1797B)
1 package decoder 2 3 import ( 4 "fmt" 5 "unsafe" 6 7 "github.com/goccy/go-json/internal/errors" 8 ) 9 10 type boolDecoder struct { 11 structName string 12 fieldName string 13 } 14 15 func newBoolDecoder(structName, fieldName string) *boolDecoder { 16 return &boolDecoder{structName: structName, fieldName: fieldName} 17 } 18 19 func (d *boolDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error { 20 c := s.skipWhiteSpace() 21 for { 22 switch c { 23 case 't': 24 if err := trueBytes(s); err != nil { 25 return err 26 } 27 **(**bool)(unsafe.Pointer(&p)) = true 28 return nil 29 case 'f': 30 if err := falseBytes(s); err != nil { 31 return err 32 } 33 **(**bool)(unsafe.Pointer(&p)) = false 34 return nil 35 case 'n': 36 if err := nullBytes(s); err != nil { 37 return err 38 } 39 return nil 40 case nul: 41 if s.read() { 42 c = s.char() 43 continue 44 } 45 goto ERROR 46 } 47 break 48 } 49 ERROR: 50 return errors.ErrUnexpectedEndOfJSON("bool", s.totalOffset()) 51 } 52 53 func (d *boolDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { 54 buf := ctx.Buf 55 cursor = skipWhiteSpace(buf, cursor) 56 switch buf[cursor] { 57 case 't': 58 if err := validateTrue(buf, cursor); err != nil { 59 return 0, err 60 } 61 cursor += 4 62 **(**bool)(unsafe.Pointer(&p)) = true 63 return cursor, nil 64 case 'f': 65 if err := validateFalse(buf, cursor); err != nil { 66 return 0, err 67 } 68 cursor += 5 69 **(**bool)(unsafe.Pointer(&p)) = false 70 return cursor, nil 71 case 'n': 72 if err := validateNull(buf, cursor); err != nil { 73 return 0, err 74 } 75 cursor += 4 76 return cursor, nil 77 } 78 return 0, errors.ErrUnexpectedEndOfJSON("bool", cursor) 79 } 80 81 func (d *boolDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { 82 return nil, 0, fmt.Errorf("json: bool decoder does not support decode path") 83 }