Skip to content

Commit a980b0c

Browse files
authored
fix: support for byte slice when marshaling Value to JSON (#714)
1 parent bbe4618 commit a980b0c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

cbor/value.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ func (v *Value) processArray(data []byte) error {
162162
func generateAstJson(obj interface{}) ([]byte, error) {
163163
tmpJsonObj := map[string]interface{}{}
164164
switch v := obj.(type) {
165+
case []byte:
166+
tmpJsonObj["bytes"] = hex.EncodeToString(v)
165167
case ByteString:
166168
tmpJsonObj["bytes"] = hex.EncodeToString(v.Bytes())
167169
case WrappedCbor:

cbor/value_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ var testDefs = []struct {
121121
),
122122
expectedAstJson: `{"map":[{"k":{"int":1},"v":{"int":2}},{"k":{"int":3},"v":{"int":4}}]}`,
123123
},
124+
// 259({2: [h'abcd']})
125+
{
126+
cborHex: "D90103A1028142ABCD",
127+
expectedObject: cbor.Map(
128+
map[any]any{
129+
uint64(2): []any{
130+
[]byte{0xab, 0xcd},
131+
},
132+
},
133+
),
134+
expectedAstJson: `{"map":[{"k":{"int":2},"v":{"list":[{"bytes":"abcd"}]}}]}`,
135+
},
124136
}
125137

126138
func TestValueDecode(t *testing.T) {

0 commit comments

Comments
 (0)