aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/json-iterator/go/reflect_json_number.go
blob: 98d45c1ec25500f9d7f7c944286e26bbc5d321ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package jsoniter

import (
	"encoding/json"
	"github.com/modern-go/reflect2"
	"strconv"
	"unsafe"
)

type Number string

// String returns the literal text of the number.
func (n Number) String() string { return string(n) }

// Float64 returns the number as a float64.
func (n Number) Float64() (float64, error) {
	return strconv.ParseFloat(string(n), 64)
}

// Int64 returns the number as an int64.
func (n Number) Int64() (int64, error) {
	return strconv.ParseInt(string(n), 10, 64)
}

func CastJsonNumber(val interface{}) (string, bool) {
	switch typedVal := val.(type) {
	case json.Number:
		return string(typedVal), true
	case Number:
		return string(typedVal), true
	}
	return "", false
}

var jsonNumberType = reflect2.TypeOfPtr((*json.Number)(nil)).Elem()
var jsoniterNumberType = reflect2.TypeOfPtr((*Number)(nil)).Elem()

func createDecoderOfJsonNumber(ctx *ctx, typ reflect2.Type) ValDecoder {
	if typ.AssignableTo(jsonNumberType) {
		return &jsonNumberCodec{}
	}
	if typ.AssignableTo(jsoniterNumberType) {
		return &jsoniterNumberCodec{}
	}
	return nil
}

func createEncoderOfJsonNumber(ctx *ctx, typ reflect2.Type) ValEncoder {
	if typ.AssignableTo(jsonNumberType) {
		return &jsonNumberCodec{}
	}
	if typ.AssignableTo(jsoniterNumberType) {
		return &jsoniterNumberCodec{}
	}
	return nil
}

type jsonNumberCodec struct {
}

func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
	switch iter.WhatIsNext() {
	case StringValue:
		*((*json.Number)(ptr)) = json.Number(iter.ReadString())
	case NilValue:
		iter.skipFourBytes('n', 'u', 'l', 'l')
		*((*json.Number)(ptr)) = ""
	default:
		*((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
	}
}

func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
	number := *((*json.Number)(ptr))
	if len(number) == 0 {
		stream.writeByte('0')
	} else {
		stream.WriteRaw(string(number))
	}
}

func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
	return len(*((*json.Number)(ptr))) == 0
}

type jsoniterNumberCodec struct {
}

func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
	switch iter.WhatIsNext() {
	case StringValue:
		*((*Number)(ptr)) = Number(iter.ReadString())
	case NilValue:
		iter.skipFourBytes('n', 'u', 'l', 'l')
		*((*Number)(ptr)) = ""
	default:
		*((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
	}
}

func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
	number := *((*Number)(ptr))
	if len(number) == 0 {
		stream.writeByte('0')
	} else {
		stream.WriteRaw(string(number))
	}
}

func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
	return len(*((*Number)(ptr))) == 0
}