| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package json // import "go.opentelemetry.io/collector/pdata/internal/json" |
| 5 |
import ( |
| 6 |
"encoding/base64" |
| 7 |
"strconv" |
| 8 |
|
| 9 |
jsoniter "github.com/json-iterator/go" |
| 10 |
) |
| 11 |
|
| 12 |
func BorrowIterator(data []byte) *Iterator { |
| 13 |
return &Iterator{ |
| 14 |
delegate: jsoniter.ConfigFastest.BorrowIterator(data), |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
func ReturnIterator(s *Iterator) { |
| 19 |
jsoniter.ConfigFastest.ReturnIterator(s.delegate) |
| 20 |
} |
| 21 |
|
| 22 |
type Iterator struct { |
| 23 |
delegate *jsoniter.Iterator |
| 24 |
} |
| 25 |
|
| 26 |
// ReadInt32 unmarshalls JSON data into an int32. Accepts both numbers and strings decimal. |
| 27 |
// See https://developers.google.com/protocol-buffers/docs/proto3#json. |
| 28 |
func (iter *Iterator) ReadInt32() int32 { |
| 29 |
switch iter.delegate.WhatIsNext() { |
| 30 |
case jsoniter.NumberValue: |
| 31 |
return iter.delegate.ReadInt32() |
| 32 |
case jsoniter.StringValue: |
| 33 |
val, err := strconv.ParseInt(iter.ReadString(), 10, 32) |
| 34 |
if err != nil { |
| 35 |
iter.ReportError("ReadInt32", err.Error()) |
| 36 |
return 0 |
| 37 |
} |
| 38 |
return int32(val) |
| 39 |
default: |
| 40 |
iter.ReportError("ReadInt32", "unsupported value type") |
| 41 |
return 0 |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// ReadUint32 unmarshalls JSON data into an uint32. Accepts both numbers and strings decimal. |
| 46 |
// See https://developers.google.com/protocol-buffers/docs/proto3#json. |
| 47 |
func (iter *Iterator) ReadUint32() uint32 { |
| 48 |
switch iter.delegate.WhatIsNext() { |
| 49 |
case jsoniter.NumberValue: |
| 50 |
return iter.delegate.ReadUint32() |
| 51 |
case jsoniter.StringValue: |
| 52 |
val, err := strconv.ParseUint(iter.ReadString(), 10, 32) |
| 53 |
if err != nil { |
| 54 |
iter.ReportError("ReadUint32", err.Error()) |
| 55 |
return 0 |
| 56 |
} |
| 57 |
return uint32(val) |
| 58 |
default: |
| 59 |
iter.ReportError("ReadUint32", "unsupported value type") |
| 60 |
return 0 |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// ReadInt64 unmarshalls JSON data into an int64. Accepts both numbers and strings decimal. |
| 65 |
// See https://developers.google.com/protocol-buffers/docs/proto3#json. |
| 66 |
func (iter *Iterator) ReadInt64() int64 { |
| 67 |
switch iter.delegate.WhatIsNext() { |
| 68 |
case jsoniter.NumberValue: |
| 69 |
return iter.delegate.ReadInt64() |
| 70 |
case jsoniter.StringValue: |
| 71 |
val, err := strconv.ParseInt(iter.ReadString(), 10, 64) |
| 72 |
if err != nil { |
| 73 |
iter.ReportError("ReadInt64", err.Error()) |
| 74 |
return 0 |
| 75 |
} |
| 76 |
return val |
| 77 |
default: |
| 78 |
iter.ReportError("ReadInt64", "unsupported value type") |
| 79 |
return 0 |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// ReadUint64 unmarshalls JSON data into an uint64. Accepts both numbers and strings decimal. |
| 84 |
// See https://developers.google.com/protocol-buffers/docs/proto3#json. |
| 85 |
func (iter *Iterator) ReadUint64() uint64 { |
| 86 |
switch iter.delegate.WhatIsNext() { |
| 87 |
case jsoniter.NumberValue: |
| 88 |
return iter.delegate.ReadUint64() |
| 89 |
case jsoniter.StringValue: |
| 90 |
val, err := strconv.ParseUint(iter.ReadString(), 10, 64) |
| 91 |
if err != nil { |
| 92 |
iter.ReportError("ReadUint64", err.Error()) |
| 93 |
return 0 |
| 94 |
} |
| 95 |
return val |
| 96 |
default: |
| 97 |
iter.ReportError("ReadUint64", "unsupported value type") |
| 98 |
return 0 |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func (iter *Iterator) ReadFloat32() float32 { |
| 103 |
switch iter.delegate.WhatIsNext() { |
| 104 |
case jsoniter.NumberValue: |
| 105 |
return iter.delegate.ReadFloat32() |
| 106 |
case jsoniter.StringValue: |
| 107 |
val, err := strconv.ParseFloat(iter.ReadString(), 32) |
| 108 |
if err != nil { |
| 109 |
iter.ReportError("ReadUint64", err.Error()) |
| 110 |
return 0 |
| 111 |
} |
| 112 |
return float32(val) |
| 113 |
default: |
| 114 |
iter.ReportError("ReadUint64", "unsupported value type") |
| 115 |
return 0 |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (iter *Iterator) ReadFloat64() float64 { |
| 120 |
switch iter.delegate.WhatIsNext() { |
| 121 |
case jsoniter.NumberValue: |
| 122 |
return iter.delegate.ReadFloat64() |
| 123 |
case jsoniter.StringValue: |
| 124 |
val, err := strconv.ParseFloat(iter.ReadString(), 64) |
| 125 |
if err != nil { |
| 126 |
iter.ReportError("ReadUint64", err.Error()) |
| 127 |
return 0 |
| 128 |
} |
| 129 |
return val |
| 130 |
default: |
| 131 |
iter.ReportError("ReadUint64", "unsupported value type") |
| 132 |
return 0 |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// ReadBool reads a json object as BoolValue |
| 137 |
func (iter *Iterator) ReadBool() bool { |
| 138 |
return iter.delegate.ReadBool() |
| 139 |
} |
| 140 |
|
| 141 |
// ReadString read string from iterator |
| 142 |
func (iter *Iterator) ReadString() string { |
| 143 |
return iter.delegate.ReadString() |
| 144 |
} |
| 145 |
|
| 146 |
// ReadBytes read base64 encoded bytes from iterator. |
| 147 |
func (iter *Iterator) ReadBytes() []byte { |
| 148 |
buf := iter.ReadStringAsSlice() |
| 149 |
if len(buf) == 0 { |
| 150 |
return nil |
| 151 |
} |
| 152 |
orig := make([]byte, base64.StdEncoding.DecodedLen(len(buf))) |
| 153 |
n, err := base64.StdEncoding.Decode(orig, buf) |
| 154 |
if err != nil { |
| 155 |
iter.ReportError("base64.Decode", err.Error()) |
| 156 |
} |
| 157 |
return orig[:n] |
| 158 |
} |
| 159 |
|
| 160 |
// ReadStringAsSlice read string from iterator without copying into string form. |
| 161 |
// The []byte cannot be kept, as it will change after next iterator call. |
| 162 |
func (iter *Iterator) ReadStringAsSlice() []byte { |
| 163 |
return iter.delegate.ReadStringAsSlice() |
| 164 |
} |
| 165 |
|
| 166 |
// ReportError record a error in iterator instance with current position. |
| 167 |
func (iter *Iterator) ReportError(operation, msg string) { |
| 168 |
iter.delegate.ReportError(operation, msg) |
| 169 |
} |
| 170 |
|
| 171 |
// Error returns any recorded error if any otherwise it returns nil. |
| 172 |
func (iter *Iterator) Error() error { |
| 173 |
return iter.delegate.Error |
| 174 |
} |
| 175 |
|
| 176 |
// Skip skips a json object and positions to relatively the next json object |
| 177 |
func (iter *Iterator) Skip() { |
| 178 |
iter.delegate.Skip() |
| 179 |
} |
| 180 |
|
| 181 |
// ReadArray read array element, returns true if the array has more element to read. |
| 182 |
func (iter *Iterator) ReadArray() bool { |
| 183 |
return iter.delegate.ReadArray() |
| 184 |
} |
| 185 |
|
| 186 |
// ReadObject read one field from object. |
| 187 |
// If object ended, returns empty string. Otherwise, returns the field name. |
| 188 |
func (iter *Iterator) ReadObject() string { |
| 189 |
return iter.delegate.ReadObject() |
| 190 |
} |
| 191 |
|
| 192 |
// ReadEnumValue returns the enum integer value representation. Accepts both enum names and enum integer values. |
| 193 |
// See https://developers.google.com/protocol-buffers/docs/proto3#json. |
| 194 |
func (iter *Iterator) ReadEnumValue(valueMap map[string]int32) int32 { |
| 195 |
switch iter.delegate.WhatIsNext() { |
| 196 |
case jsoniter.NumberValue: |
| 197 |
return iter.ReadInt32() |
| 198 |
case jsoniter.StringValue: |
| 199 |
val, ok := valueMap[iter.ReadString()] |
| 200 |
// Same behavior with official protobuf JSON decoder, |
| 201 |
// see https://github.com/open-telemetry/opentelemetry-proto-go/pull/81 |
| 202 |
if !ok { |
| 203 |
iter.ReportError("ReadEnumValue", "unknown string value") |
| 204 |
return 0 |
| 205 |
} |
| 206 |
return val |
| 207 |
default: |
| 208 |
iter.ReportError("ReadEnumValue", "unsupported value type") |
| 209 |
return 0 |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
// ResetBytes reuse iterator instance by specifying another byte array as input |
| 214 |
func (iter *Iterator) ResetBytes(input []byte) *Iterator { |
| 215 |
iter.delegate.ResetBytes(input) |
| 216 |
return iter |
| 217 |
} |