Debian Codegraph @

pdata/xpdata/json.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package xpdata // import "go.opentelemetry.io/collector/pdata/xpdata"
5
6 import (
7 "slices"
8
9 "go.opentelemetry.io/collector/pdata/internal"
10 "go.opentelemetry.io/collector/pdata/internal/json"
11 "go.opentelemetry.io/collector/pdata/pcommon"
12 )
13
14 type JSONMarshaler struct{}
15
16 func (*JSONMarshaler) MarshalValue(value pcommon.Value) ([]byte, error) {
17 av := internal.GetValueOrig(internal.ValueWrapper(value))
18 dest := json.BorrowStream(nil)
19 defer json.ReturnStream(dest)
20 av.MarshalJSON(dest)
21 if dest.Error() != nil {
22 return nil, dest.Error()
23 }
24 return slices.Clone(dest.Buffer()), nil
25 }
26
27 type JSONUnmarshaler struct{}
28
29 func (*JSONUnmarshaler) UnmarshalValue(buf []byte) (pcommon.Value, error) {
30 iter := json.BorrowIterator(buf)
31 defer json.ReturnIterator(iter)
32 value := &internal.AnyValue{}
33 value.UnmarshalJSON(iter)
34 if iter.Error() != nil {
35 return pcommon.NewValueEmpty(), iter.Error()
36 }
37 return pcommon.Value(internal.NewValueWrapper(value, internal.NewState())), nil
38 }