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

     1  package jzon
     2  
     3  import (
     4  	"encoding"
     5  	"reflect"
     6  	"unsafe"
     7  )
     8  
     9  var (
    10  	textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
    11  )
    12  
    13  type textUnmarshalerDecoder rtype
    14  
    15  func (dec textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error {
    16  	obj := packEFace(rtype(dec), ptr)
    17  	unmarshaler := obj.(encoding.TextUnmarshaler)
    18  	c, err := it.nextToken()
    19  	if err != nil {
    20  		return err
    21  	}
    22  	switch c {
    23  	case '"':
    24  		it.head++
    25  		b, err := it.readStringAsSlice()
    26  		if err != nil {
    27  			return err
    28  		}
    29  		return unmarshaler.UnmarshalText(b)
    30  	case 'n':
    31  		it.head++
    32  		return it.expectBytes("ull")
    33  	default:
    34  		return UnexpectedByteError{got: c, exp: '"', exp2: 'n'}
    35  	}
    36  }