1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-20 18:48:32 +02:00

rename AdaptedDecoder => Decoder and AdaptedEncoder => Encoder

This commit is contained in:
Tao Wen 2017-06-20 08:00:43 +08:00
parent b3170a8cef
commit aa01f57b7f
2 changed files with 20 additions and 20 deletions

View File

@ -59,19 +59,19 @@ func MarshalToString(v interface{}) (string, error) {
// //
// NewDecoder returns a new decoder that reads from r. // NewDecoder returns a new decoder that reads from r.
// //
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned // Instead of a json/encoding Decoder, an Decoder is returned
// Refer to https://godoc.org/encoding/json#NewDecoder for more information // Refer to https://godoc.org/encoding/json#NewDecoder for more information
func NewDecoder(reader io.Reader) *AdaptedDecoder { func NewDecoder(reader io.Reader) *Decoder {
return ConfigDefault.NewDecoder(reader) return ConfigDefault.NewDecoder(reader)
} }
// AdaptedDecoder reads and decodes JSON values from an input stream. // Decoder reads and decodes JSON values from an input stream.
// AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress) // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
type AdaptedDecoder struct { type Decoder struct {
iter *Iterator iter *Iterator
} }
func (adapter *AdaptedDecoder) Decode(obj interface{}) error { func (adapter *Decoder) Decode(obj interface{}) error {
adapter.iter.ReadVal(obj) adapter.iter.ReadVal(obj)
err := adapter.iter.Error err := adapter.iter.Error
if err == io.EOF { if err == io.EOF {
@ -80,40 +80,40 @@ func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
return adapter.iter.Error return adapter.iter.Error
} }
func (adapter *AdaptedDecoder) More() bool { func (adapter *Decoder) More() bool {
return adapter.iter.head != adapter.iter.tail return adapter.iter.head != adapter.iter.tail
} }
func (adapter *AdaptedDecoder) Buffered() io.Reader { func (adapter *Decoder) Buffered() io.Reader {
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail] remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
return bytes.NewReader(remaining) return bytes.NewReader(remaining)
} }
func (decoder *AdaptedDecoder) UseNumber() { func (decoder *Decoder) UseNumber() {
origCfg := decoder.iter.cfg.configBeforeFrozen origCfg := decoder.iter.cfg.configBeforeFrozen
origCfg.UseNumber = true origCfg.UseNumber = true
decoder.iter.cfg = origCfg.Froze() decoder.iter.cfg = origCfg.Froze()
} }
func NewEncoder(writer io.Writer) *AdaptedEncoder { func NewEncoder(writer io.Writer) *Encoder {
return ConfigDefault.NewEncoder(writer) return ConfigDefault.NewEncoder(writer)
} }
type AdaptedEncoder struct { type Encoder struct {
stream *Stream stream *Stream
} }
func (adapter *AdaptedEncoder) Encode(val interface{}) error { func (adapter *Encoder) Encode(val interface{}) error {
adapter.stream.WriteVal(val) adapter.stream.WriteVal(val)
adapter.stream.Flush() adapter.stream.Flush()
return adapter.stream.Error return adapter.stream.Error
} }
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) { func (adapter *Encoder) SetIndent(prefix, indent string) {
adapter.stream.cfg.indentionStep = len(indent) adapter.stream.cfg.indentionStep = len(indent)
} }
func (adapter *AdaptedEncoder) SetEscapeHTML(escapeHtml bool) { func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
config := adapter.stream.cfg.configBeforeFrozen config := adapter.stream.cfg.configBeforeFrozen
config.EscapeHtml = escapeHtml config.EscapeHtml = escapeHtml
adapter.stream.cfg = config.Froze() adapter.stream.cfg = config.Froze()

View File

@ -35,8 +35,8 @@ type Api interface {
UnmarshalFromString(str string, v interface{}) error UnmarshalFromString(str string, v interface{}) error
Unmarshal(data []byte, v interface{}) error Unmarshal(data []byte, v interface{}) error
Get(data []byte, path ...interface{}) Any Get(data []byte, path ...interface{}) Any
NewEncoder(writer io.Writer) *AdaptedEncoder NewEncoder(writer io.Writer) *Encoder
NewDecoder(reader io.Reader) *AdaptedDecoder NewDecoder(reader io.Reader) *Decoder
} }
var ConfigDefault = Config{ var ConfigDefault = Config{
@ -281,12 +281,12 @@ func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
return iter.Error return iter.Error
} }
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *AdaptedEncoder { func (cfg *frozenConfig) NewEncoder(writer io.Writer) *Encoder {
stream := NewStream(cfg, writer, 512) stream := NewStream(cfg, writer, 512)
return &AdaptedEncoder{stream} return &Encoder{stream}
} }
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *AdaptedDecoder { func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
iter := Parse(cfg, reader, 512) iter := Parse(cfg, reader, 512)
return &AdaptedDecoder{iter} return &Decoder{iter}
} }