Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support for byte slice when marshaling Value to JSON #714

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cbor/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions cbor/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down