github.com/night-codes/go-json@v0.9.15/internal/decoder/bool.go (about)

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