feature_reflect_extension.go 8.5 KB

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