feature_config.go 9.1 KB

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