feature_config.go 8.3 KB

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