...
1 package jsoniter
2
3 import (
4 "encoding/json"
5 "github.com/modern-go/reflect2"
6 "unsafe"
7 )
8
9 var jsonRawMessageType = reflect2.TypeOfPtr((*json.RawMessage)(nil)).Elem()
10 var jsoniterRawMessageType = reflect2.TypeOfPtr((*RawMessage)(nil)).Elem()
11
12 func createEncoderOfJsonRawMessage(ctx *ctx, typ reflect2.Type) ValEncoder {
13 if typ == jsonRawMessageType {
14 return &jsonRawMessageCodec{}
15 }
16 if typ == jsoniterRawMessageType {
17 return &jsoniterRawMessageCodec{}
18 }
19 return nil
20 }
21
22 func createDecoderOfJsonRawMessage(ctx *ctx, typ reflect2.Type) ValDecoder {
23 if typ == jsonRawMessageType {
24 return &jsonRawMessageCodec{}
25 }
26 if typ == jsoniterRawMessageType {
27 return &jsoniterRawMessageCodec{}
28 }
29 return nil
30 }
31
32 type jsonRawMessageCodec struct {
33 }
34
35 func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
36 if iter.ReadNil() {
37 *((*json.RawMessage)(ptr)) = nil
38 } else {
39 *((*json.RawMessage)(ptr)) = iter.SkipAndReturnBytes()
40 }
41 }
42
43 func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
44 if *((*json.RawMessage)(ptr)) == nil {
45 stream.WriteNil()
46 } else {
47 stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
48 }
49 }
50
51 func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
52 return len(*((*json.RawMessage)(ptr))) == 0
53 }
54
55 type jsoniterRawMessageCodec struct {
56 }
57
58 func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
59 if iter.ReadNil() {
60 *((*RawMessage)(ptr)) = nil
61 } else {
62 *((*RawMessage)(ptr)) = iter.SkipAndReturnBytes()
63 }
64 }
65
66 func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
67 if *((*RawMessage)(ptr)) == nil {
68 stream.WriteNil()
69 } else {
70 stream.WriteRaw(string(*((*RawMessage)(ptr))))
71 }
72 }
73
74 func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
75 return len(*((*RawMessage)(ptr))) == 0
76 }
77
View as plain text