feature_reflect_extension.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "unicode"
  7. "unsafe"
  8. )
  9. var typeDecoders = map[string]ValDecoder{}
  10. var fieldDecoders = map[string]ValDecoder{}
  11. var typeEncoders = map[string]ValEncoder{}
  12. var fieldEncoders = map[string]ValEncoder{}
  13. var extensions = []Extension{}
  14. type StructDescriptor struct {
  15. onePtrEmbedded bool
  16. onePtrOptimization bool
  17. Type reflect.Type
  18. Fields []*Binding
  19. }
  20. func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding {
  21. for _, binding := range structDescriptor.Fields {
  22. if binding.Field.Name == fieldName {
  23. return binding
  24. }
  25. }
  26. return nil
  27. }
  28. type Binding struct {
  29. levels []int
  30. Field *reflect.StructField
  31. FromNames []string
  32. ToNames []string
  33. Encoder ValEncoder
  34. Decoder ValDecoder
  35. }
  36. type Extension interface {
  37. UpdateStructDescriptor(structDescriptor *StructDescriptor)
  38. CreateDecoder(typ reflect.Type) ValDecoder
  39. CreateEncoder(typ reflect.Type) ValEncoder
  40. DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder
  41. DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder
  42. }
  43. type DummyExtension struct {
  44. }
  45. func (extension *DummyExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
  46. }
  47. func (extension *DummyExtension) CreateDecoder(typ reflect.Type) ValDecoder {
  48. return nil
  49. }
  50. func (extension *DummyExtension) CreateEncoder(typ reflect.Type) ValEncoder {
  51. return nil
  52. }
  53. func (extension *DummyExtension) DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder {
  54. return decoder
  55. }
  56. func (extension *DummyExtension) DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder {
  57. return encoder
  58. }
  59. type funcDecoder struct {
  60. fun DecoderFunc
  61. }
  62. func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  63. decoder.fun(ptr, iter)
  64. }
  65. type funcEncoder struct {
  66. fun EncoderFunc
  67. isEmptyFunc func(ptr unsafe.Pointer) bool
  68. }
  69. func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  70. encoder.fun(ptr, stream)
  71. }
  72. func (encoder *funcEncoder) EncodeInterface(val interface{}, stream *Stream) {
  73. WriteToStream(val, stream, encoder)
  74. }
  75. func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  76. if encoder.isEmptyFunc == nil {
  77. return false
  78. }
  79. return encoder.isEmptyFunc(ptr)
  80. }
  81. func RegisterTypeDecoderFunc(typ string, fun DecoderFunc) {
  82. typeDecoders[typ] = &funcDecoder{fun}
  83. }
  84. func RegisterTypeDecoder(typ string, decoder ValDecoder) {
  85. typeDecoders[typ] = decoder
  86. }
  87. func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc) {
  88. RegisterFieldDecoder(typ, field, &funcDecoder{fun})
  89. }
  90. func RegisterFieldDecoder(typ string, field string, decoder ValDecoder) {
  91. fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = decoder
  92. }
  93. func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
  94. typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc}
  95. }
  96. func RegisterTypeEncoder(typ string, encoder ValEncoder) {
  97. typeEncoders[typ] = encoder
  98. }
  99. func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
  100. RegisterFieldEncoder(typ, field, &funcEncoder{fun, isEmptyFunc})
  101. }
  102. func RegisterFieldEncoder(typ string, field string, encoder ValEncoder) {
  103. fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = encoder
  104. }
  105. func RegisterExtension(extension Extension) {
  106. extensions = append(extensions, extension)
  107. }
  108. func getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
  109. decoder := _getTypeDecoderFromExtension(typ)
  110. if decoder != nil {
  111. for _, extension := range extensions {
  112. decoder = extension.DecorateDecoder(typ, decoder)
  113. }
  114. }
  115. return decoder
  116. }
  117. func _getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
  118. for _, extension := range extensions {
  119. decoder := extension.CreateDecoder(typ)
  120. if decoder != nil {
  121. return decoder
  122. }
  123. }
  124. typeName := typ.String()
  125. decoder := typeDecoders[typeName]
  126. if decoder != nil {
  127. return decoder
  128. }
  129. if typ.Kind() == reflect.Ptr {
  130. decoder := typeDecoders[typ.Elem().String()]
  131. if decoder != nil {
  132. return &optionalDecoder{typ.Elem(), decoder}
  133. }
  134. }
  135. return nil
  136. }
  137. func getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
  138. encoder := _getTypeEncoderFromExtension(typ)
  139. if encoder != nil {
  140. for _, extension := range extensions {
  141. encoder = extension.DecorateEncoder(typ, encoder)
  142. }
  143. }
  144. return encoder
  145. }
  146. func _getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
  147. for _, extension := range extensions {
  148. encoder := extension.CreateEncoder(typ)
  149. if encoder != nil {
  150. return encoder
  151. }
  152. }
  153. typeName := typ.String()
  154. encoder := typeEncoders[typeName]
  155. if encoder != nil {
  156. return encoder
  157. }
  158. if typ.Kind() == reflect.Ptr {
  159. encoder := typeEncoders[typ.Elem().String()]
  160. if encoder != nil {
  161. return &optionalEncoder{encoder}
  162. }
  163. }
  164. return nil
  165. }
  166. func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, error) {
  167. headAnonymousBindings := []*Binding{}
  168. tailAnonymousBindings := []*Binding{}
  169. bindings := []*Binding{}
  170. for i := 0; i < typ.NumField(); i++ {
  171. field := typ.Field(i)
  172. if field.Anonymous && field.Tag.Get("json") == "" {
  173. if field.Type.Kind() == reflect.Struct {
  174. structDescriptor, err := describeStruct(cfg, field.Type)
  175. if err != nil {
  176. return nil, err
  177. }
  178. for _, binding := range structDescriptor.Fields {
  179. binding.levels = append([]int{i}, binding.levels...)
  180. binding.Encoder = &structFieldEncoder{&field, binding.Encoder, false}
  181. binding.Decoder = &structFieldDecoder{&field, binding.Decoder}
  182. if field.Offset == 0 {
  183. headAnonymousBindings = append(headAnonymousBindings, binding)
  184. } else {
  185. tailAnonymousBindings = append(tailAnonymousBindings, binding)
  186. }
  187. }
  188. continue
  189. } else if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
  190. structDescriptor, err := describeStruct(cfg, field.Type.Elem())
  191. if err != nil {
  192. return nil, err
  193. }
  194. for _, binding := range structDescriptor.Fields {
  195. binding.levels = append([]int{i}, binding.levels...)
  196. binding.Encoder = &optionalEncoder{binding.Encoder}
  197. binding.Encoder = &structFieldEncoder{&field, binding.Encoder, false}
  198. binding.Decoder = &deferenceDecoder{field.Type.Elem(), binding.Decoder}
  199. binding.Decoder = &structFieldDecoder{&field, binding.Decoder}
  200. if field.Offset == 0 {
  201. headAnonymousBindings = append(headAnonymousBindings, binding)
  202. } else {
  203. tailAnonymousBindings = append(tailAnonymousBindings, binding)
  204. }
  205. }
  206. continue
  207. }
  208. }
  209. tagParts := strings.Split(field.Tag.Get("json"), ",")
  210. fieldNames := calcFieldNames(field.Name, tagParts[0], string(field.Tag.Get("json")))
  211. fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
  212. decoder := fieldDecoders[fieldCacheKey]
  213. if decoder == nil {
  214. var err error
  215. decoder, err = decoderOfType(cfg, field.Type)
  216. if err != nil {
  217. return nil, err
  218. }
  219. }
  220. encoder := fieldEncoders[fieldCacheKey]
  221. if encoder == nil {
  222. var err error
  223. encoder, err = encoderOfType(cfg, field.Type)
  224. if err != nil {
  225. return nil, err
  226. }
  227. // map is stored as pointer in the struct
  228. if field.Type.Kind() == reflect.Map {
  229. encoder = &optionalEncoder{encoder}
  230. }
  231. }
  232. binding := &Binding{
  233. Field: &field,
  234. FromNames: fieldNames,
  235. ToNames: fieldNames,
  236. Decoder: decoder,
  237. Encoder: encoder,
  238. }
  239. binding.levels = []int{i}
  240. bindings = append(bindings, binding)
  241. }
  242. return createStructDescriptor(cfg, typ, bindings, headAnonymousBindings, tailAnonymousBindings), nil
  243. }
  244. func createStructDescriptor(cfg *frozenConfig, typ reflect.Type, bindings []*Binding, headAnonymousBindings []*Binding, tailAnonymousBindings []*Binding) *StructDescriptor {
  245. onePtrEmbedded := false
  246. onePtrOptimization := false
  247. if typ.NumField() == 1 {
  248. firstField := typ.Field(0)
  249. switch firstField.Type.Kind() {
  250. case reflect.Ptr:
  251. if firstField.Anonymous && firstField.Type.Elem().Kind() == reflect.Struct {
  252. onePtrEmbedded = true
  253. }
  254. fallthrough
  255. case reflect.Map:
  256. onePtrOptimization = true
  257. }
  258. }
  259. structDescriptor := &StructDescriptor{
  260. onePtrEmbedded: onePtrEmbedded,
  261. onePtrOptimization: onePtrOptimization,
  262. Type: typ,
  263. Fields: bindings,
  264. }
  265. for _, extension := range extensions {
  266. extension.UpdateStructDescriptor(structDescriptor)
  267. }
  268. processTags(structDescriptor, cfg)
  269. // insert anonymous bindings to the head
  270. structDescriptor.Fields = append(headAnonymousBindings, structDescriptor.Fields...)
  271. structDescriptor.Fields = append(structDescriptor.Fields, tailAnonymousBindings...)
  272. return structDescriptor
  273. }
  274. func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
  275. for _, binding := range structDescriptor.Fields {
  276. shouldOmitEmpty := false
  277. tagParts := strings.Split(binding.Field.Tag.Get("json"), ",")
  278. for _, tagPart := range tagParts[1:] {
  279. if tagPart == "omitempty" {
  280. shouldOmitEmpty = true
  281. } else if tagPart == "string" {
  282. if binding.Field.Type.Kind() == reflect.String {
  283. binding.Decoder = &stringModeStringDecoder{binding.Decoder, cfg}
  284. binding.Encoder = &stringModeStringEncoder{binding.Encoder, cfg}
  285. } else {
  286. binding.Decoder = &stringModeNumberDecoder{binding.Decoder}
  287. binding.Encoder = &stringModeNumberEncoder{binding.Encoder}
  288. }
  289. }
  290. }
  291. binding.Decoder = &structFieldDecoder{binding.Field, binding.Decoder}
  292. binding.Encoder = &structFieldEncoder{binding.Field, binding.Encoder, shouldOmitEmpty}
  293. }
  294. }
  295. func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string {
  296. // ignore?
  297. if wholeTag == "-" {
  298. return []string{}
  299. }
  300. // rename?
  301. var fieldNames []string
  302. if tagProvidedFieldName == "" {
  303. fieldNames = []string{originalFieldName}
  304. } else {
  305. fieldNames = []string{tagProvidedFieldName}
  306. }
  307. // private?
  308. isNotExported := unicode.IsLower(rune(originalFieldName[0]))
  309. if isNotExported {
  310. fieldNames = []string{}
  311. }
  312. return fieldNames
  313. }