github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_json_number.go (about)

     1  package jzon
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  type jsonNumberDecoder struct{}
     8  
     9  func (*jsonNumberDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error {
    10  	c, err := it.nextToken()
    11  	if err != nil {
    12  		return err
    13  	}
    14  	var s string
    15  	switch valueTypeMap[c] {
    16  	case StringValue:
    17  		it.head++
    18  		c, err = it.nextByte()
    19  		if err != nil {
    20  			return err
    21  		}
    22  		if valueTypeMap[c] != NumberValue {
    23  			return InvalidDigitError{c}
    24  		}
    25  		it.head++
    26  		s, err = it.readNumberAsString(c)
    27  		if err != nil {
    28  			return err
    29  		}
    30  		c, err = it.nextByte()
    31  		if err != nil {
    32  			return err
    33  		}
    34  		if c != '"' {
    35  			return UnexpectedByteError{exp: '"', got: c}
    36  		}
    37  		it.head++
    38  		*((*string)(ptr)) = s
    39  		return nil
    40  	case NullValue:
    41  		// to be compatible with standard lib
    42  		it.head++
    43  		return it.expectBytes("ull")
    44  	case NumberValue:
    45  		it.head++
    46  		s, err = it.readNumberAsString(c)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		*((*string)(ptr)) = s
    51  		return nil
    52  	default:
    53  		return UnexpectedByteError{got: c}
    54  	}
    55  }