Skip to content

Commit

Permalink
Support unmarshalling of json byte slices (#3354)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Sep 3, 2024
1 parent 9cbcc7c commit 833b3a0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
16 changes: 8 additions & 8 deletions vms/avm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func TestServiceGetTxJSON_ExportTx(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"destinationChain": "2mcwQKiD8VEspmMJpL1dc7okQQ5dDVAWeCBZ7FWBFAbxpv3t7w",
"exportedOutputs": [
{
Expand Down Expand Up @@ -800,7 +800,7 @@ func TestServiceGetTxJSON_CreateAssetTx(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"name": "Team Rocket",
"symbol": "TR",
"denomination": 0,
Expand Down Expand Up @@ -972,7 +972,7 @@ func TestServiceGetTxJSON_OperationTxWithNftxMintOp(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"operations": [
{
"assetID": %[4]q,
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func TestServiceGetTxJSON_OperationTxWithMultipleNftxMintOp(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"operations": [
{
"assetID": %[4]q,
Expand Down Expand Up @@ -1292,7 +1292,7 @@ func TestServiceGetTxJSON_OperationTxWithSecpMintOp(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"operations": [
{
"assetID": %[4]q,
Expand Down Expand Up @@ -1439,7 +1439,7 @@ func TestServiceGetTxJSON_OperationTxWithMultipleSecpMintOp(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"operations": [
{
"assetID": %[4]q,
Expand Down Expand Up @@ -1617,7 +1617,7 @@ func TestServiceGetTxJSON_OperationTxWithPropertyFxMintOp(t *testing.T) {
}
}
],
"memo": "0x",
"memo": null,
"operations": [
{
"assetID": %[4]q,
Expand Down Expand Up @@ -1759,7 +1759,7 @@ func TestServiceGetTxJSON_OperationTxWithPropertyFxMintOpMultiple(t *testing.T)
}
}
],
"memo": "0x",
"memo": null,
"operations": [
{
"assetID": %[4]q,
Expand Down
4 changes: 4 additions & 0 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func TestGetTx(t *testing.T) {
constants.AVMID,
[]ids.ID{},
"chain name",
common.WithMemo([]byte{}),
)
require.NoError(t, err)
return tx
Expand Down Expand Up @@ -266,6 +267,7 @@ func TestGetTx(t *testing.T) {
rewardsOwner,
rewardsOwner,
0,
common.WithMemo([]byte{}),
)
require.NoError(t, err)
return tx
Expand All @@ -289,6 +291,7 @@ func TestGetTx(t *testing.T) {
},
},
}},
common.WithMemo([]byte{}),
)
require.NoError(t, err)
return tx
Expand Down Expand Up @@ -792,6 +795,7 @@ func TestGetBlock(t *testing.T) {
constants.AVMID,
[]ids.ID{},
"chain name",
common.WithMemo([]byte{}),
)
require.NoError(err)

Expand Down
23 changes: 23 additions & 0 deletions vms/types/blob_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,36 @@ import (
"github.com/ava-labs/avalanchego/utils/formatting"
)

const nullStr = "null"

// JSONByteSlice represents [[]byte] that is json marshalled to hex
type JSONByteSlice []byte

func (b JSONByteSlice) MarshalJSON() ([]byte, error) {
if b == nil {
return []byte(nullStr), nil
}

hexData, err := formatting.Encode(formatting.HexNC, b)
if err != nil {
return nil, err
}
return json.Marshal(hexData)
}

func (b *JSONByteSlice) UnmarshalJSON(jsonBytes []byte) error {
if string(jsonBytes) == nullStr {
return nil
}

var hexData string
if err := json.Unmarshal(jsonBytes, &hexData); err != nil {
return err
}
v, err := formatting.Decode(formatting.HexNC, hexData)
if err != nil {
return err
}
*b = v
return nil
}
56 changes: 56 additions & 0 deletions vms/types/blob_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package types

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestJSON(t *testing.T) {
tests := []struct {
name string
value JSONByteSlice
expectedJSON string
}{
{
name: "nil",
value: nil,
expectedJSON: nullStr,
},
{
name: "empty",
value: []byte{},
expectedJSON: `"0x"`,
},
{
name: "not empty",
value: []byte{0, 1, 2, 3},
expectedJSON: `"0x00010203"`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

jsonBytes, err := json.Marshal(test.value)
require.NoError(err)
require.Equal(test.expectedJSON, string(jsonBytes))

var unmarshaled JSONByteSlice
require.NoError(json.Unmarshal(jsonBytes, &unmarshaled))
require.Equal(test.value, unmarshaled)
})
}
}

func TestUnmarshalJSONNullKeepsInitialValue(t *testing.T) {
require := require.New(t)

unmarshaled := JSONByteSlice{1, 2, 3}
require.NoError(json.Unmarshal([]byte(nullStr), &unmarshaled))
require.Equal(JSONByteSlice{1, 2, 3}, unmarshaled)
}

0 comments on commit 833b3a0

Please sign in to comment.