| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package json |
| 18 |
|
| 19 |
import ( |
| 20 |
gojson "encoding/json" |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
type UnmarshalTypeError = gojson.UnmarshalTypeError |
| 28 |
type UnmarshalFieldError = gojson.UnmarshalFieldError |
| 29 |
type InvalidUnmarshalError = gojson.InvalidUnmarshalError |
| 30 |
type Number = gojson.Number |
| 31 |
type RawMessage = gojson.RawMessage |
| 32 |
type Token = gojson.Token |
| 33 |
type Delim = gojson.Delim |
| 34 |
|
| 35 |
type UnmarshalOpt func(*decodeState) |
| 36 |
|
| 37 |
func UseNumber(d *decodeState) { |
| 38 |
d.useNumber = true |
| 39 |
} |
| 40 |
func DisallowUnknownFields(d *decodeState) { |
| 41 |
d.disallowUnknownFields = true |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func CaseSensitive(d *decodeState) { |
| 47 |
d.caseSensitive = true |
| 48 |
} |
| 49 |
func (d *Decoder) CaseSensitive() { |
| 50 |
d.d.caseSensitive = true |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func PreserveInts(d *decodeState) { |
| 59 |
d.preserveInts = true |
| 60 |
} |
| 61 |
func (d *Decoder) PreserveInts() { |
| 62 |
d.d.preserveInts = true |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func DisallowDuplicateFields(d *decodeState) { |
| 67 |
d.disallowDuplicateFields = true |
| 68 |
} |
| 69 |
func (d *Decoder) DisallowDuplicateFields() { |
| 70 |
d.d.disallowDuplicateFields = true |
| 71 |
} |
| 72 |
|
| 73 |
func (d *decodeState) newFieldError(errType strictErrType, field string) *strictError { |
| 74 |
if len(d.strictFieldStack) > 0 { |
| 75 |
return &strictError{ |
| 76 |
ErrType: errType, |
| 77 |
Path: strings.Join(d.strictFieldStack, "") + "." + field, |
| 78 |
} |
| 79 |
} else { |
| 80 |
return &strictError{ |
| 81 |
ErrType: errType, |
| 82 |
Path: field, |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
func (d *decodeState) saveStrictError(err *strictError) { |
| 90 |
|
| 91 |
if len(d.savedStrictErrors) >= 100 { |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
if d.seenStrictErrors == nil { |
| 96 |
d.seenStrictErrors = map[strictError]struct{}{} |
| 97 |
} |
| 98 |
if _, seen := d.seenStrictErrors[*err]; seen { |
| 99 |
return |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
d.seenStrictErrors[*err] = struct{}{} |
| 104 |
d.savedStrictErrors = append(d.savedStrictErrors, err) |
| 105 |
} |
| 106 |
|
| 107 |
func (d *decodeState) appendStrictFieldStackKey(key string) { |
| 108 |
if !d.disallowDuplicateFields && !d.disallowUnknownFields { |
| 109 |
return |
| 110 |
} |
| 111 |
if len(d.strictFieldStack) > 0 { |
| 112 |
d.strictFieldStack = append(d.strictFieldStack, ".", key) |
| 113 |
} else { |
| 114 |
d.strictFieldStack = append(d.strictFieldStack, key) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func (d *decodeState) appendStrictFieldStackIndex(i int) { |
| 119 |
if !d.disallowDuplicateFields && !d.disallowUnknownFields { |
| 120 |
return |
| 121 |
} |
| 122 |
d.strictFieldStack = append(d.strictFieldStack, "[", strconv.Itoa(i), "]") |
| 123 |
} |
| 124 |
|
| 125 |
type strictErrType string |
| 126 |
|
| 127 |
const ( |
| 128 |
unknownStrictErrType strictErrType = "unknown field" |
| 129 |
duplicateStrictErrType strictErrType = "duplicate field" |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
type strictError struct { |
| 136 |
ErrType strictErrType |
| 137 |
Path string |
| 138 |
} |
| 139 |
|
| 140 |
func (e *strictError) Error() string { |
| 141 |
return string(e.ErrType) + " " + strconv.Quote(e.Path) |
| 142 |
} |
| 143 |
|
| 144 |
func (e *strictError) FieldPath() string { |
| 145 |
return e.Path |
| 146 |
} |
| 147 |
|
| 148 |
func (e *strictError) SetFieldPath(path string) { |
| 149 |
e.Path = path |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
type UnmarshalStrictError struct { |
| 155 |
Errors []error |
| 156 |
} |
| 157 |
|
| 158 |
func (e *UnmarshalStrictError) Error() string { |
| 159 |
var b strings.Builder |
| 160 |
b.WriteString("json: ") |
| 161 |
for i, err := range e.Errors { |
| 162 |
if i > 0 { |
| 163 |
b.WriteString(", ") |
| 164 |
} |
| 165 |
b.WriteString(err.Error()) |
| 166 |
} |
| 167 |
return b.String() |
| 168 |
} |