另客网go项目公用的代码库

reflect_map.go 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "github.com/modern-go/reflect2"
  5. "io"
  6. "reflect"
  7. "sort"
  8. "unsafe"
  9. )
  10. func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder {
  11. mapType := typ.(*reflect2.UnsafeMapType)
  12. keyDecoder := decoderOfMapKey(ctx.append("[mapKey]"), mapType.Key())
  13. elemDecoder := decoderOfType(ctx.append("[mapElem]"), mapType.Elem())
  14. return &mapDecoder{
  15. mapType: mapType,
  16. keyType: mapType.Key(),
  17. elemType: mapType.Elem(),
  18. keyDecoder: keyDecoder,
  19. elemDecoder: elemDecoder,
  20. }
  21. }
  22. func encoderOfMap(ctx *ctx, typ reflect2.Type) ValEncoder {
  23. mapType := typ.(*reflect2.UnsafeMapType)
  24. if ctx.sortMapKeys {
  25. return &sortKeysMapEncoder{
  26. mapType: mapType,
  27. keyEncoder: encoderOfMapKey(ctx.append("[mapKey]"), mapType.Key()),
  28. elemEncoder: encoderOfType(ctx.append("[mapElem]"), mapType.Elem()),
  29. }
  30. }
  31. return &mapEncoder{
  32. mapType: mapType,
  33. keyEncoder: encoderOfMapKey(ctx.append("[mapKey]"), mapType.Key()),
  34. elemEncoder: encoderOfType(ctx.append("[mapElem]"), mapType.Elem()),
  35. }
  36. }
  37. func decoderOfMapKey(ctx *ctx, typ reflect2.Type) ValDecoder {
  38. decoder := ctx.decoderExtension.CreateMapKeyDecoder(typ)
  39. if decoder != nil {
  40. return decoder
  41. }
  42. for _, extension := range ctx.extraExtensions {
  43. decoder := extension.CreateMapKeyDecoder(typ)
  44. if decoder != nil {
  45. return decoder
  46. }
  47. }
  48. switch typ.Kind() {
  49. case reflect.String:
  50. return decoderOfType(ctx, reflect2.DefaultTypeOfKind(reflect.String))
  51. case reflect.Bool,
  52. reflect.Uint8, reflect.Int8,
  53. reflect.Uint16, reflect.Int16,
  54. reflect.Uint32, reflect.Int32,
  55. reflect.Uint64, reflect.Int64,
  56. reflect.Uint, reflect.Int,
  57. reflect.Float32, reflect.Float64,
  58. reflect.Uintptr:
  59. typ = reflect2.DefaultTypeOfKind(typ.Kind())
  60. return &numericMapKeyDecoder{decoderOfType(ctx, typ)}
  61. default:
  62. ptrType := reflect2.PtrTo(typ)
  63. if ptrType.Implements(textMarshalerType) {
  64. return &referenceDecoder{
  65. &textUnmarshalerDecoder{
  66. valType: ptrType,
  67. },
  68. }
  69. }
  70. if typ.Implements(textMarshalerType) {
  71. return &textUnmarshalerDecoder{
  72. valType: typ,
  73. }
  74. }
  75. return &lazyErrorDecoder{err: fmt.Errorf("unsupported map key type: %v", typ)}
  76. }
  77. }
  78. func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
  79. encoder := ctx.encoderExtension.CreateMapKeyEncoder(typ)
  80. if encoder != nil {
  81. return encoder
  82. }
  83. for _, extension := range ctx.extraExtensions {
  84. encoder := extension.CreateMapKeyEncoder(typ)
  85. if encoder != nil {
  86. return encoder
  87. }
  88. }
  89. switch typ.Kind() {
  90. case reflect.String:
  91. return encoderOfType(ctx, reflect2.DefaultTypeOfKind(reflect.String))
  92. case reflect.Bool,
  93. reflect.Uint8, reflect.Int8,
  94. reflect.Uint16, reflect.Int16,
  95. reflect.Uint32, reflect.Int32,
  96. reflect.Uint64, reflect.Int64,
  97. reflect.Uint, reflect.Int,
  98. reflect.Float32, reflect.Float64,
  99. reflect.Uintptr:
  100. typ = reflect2.DefaultTypeOfKind(typ.Kind())
  101. return &numericMapKeyEncoder{encoderOfType(ctx, typ)}
  102. default:
  103. if typ == textMarshalerType {
  104. return &directTextMarshalerEncoder{
  105. stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
  106. }
  107. }
  108. if typ.Implements(textMarshalerType) {
  109. return &textMarshalerEncoder{
  110. valType: typ,
  111. stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
  112. }
  113. }
  114. if typ.Kind() == reflect.Interface {
  115. return &dynamicMapKeyEncoder{ctx, typ}
  116. }
  117. return &lazyErrorEncoder{err: fmt.Errorf("unsupported map key type: %v", typ)}
  118. }
  119. }
  120. type mapDecoder struct {
  121. mapType *reflect2.UnsafeMapType
  122. keyType reflect2.Type
  123. elemType reflect2.Type
  124. keyDecoder ValDecoder
  125. elemDecoder ValDecoder
  126. }
  127. func (decoder *mapDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  128. mapType := decoder.mapType
  129. c := iter.nextToken()
  130. if c == 'n' {
  131. iter.skipThreeBytes('u', 'l', 'l')
  132. *(*unsafe.Pointer)(ptr) = nil
  133. mapType.UnsafeSet(ptr, mapType.UnsafeNew())
  134. return
  135. }
  136. if mapType.UnsafeIsNil(ptr) {
  137. mapType.UnsafeSet(ptr, mapType.UnsafeMakeMap(0))
  138. }
  139. if c != '{' {
  140. iter.ReportError("ReadMapCB", `expect { or n, but found `+string([]byte{c}))
  141. return
  142. }
  143. c = iter.nextToken()
  144. if c == '}' {
  145. return
  146. }
  147. if c != '"' {
  148. iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
  149. return
  150. }
  151. iter.unreadByte()
  152. key := decoder.keyType.UnsafeNew()
  153. decoder.keyDecoder.Decode(key, iter)
  154. c = iter.nextToken()
  155. if c != ':' {
  156. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  157. return
  158. }
  159. elem := decoder.elemType.UnsafeNew()
  160. decoder.elemDecoder.Decode(elem, iter)
  161. decoder.mapType.UnsafeSetIndex(ptr, key, elem)
  162. for c = iter.nextToken(); c == ','; c = iter.nextToken() {
  163. key := decoder.keyType.UnsafeNew()
  164. decoder.keyDecoder.Decode(key, iter)
  165. c = iter.nextToken()
  166. if c != ':' {
  167. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  168. return
  169. }
  170. elem := decoder.elemType.UnsafeNew()
  171. decoder.elemDecoder.Decode(elem, iter)
  172. decoder.mapType.UnsafeSetIndex(ptr, key, elem)
  173. }
  174. if c != '}' {
  175. iter.ReportError("ReadMapCB", `expect }, but found `+string([]byte{c}))
  176. }
  177. }
  178. type numericMapKeyDecoder struct {
  179. decoder ValDecoder
  180. }
  181. func (decoder *numericMapKeyDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  182. c := iter.nextToken()
  183. if c != '"' {
  184. iter.ReportError("ReadMapCB", `expect ", but found `+string([]byte{c}))
  185. return
  186. }
  187. decoder.decoder.Decode(ptr, iter)
  188. c = iter.nextToken()
  189. if c != '"' {
  190. iter.ReportError("ReadMapCB", `expect ", but found `+string([]byte{c}))
  191. return
  192. }
  193. }
  194. type numericMapKeyEncoder struct {
  195. encoder ValEncoder
  196. }
  197. func (encoder *numericMapKeyEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  198. stream.writeByte('"')
  199. encoder.encoder.Encode(ptr, stream)
  200. stream.writeByte('"')
  201. }
  202. func (encoder *numericMapKeyEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  203. return false
  204. }
  205. type dynamicMapKeyEncoder struct {
  206. ctx *ctx
  207. valType reflect2.Type
  208. }
  209. func (encoder *dynamicMapKeyEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  210. obj := encoder.valType.UnsafeIndirect(ptr)
  211. encoderOfMapKey(encoder.ctx, reflect2.TypeOf(obj)).Encode(reflect2.PtrOf(obj), stream)
  212. }
  213. func (encoder *dynamicMapKeyEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  214. obj := encoder.valType.UnsafeIndirect(ptr)
  215. return encoderOfMapKey(encoder.ctx, reflect2.TypeOf(obj)).IsEmpty(reflect2.PtrOf(obj))
  216. }
  217. type mapEncoder struct {
  218. mapType *reflect2.UnsafeMapType
  219. keyEncoder ValEncoder
  220. elemEncoder ValEncoder
  221. }
  222. func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  223. stream.WriteObjectStart()
  224. iter := encoder.mapType.UnsafeIterate(ptr)
  225. for i := 0; iter.HasNext(); i++ {
  226. if i != 0 {
  227. stream.WriteMore()
  228. }
  229. key, elem := iter.UnsafeNext()
  230. encoder.keyEncoder.Encode(key, stream)
  231. if stream.indention > 0 {
  232. stream.writeTwoBytes(byte(':'), byte(' '))
  233. } else {
  234. stream.writeByte(':')
  235. }
  236. encoder.elemEncoder.Encode(elem, stream)
  237. }
  238. stream.WriteObjectEnd()
  239. }
  240. func (encoder *mapEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  241. iter := encoder.mapType.UnsafeIterate(ptr)
  242. return !iter.HasNext()
  243. }
  244. type sortKeysMapEncoder struct {
  245. mapType *reflect2.UnsafeMapType
  246. keyEncoder ValEncoder
  247. elemEncoder ValEncoder
  248. }
  249. func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  250. if *(*unsafe.Pointer)(ptr) == nil {
  251. stream.WriteNil()
  252. return
  253. }
  254. stream.WriteObjectStart()
  255. mapIter := encoder.mapType.UnsafeIterate(ptr)
  256. subStream := stream.cfg.BorrowStream(nil)
  257. subIter := stream.cfg.BorrowIterator(nil)
  258. keyValues := encodedKeyValues{}
  259. for mapIter.HasNext() {
  260. subStream.buf = make([]byte, 0, 64)
  261. key, elem := mapIter.UnsafeNext()
  262. encoder.keyEncoder.Encode(key, subStream)
  263. if subStream.Error != nil && subStream.Error != io.EOF && stream.Error == nil {
  264. stream.Error = subStream.Error
  265. }
  266. encodedKey := subStream.Buffer()
  267. subIter.ResetBytes(encodedKey)
  268. decodedKey := subIter.ReadString()
  269. if stream.indention > 0 {
  270. subStream.writeTwoBytes(byte(':'), byte(' '))
  271. } else {
  272. subStream.writeByte(':')
  273. }
  274. encoder.elemEncoder.Encode(elem, subStream)
  275. keyValues = append(keyValues, encodedKV{
  276. key: decodedKey,
  277. keyValue: subStream.Buffer(),
  278. })
  279. }
  280. sort.Sort(keyValues)
  281. for i, keyValue := range keyValues {
  282. if i != 0 {
  283. stream.WriteMore()
  284. }
  285. stream.Write(keyValue.keyValue)
  286. }
  287. stream.WriteObjectEnd()
  288. stream.cfg.ReturnStream(subStream)
  289. stream.cfg.ReturnIterator(subIter)
  290. }
  291. func (encoder *sortKeysMapEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  292. iter := encoder.mapType.UnsafeIterate(ptr)
  293. return !iter.HasNext()
  294. }
  295. type encodedKeyValues []encodedKV
  296. type encodedKV struct {
  297. key string
  298. keyValue []byte
  299. }
  300. func (sv encodedKeyValues) Len() int { return len(sv) }
  301. func (sv encodedKeyValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
  302. func (sv encodedKeyValues) Less(i, j int) bool { return sv[i].key < sv[j].key }