gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

adapter.go (4598B)


      1 package jsoniter
      2 
      3 import (
      4 	"bytes"
      5 	"io"
      6 )
      7 
      8 // RawMessage to make replace json with jsoniter
      9 type RawMessage []byte
     10 
     11 // Unmarshal adapts to json/encoding Unmarshal API
     12 //
     13 // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
     14 // Refer to https://godoc.org/encoding/json#Unmarshal for more information
     15 func Unmarshal(data []byte, v interface{}) error {
     16 	return ConfigDefault.Unmarshal(data, v)
     17 }
     18 
     19 // UnmarshalFromString is a convenient method to read from string instead of []byte
     20 func UnmarshalFromString(str string, v interface{}) error {
     21 	return ConfigDefault.UnmarshalFromString(str, v)
     22 }
     23 
     24 // Get quick method to get value from deeply nested JSON structure
     25 func Get(data []byte, path ...interface{}) Any {
     26 	return ConfigDefault.Get(data, path...)
     27 }
     28 
     29 // Marshal adapts to json/encoding Marshal API
     30 //
     31 // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
     32 // Refer to https://godoc.org/encoding/json#Marshal for more information
     33 func Marshal(v interface{}) ([]byte, error) {
     34 	return ConfigDefault.Marshal(v)
     35 }
     36 
     37 // MarshalIndent same as json.MarshalIndent. Prefix is not supported.
     38 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
     39 	return ConfigDefault.MarshalIndent(v, prefix, indent)
     40 }
     41 
     42 // MarshalToString convenient method to write as string instead of []byte
     43 func MarshalToString(v interface{}) (string, error) {
     44 	return ConfigDefault.MarshalToString(v)
     45 }
     46 
     47 // NewDecoder adapts to json/stream NewDecoder API.
     48 //
     49 // NewDecoder returns a new decoder that reads from r.
     50 //
     51 // Instead of a json/encoding Decoder, an Decoder is returned
     52 // Refer to https://godoc.org/encoding/json#NewDecoder for more information
     53 func NewDecoder(reader io.Reader) *Decoder {
     54 	return ConfigDefault.NewDecoder(reader)
     55 }
     56 
     57 // Decoder reads and decodes JSON values from an input stream.
     58 // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
     59 type Decoder struct {
     60 	iter *Iterator
     61 }
     62 
     63 // Decode decode JSON into interface{}
     64 func (adapter *Decoder) Decode(obj interface{}) error {
     65 	if adapter.iter.head == adapter.iter.tail && adapter.iter.reader != nil {
     66 		if !adapter.iter.loadMore() {
     67 			return io.EOF
     68 		}
     69 	}
     70 	adapter.iter.ReadVal(obj)
     71 	err := adapter.iter.Error
     72 	if err == io.EOF {
     73 		return nil
     74 	}
     75 	return adapter.iter.Error
     76 }
     77 
     78 // More is there more?
     79 func (adapter *Decoder) More() bool {
     80 	iter := adapter.iter
     81 	if iter.Error != nil {
     82 		return false
     83 	}
     84 	c := iter.nextToken()
     85 	if c == 0 {
     86 		return false
     87 	}
     88 	iter.unreadByte()
     89 	return c != ']' && c != '}'
     90 }
     91 
     92 // Buffered remaining buffer
     93 func (adapter *Decoder) Buffered() io.Reader {
     94 	remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
     95 	return bytes.NewReader(remaining)
     96 }
     97 
     98 // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
     99 // Number instead of as a float64.
    100 func (adapter *Decoder) UseNumber() {
    101 	cfg := adapter.iter.cfg.configBeforeFrozen
    102 	cfg.UseNumber = true
    103 	adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
    104 }
    105 
    106 // DisallowUnknownFields causes the Decoder to return an error when the destination
    107 // is a struct and the input contains object keys which do not match any
    108 // non-ignored, exported fields in the destination.
    109 func (adapter *Decoder) DisallowUnknownFields() {
    110 	cfg := adapter.iter.cfg.configBeforeFrozen
    111 	cfg.DisallowUnknownFields = true
    112 	adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
    113 }
    114 
    115 // NewEncoder same as json.NewEncoder
    116 func NewEncoder(writer io.Writer) *Encoder {
    117 	return ConfigDefault.NewEncoder(writer)
    118 }
    119 
    120 // Encoder same as json.Encoder
    121 type Encoder struct {
    122 	stream *Stream
    123 }
    124 
    125 // Encode encode interface{} as JSON to io.Writer
    126 func (adapter *Encoder) Encode(val interface{}) error {
    127 	adapter.stream.WriteVal(val)
    128 	adapter.stream.WriteRaw("\n")
    129 	adapter.stream.Flush()
    130 	return adapter.stream.Error
    131 }
    132 
    133 // SetIndent set the indention. Prefix is not supported
    134 func (adapter *Encoder) SetIndent(prefix, indent string) {
    135 	config := adapter.stream.cfg.configBeforeFrozen
    136 	config.IndentionStep = len(indent)
    137 	adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
    138 }
    139 
    140 // SetEscapeHTML escape html by default, set to false to disable
    141 func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
    142 	config := adapter.stream.cfg.configBeforeFrozen
    143 	config.EscapeHTML = escapeHTML
    144 	adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
    145 }
    146 
    147 // Valid reports whether data is a valid JSON encoding.
    148 func Valid(data []byte) bool {
    149 	return ConfigDefault.Valid(data)
    150 }