| 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 |
|
| 6 |
import ( |
| 7 |
"encoding/base64" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"math" |
| 11 |
"strconv" |
| 12 |
|
| 13 |
jsoniter "github.com/json-iterator/go" |
| 14 |
) |
| 15 |
|
| 16 |
func BorrowStream(writer io.Writer) *Stream { |
| 17 |
return &Stream{ |
| 18 |
Stream: jsoniter.ConfigFastest.BorrowStream(writer), |
| 19 |
wmTracker: make([]bool, 32), |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func ReturnStream(s *Stream) { |
| 24 |
jsoniter.ConfigFastest.ReturnStream(s.Stream) |
| 25 |
} |
| 26 |
|
| 27 |
// Stream avoids the need to explicitly call the `Stream.WriteMore` method while marshaling objects by |
| 28 |
// checking if a field was previously written inside the current object and automatically appending a "," |
| 29 |
// if so before writing the next field. |
| 30 |
type Stream struct { |
| 31 |
*jsoniter.Stream |
| 32 |
// wmTracker acts like a stack which pushes a new value when an object is started and removes the |
| 33 |
// top when it is ended. The value added for every object tracks if there is any written field |
| 34 |
// already for that object, and if it is then automatically add a "," before any new field. |
| 35 |
wmTracker []bool |
| 36 |
} |
| 37 |
|
| 38 |
func (ots *Stream) WriteObjectStart() { |
| 39 |
ots.Stream.WriteObjectStart() |
| 40 |
ots.wmTracker = append(ots.wmTracker, false) |
| 41 |
} |
| 42 |
|
| 43 |
func (ots *Stream) WriteObjectField(field string) { |
| 44 |
if ots.wmTracker[len(ots.wmTracker)-1] { |
| 45 |
ots.WriteMore() |
| 46 |
} |
| 47 |
|
| 48 |
ots.Stream.WriteObjectField(field) |
| 49 |
ots.wmTracker[len(ots.wmTracker)-1] = true |
| 50 |
} |
| 51 |
|
| 52 |
func (ots *Stream) WriteObjectEnd() { |
| 53 |
ots.Stream.WriteObjectEnd() |
| 54 |
ots.wmTracker = ots.wmTracker[:len(ots.wmTracker)-1] |
| 55 |
} |
| 56 |
|
| 57 |
// WriteInt64 writes the values as a decimal string. This is per the protobuf encoding rules for int64, fixed64, uint64. |
| 58 |
func (ots *Stream) WriteInt64(val int64) { |
| 59 |
ots.WriteString(strconv.FormatInt(val, 10)) |
| 60 |
} |
| 61 |
|
| 62 |
// WriteUint64 writes the values as a decimal string. This is per the protobuf encoding rules for int64, fixed64, uint64. |
| 63 |
func (ots *Stream) WriteUint64(val uint64) { |
| 64 |
ots.WriteString(strconv.FormatUint(val, 10)) |
| 65 |
} |
| 66 |
|
| 67 |
// WriteBytes writes the values as a base64 encoded string. This is per the protobuf encoding rules for bytes. |
| 68 |
func (ots *Stream) WriteBytes(val []byte) { |
| 69 |
if len(val) == 0 { |
| 70 |
ots.WriteString("") |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
ots.WriteString(base64.StdEncoding.EncodeToString(val)) |
| 75 |
} |
| 76 |
|
| 77 |
// WriteFloat64 writes the JSON value that will be a number or one of the special string |
| 78 |
// values "NaN", "Infinity", and "-Infinity". Either numbers or strings are accepted. |
| 79 |
// Empty strings are invalid. Exponent notation is also accepted. |
| 80 |
// See https://protobuf.dev/programming-guides/json/. |
| 81 |
func (ots *Stream) WriteFloat64(val float64) { |
| 82 |
if math.IsNaN(val) { |
| 83 |
ots.WriteString("NaN") |
| 84 |
return |
| 85 |
} |
| 86 |
if math.IsInf(val, 1) { |
| 87 |
ots.WriteString("Infinity") |
| 88 |
return |
| 89 |
} |
| 90 |
if math.IsInf(val, -1) { |
| 91 |
ots.WriteString("-Infinity") |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
ots.Stream.WriteFloat64(val) |
| 96 |
} |
| 97 |
|
| 98 |
func (ots *Stream) ReportError(err error) { |
| 99 |
ots.Stream.Error = errors.Join(ots.Stream.Error, err) |
| 100 |
} |
| 101 |
|
| 102 |
func (ots *Stream) Error() error { |
| 103 |
return ots.Stream.Error |
| 104 |
} |