From 40c89be23a00f19a603d589009629ee451ca138e Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Sat, 21 Sep 2024 08:59:00 -0500 Subject: [PATCH] fix: support for byte slice when marshaling Value to JSON --- cbor/value.go | 2 ++ cbor/value_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/cbor/value.go b/cbor/value.go index e1e0841e..06cda4a3 100644 --- a/cbor/value.go +++ b/cbor/value.go @@ -162,6 +162,8 @@ func (v *Value) processArray(data []byte) error { func generateAstJson(obj interface{}) ([]byte, error) { tmpJsonObj := map[string]interface{}{} switch v := obj.(type) { + case []byte: + tmpJsonObj["bytes"] = hex.EncodeToString(v) case ByteString: tmpJsonObj["bytes"] = hex.EncodeToString(v.Bytes()) case WrappedCbor: diff --git a/cbor/value_test.go b/cbor/value_test.go index 4151f100..81804fc8 100644 --- a/cbor/value_test.go +++ b/cbor/value_test.go @@ -121,6 +121,18 @@ var testDefs = []struct { ), expectedAstJson: `{"map":[{"k":{"int":1},"v":{"int":2}},{"k":{"int":3},"v":{"int":4}}]}`, }, + // 259({2: [h'abcd']}) + { + cborHex: "D90103A1028142ABCD", + expectedObject: cbor.Map( + map[any]any{ + uint64(2): []any{ + []byte{0xab, 0xcd}, + }, + }, + ), + expectedAstJson: `{"map":[{"k":{"int":2},"v":{"list":[{"bytes":"abcd"}]}}]}`, + }, } func TestValueDecode(t *testing.T) {