feature_adapter.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "io"
  5. )
  6. type RawMessage []byte
  7. // Unmarshal adapts to json/encoding Unmarshal API
  8. //
  9. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  10. // Refer to https://godoc.org/encoding/json#Unmarshal for more information
  11. func Unmarshal(data []byte, v interface{}) error {
  12. return ConfigDefault.Unmarshal(data, v)
  13. }
  14. func lastNotSpacePos(data []byte) int {
  15. for i := len(data) - 1; i >= 0; i-- {
  16. if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
  17. return i + 1
  18. }
  19. }
  20. return 0
  21. }
  22. func UnmarshalFromString(str string, v interface{}) error {
  23. return ConfigDefault.UnmarshalFromString(str, v)
  24. }
  25. func Get(data []byte, path ...interface{}) Any {
  26. return ConfigDefault.Get(data, path...)
  27. }
  28. // Marshal adapts to json/encoding Marshal API
  29. //
  30. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  31. // Refer to https://godoc.org/encoding/json#Marshal for more information
  32. func Marshal(v interface{}) ([]byte, error) {
  33. return ConfigDefault.Marshal(v)
  34. }
  35. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  36. return ConfigDefault.MarshalIndent(v, prefix, indent)
  37. }
  38. func MarshalToString(v interface{}) (string, error) {
  39. return ConfigDefault.MarshalToString(v)
  40. }
  41. // NewDecoder adapts to json/stream NewDecoder API.
  42. //
  43. // NewDecoder returns a new decoder that reads from r.
  44. //
  45. // Instead of a json/encoding Decoder, an Decoder is returned
  46. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  47. func NewDecoder(reader io.Reader) *Decoder {
  48. return ConfigDefault.NewDecoder(reader)
  49. }
  50. // Decoder reads and decodes JSON values from an input stream.
  51. // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  52. type Decoder struct {
  53. iter *Iterator
  54. }
  55. func (adapter *Decoder) Decode(obj interface{}) error {
  56. adapter.iter.ReadVal(obj)
  57. err := adapter.iter.Error
  58. if err == io.EOF {
  59. return nil
  60. }
  61. return adapter.iter.Error
  62. }
  63. func (adapter *Decoder) More() bool {
  64. return adapter.iter.head != adapter.iter.tail
  65. }
  66. func (adapter *Decoder) Buffered() io.Reader {
  67. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  68. return bytes.NewReader(remaining)
  69. }
  70. func (decoder *Decoder) UseNumber() {
  71. origCfg := decoder.iter.cfg.configBeforeFrozen
  72. origCfg.UseNumber = true
  73. decoder.iter.cfg = origCfg.Froze().(*frozenConfig)
  74. }
  75. func NewEncoder(writer io.Writer) *Encoder {
  76. return ConfigDefault.NewEncoder(writer)
  77. }
  78. type Encoder struct {
  79. stream *Stream
  80. }
  81. func (adapter *Encoder) Encode(val interface{}) error {
  82. adapter.stream.WriteVal(val)
  83. adapter.stream.Flush()
  84. return adapter.stream.Error
  85. }
  86. func (adapter *Encoder) SetIndent(prefix, indent string) {
  87. adapter.stream.cfg.indentionStep = len(indent)
  88. }
  89. func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
  90. config := adapter.stream.cfg.configBeforeFrozen
  91. config.EscapeHTML = escapeHtml
  92. adapter.stream.cfg = config.Froze().(*frozenConfig)
  93. }