1
0
mirror of https://github.com/json-iterator/go.git synced 2024-12-03 08:45:22 +02:00
json-iterator/reflect_json_raw_message.go

77 lines
1.8 KiB
Go
Raw Normal View History

2018-02-20 16:38:35 +02:00
package jsoniter
import (
"encoding/json"
2018-02-28 11:09:30 +02:00
"github.com/modern-go/reflect2"
2018-02-24 16:04:41 +02:00
"unsafe"
2018-02-20 16:38:35 +02:00
)
2018-02-22 04:12:08 +02:00
var jsonRawMessageType = reflect2.TypeOfPtr((*json.RawMessage)(nil)).Elem()
var jsoniterRawMessageType = reflect2.TypeOfPtr((*RawMessage)(nil)).Elem()
2018-02-22 04:12:08 +02:00
func createEncoderOfJsonRawMessage(ctx *ctx, typ reflect2.Type) ValEncoder {
2018-02-20 16:38:35 +02:00
if typ == jsonRawMessageType {
return &jsonRawMessageCodec{}
}
if typ == jsoniterRawMessageType {
return &jsoniterRawMessageCodec{}
}
return nil
}
2018-02-22 04:12:08 +02:00
func createDecoderOfJsonRawMessage(ctx *ctx, typ reflect2.Type) ValDecoder {
2018-02-20 16:38:35 +02:00
if typ == jsonRawMessageType {
return &jsonRawMessageCodec{}
}
if typ == jsoniterRawMessageType {
return &jsoniterRawMessageCodec{}
}
return nil
}
type jsonRawMessageCodec struct {
}
func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
2020-07-21 11:07:23 +02:00
if iter.ReadNil() {
*((*json.RawMessage)(ptr)) = nil
} else {
*((*json.RawMessage)(ptr)) = iter.SkipAndReturnBytes()
}
2018-02-20 16:38:35 +02:00
}
func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2020-07-21 11:07:23 +02:00
if *((*json.RawMessage)(ptr)) == nil {
stream.WriteNil()
} else {
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
}
2018-02-20 16:38:35 +02:00
}
func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
return len(*((*json.RawMessage)(ptr))) == 0
}
type jsoniterRawMessageCodec struct {
}
func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
2020-07-21 11:07:23 +02:00
if iter.ReadNil() {
*((*RawMessage)(ptr)) = nil
} else {
*((*RawMessage)(ptr)) = iter.SkipAndReturnBytes()
}
2018-02-20 16:38:35 +02:00
}
func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2020-07-21 11:07:23 +02:00
if *((*RawMessage)(ptr)) == nil {
stream.WriteNil()
} else {
stream.WriteRaw(string(*((*RawMessage)(ptr))))
}
2018-02-20 16:38:35 +02:00
}
func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
return len(*((*RawMessage)(ptr))) == 0
2018-02-24 16:04:41 +02:00
}