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

     1  package jzon
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // float32 decoder
     8  type float32Decoder struct {
     9  }
    10  
    11  func (*float32Decoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error {
    12  	c, err := it.nextToken()
    13  	if err != nil {
    14  		return err
    15  	}
    16  	if c == 'n' {
    17  		it.head++
    18  		return it.expectBytes("ull")
    19  	}
    20  	f, err := it.ReadFloat32()
    21  	if err != nil {
    22  		return err
    23  	}
    24  	*(*float32)(ptr) = f
    25  	return nil
    26  }
    27  
    28  // float64 decoder
    29  type float64Decoder struct {
    30  }
    31  
    32  func (*float64Decoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error {
    33  	c, err := it.nextToken()
    34  	if err != nil {
    35  		return err
    36  	}
    37  	if c == 'n' {
    38  		it.head++
    39  		return it.expectBytes("ull")
    40  	}
    41  	f, err := it.ReadFloat64()
    42  	if err != nil {
    43  		return err
    44  	}
    45  	*(*float64)(ptr) = f
    46  	return nil
    47  }