github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/decoder.go (about) 1 package jzon 2 3 import ( 4 "bytes" 5 "io" 6 ) 7 8 // Decoder is almost standard library compatible 9 // The following standard methods are not implemented 10 // - Token 11 type Decoder struct { 12 it *Iterator 13 err error 14 } 15 16 // Release decoder, decoder should not be reused after call 17 func (dec *Decoder) Release() { 18 dec.it.Release() 19 dec.it = nil 20 } 21 22 // UseNumber causes the Decoder to unmarshal a number into an interface{} as a 23 // Number instead of as a float64. 24 func (dec *Decoder) UseNumber() { 25 dec.it.useNumber = true 26 } 27 28 // DisallowUnknownFields causes the Decoder to return an error when the destination 29 // is a struct and the input contains object keys which do not match any 30 // non-ignored, exported fields in the destination. 31 func (dec *Decoder) DisallowUnknownFields() { 32 dec.it.disallowUnknownFields = true 33 } 34 35 // Decode reads the next JSON-encoded value from its 36 // input and stores it in the value pointed to by v. 37 func (dec *Decoder) Decode(v interface{}) error { 38 if dec.err == nil { 39 dec.err = dec.it.ReadVal(v) 40 } 41 return dec.err 42 } 43 44 // Buffered returns a reader of the data remaining in the Decoder's 45 // buffer. The reader is valid until the next call to Decode. 46 func (dec *Decoder) Buffered() io.Reader { 47 return bytes.NewReader(dec.it.Buffer()) 48 } 49 50 // func (dec *Decoder) Token() (json.Token, error) { 51 // panic("not implemented") 52 // } 53 54 // More reports whether there is another element in the 55 // current array or object being parsed. 56 func (dec *Decoder) More() bool { 57 if dec.err != nil { 58 return false 59 } 60 _, err := dec.it.nextToken() 61 return err == nil 62 } 63 64 // InputOffset returns the input stream byte offset of the current decoder position. 65 // The offset gives the location of the end of the most recently returned token 66 // and the beginning of the next token. 67 // Whitespace may present at the position of offset 68 func (dec *Decoder) InputOffset() int64 { 69 return int64(dec.it.offset + dec.it.head) 70 }