feature_config.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "reflect"
  7. "unsafe"
  8. )
  9. // Config customize how the API should behave.
  10. // The API is created from Config by Froze.
  11. type Config struct {
  12. IndentionStep int
  13. MarshalFloatWith6Digits bool
  14. EscapeHTML bool
  15. SortMapKeys bool
  16. UseNumber bool
  17. TagKey string
  18. OnlyTaggedField bool
  19. ValidateJsonRawMessage bool
  20. ObjectFieldMustBeSimpleString bool
  21. }
  22. // API the public interface of this package.
  23. // Primary Marshal and Unmarshal.
  24. type API interface {
  25. IteratorPool
  26. StreamPool
  27. MarshalToString(v interface{}) (string, error)
  28. Marshal(v interface{}) ([]byte, error)
  29. MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
  30. UnmarshalFromString(str string, v interface{}) error
  31. Unmarshal(data []byte, v interface{}) error
  32. Get(data []byte, path ...interface{}) Any
  33. NewEncoder(writer io.Writer) *Encoder
  34. NewDecoder(reader io.Reader) *Decoder
  35. Valid(data []byte) bool
  36. RegisterExtension(extension Extension)
  37. }
  38. // ConfigDefault the default API
  39. var ConfigDefault = Config{
  40. EscapeHTML: true,
  41. }.Froze()
  42. // ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior
  43. var ConfigCompatibleWithStandardLibrary = Config{
  44. EscapeHTML: true,
  45. SortMapKeys: true,
  46. ValidateJsonRawMessage: true,
  47. }.Froze()
  48. // ConfigFastest marshals float with only 6 digits precision
  49. var ConfigFastest = Config{
  50. EscapeHTML: false,
  51. MarshalFloatWith6Digits: true, // will lose precession
  52. ObjectFieldMustBeSimpleString: true, // do not unescape object field
  53. }.Froze()
  54. // Froze forge API from config
  55. func (cfg Config) Froze() API {
  56. // TODO: cache frozen config
  57. frozenConfig := &frozenConfig{
  58. sortMapKeys: cfg.SortMapKeys,
  59. indentionStep: cfg.IndentionStep,
  60. objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
  61. onlyTaggedField: cfg.OnlyTaggedField,
  62. streamPool: make(chan *Stream, 16),
  63. iteratorPool: make(chan *Iterator, 16),
  64. }
  65. frozenConfig.initCache()
  66. if cfg.MarshalFloatWith6Digits {
  67. frozenConfig.marshalFloatWith6Digits()
  68. }
  69. if cfg.EscapeHTML {
  70. frozenConfig.escapeHTML()
  71. }
  72. if cfg.UseNumber {
  73. frozenConfig.useNumber()
  74. }
  75. if cfg.ValidateJsonRawMessage {
  76. frozenConfig.validateJsonRawMessage()
  77. }
  78. frozenConfig.configBeforeFrozen = cfg
  79. return frozenConfig
  80. }
  81. func (cfg *frozenConfig) validateJsonRawMessage() {
  82. encoder := &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
  83. rawMessage := *(*json.RawMessage)(ptr)
  84. iter := cfg.BorrowIterator([]byte(rawMessage))
  85. iter.Read()
  86. if iter.Error != nil {
  87. stream.WriteRaw("null")
  88. } else {
  89. cfg.ReturnIterator(iter)
  90. stream.WriteRaw(string(rawMessage))
  91. }
  92. }, func(ptr unsafe.Pointer) bool {
  93. return false
  94. }}
  95. cfg.addEncoderToCache(reflect.TypeOf((*json.RawMessage)(nil)).Elem(), encoder)
  96. cfg.addEncoderToCache(reflect.TypeOf((*RawMessage)(nil)).Elem(), encoder)
  97. }
  98. func (cfg *frozenConfig) useNumber() {
  99. cfg.addDecoderToCache(reflect.TypeOf((*interface{})(nil)).Elem(), &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
  100. if iter.WhatIsNext() == NumberValue {
  101. *((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
  102. } else {
  103. *((*interface{})(ptr)) = iter.Read()
  104. }
  105. }})
  106. }
  107. func (cfg *frozenConfig) getTagKey() string {
  108. tagKey := cfg.configBeforeFrozen.TagKey
  109. if tagKey == "" {
  110. return "json"
  111. }
  112. return tagKey
  113. }
  114. func (cfg *frozenConfig) RegisterExtension(extension Extension) {
  115. cfg.extensions = append(cfg.extensions, extension)
  116. }
  117. type lossyFloat32Encoder struct {
  118. }
  119. func (encoder *lossyFloat32Encoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  120. stream.WriteFloat32Lossy(*((*float32)(ptr)))
  121. }
  122. func (encoder *lossyFloat32Encoder) EncodeInterface(val interface{}, stream *Stream) {
  123. WriteToStream(val, stream, encoder)
  124. }
  125. func (encoder *lossyFloat32Encoder) IsEmpty(ptr unsafe.Pointer) bool {
  126. return *((*float32)(ptr)) == 0
  127. }
  128. type lossyFloat64Encoder struct {
  129. }
  130. func (encoder *lossyFloat64Encoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  131. stream.WriteFloat64Lossy(*((*float64)(ptr)))
  132. }
  133. func (encoder *lossyFloat64Encoder) EncodeInterface(val interface{}, stream *Stream) {
  134. WriteToStream(val, stream, encoder)
  135. }
  136. func (encoder *lossyFloat64Encoder) IsEmpty(ptr unsafe.Pointer) bool {
  137. return *((*float64)(ptr)) == 0
  138. }
  139. // EnableLossyFloatMarshalling keeps 10**(-6) precision
  140. // for float variables for better performance.
  141. func (cfg *frozenConfig) marshalFloatWith6Digits() {
  142. // for better performance
  143. cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &lossyFloat32Encoder{})
  144. cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &lossyFloat64Encoder{})
  145. }
  146. type htmlEscapedStringEncoder struct {
  147. }
  148. func (encoder *htmlEscapedStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  149. str := *((*string)(ptr))
  150. stream.WriteStringWithHTMLEscaped(str)
  151. }
  152. func (encoder *htmlEscapedStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
  153. WriteToStream(val, stream, encoder)
  154. }
  155. func (encoder *htmlEscapedStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  156. return *((*string)(ptr)) == ""
  157. }
  158. func (cfg *frozenConfig) escapeHTML() {
  159. cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &htmlEscapedStringEncoder{})
  160. }
  161. func (cfg *frozenConfig) cleanDecoders() {
  162. typeDecoders = map[string]ValDecoder{}
  163. fieldDecoders = map[string]ValDecoder{}
  164. *cfg = *(cfg.configBeforeFrozen.Froze().(*frozenConfig))
  165. }
  166. func (cfg *frozenConfig) cleanEncoders() {
  167. typeEncoders = map[string]ValEncoder{}
  168. fieldEncoders = map[string]ValEncoder{}
  169. *cfg = *(cfg.configBeforeFrozen.Froze().(*frozenConfig))
  170. }
  171. func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
  172. stream := cfg.BorrowStream(nil)
  173. defer cfg.ReturnStream(stream)
  174. stream.WriteVal(v)
  175. if stream.Error != nil {
  176. return "", stream.Error
  177. }
  178. return string(stream.Buffer()), nil
  179. }
  180. func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
  181. stream := cfg.BorrowStream(nil)
  182. defer cfg.ReturnStream(stream)
  183. stream.WriteVal(v)
  184. if stream.Error != nil {
  185. return nil, stream.Error
  186. }
  187. result := stream.Buffer()
  188. copied := make([]byte, len(result))
  189. copy(copied, result)
  190. return copied, nil
  191. }
  192. func (cfg *frozenConfig) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  193. if prefix != "" {
  194. panic("prefix is not supported")
  195. }
  196. for _, r := range indent {
  197. if r != ' ' {
  198. panic("indent can only be space")
  199. }
  200. }
  201. newCfg := cfg.configBeforeFrozen
  202. newCfg.IndentionStep = len(indent)
  203. return newCfg.Froze().Marshal(v)
  204. }
  205. func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
  206. data := []byte(str)
  207. data = data[:lastNotSpacePos(data)]
  208. iter := cfg.BorrowIterator(data)
  209. defer cfg.ReturnIterator(iter)
  210. iter.ReadVal(v)
  211. if iter.head == iter.tail {
  212. iter.loadMore()
  213. }
  214. if iter.Error == io.EOF {
  215. return nil
  216. }
  217. if iter.Error == nil {
  218. iter.ReportError("UnmarshalFromString", "there are bytes left after unmarshal")
  219. }
  220. return iter.Error
  221. }
  222. func (cfg *frozenConfig) Get(data []byte, path ...interface{}) Any {
  223. iter := cfg.BorrowIterator(data)
  224. defer cfg.ReturnIterator(iter)
  225. return locatePath(iter, path)
  226. }
  227. func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
  228. data = data[:lastNotSpacePos(data)]
  229. iter := cfg.BorrowIterator(data)
  230. defer cfg.ReturnIterator(iter)
  231. typ := reflect.TypeOf(v)
  232. if typ.Kind() != reflect.Ptr {
  233. // return non-pointer error
  234. return errors.New("the second param must be ptr type")
  235. }
  236. iter.ReadVal(v)
  237. if iter.head == iter.tail {
  238. iter.loadMore()
  239. }
  240. if iter.Error == io.EOF {
  241. return nil
  242. }
  243. if iter.Error == nil {
  244. iter.ReportError("Unmarshal", "there are bytes left after unmarshal")
  245. }
  246. return iter.Error
  247. }
  248. func (cfg *frozenConfig) NewEncoder(writer io.Writer) *Encoder {
  249. stream := NewStream(cfg, writer, 512)
  250. return &Encoder{stream}
  251. }
  252. func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
  253. iter := Parse(cfg, reader, 512)
  254. return &Decoder{iter}
  255. }
  256. func (cfg *frozenConfig) Valid(data []byte) bool {
  257. iter := cfg.BorrowIterator(data)
  258. defer cfg.ReturnIterator(iter)
  259. iter.Skip()
  260. return iter.Error == nil
  261. }