feature_config.go 8.4 KB

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