smartcontract: update (Parameter).MarshalJSON method

MarshalJSON should be defined on structure (not pointer), as we use
structures to marshal parameters (e.g. in NotificationEvent and
Invoke of RPC result package) and never use pointers for that purpose.

Also added marshalling of nil array into `[]` instead of `null` to
follow C# implementation.
This commit is contained in:
Anna Shaleva 2020-06-23 16:11:49 +03:00
parent 8de0332107
commit 9097a1a23d
3 changed files with 25 additions and 3 deletions

View file

@ -1,9 +1,11 @@
package native package native
import ( import (
"encoding/json"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -51,3 +53,12 @@ func TestToStackItem(t *testing.T) {
actual = stackitem.NewArray([]stackitem.Item{}) actual = stackitem.NewArray([]stackitem.Item{})
require.Equal(t, expected.ToStackItem(), actual) require.Equal(t, expected.ToStackItem(), actual)
} }
func TestMarshallJSON(t *testing.T) {
ba := &BlockedAccounts{}
p := smartcontract.ParameterFromStackItem(ba.ToStackItem(), make(map[stackitem.Item]bool))
actual, err := json.Marshal(p)
require.NoError(t, err)
expected := `{"type":"Array","value":[]}`
require.Equal(t, expected, string(actual))
}

View file

@ -57,7 +57,7 @@ type rawParameter struct {
} }
// MarshalJSON implements Marshaler interface. // MarshalJSON implements Marshaler interface.
func (p *Parameter) MarshalJSON() ([]byte, error) { func (p Parameter) MarshalJSON() ([]byte, error) {
var ( var (
resultRawValue json.RawMessage resultRawValue json.RawMessage
resultErr error resultErr error
@ -83,7 +83,11 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
} }
case ArrayType: case ArrayType:
var value = p.Value.([]Parameter) var value = p.Value.([]Parameter)
resultRawValue, resultErr = json.Marshal(value) if value == nil {
resultRawValue, resultErr = json.Marshal([]Parameter{})
} else {
resultRawValue, resultErr = json.Marshal(value)
}
case MapType: case MapType:
ppair := p.Value.([]ParameterPair) ppair := p.Value.([]ParameterPair)
resultRawValue, resultErr = json.Marshal(ppair) resultRawValue, resultErr = json.Marshal(ppair)

View file

@ -131,6 +131,13 @@ var marshalJSONTestCases = []struct {
}, },
result: `{"type":"InteropInterface","value":null}`, result: `{"type":"InteropInterface","value":null}`,
}, },
{
input: Parameter{
Type: ArrayType,
Value: []Parameter{},
},
result: `{"type":"Array","value":[]}`,
},
} }
var marshalJSONErrorCases = []Parameter{ var marshalJSONErrorCases = []Parameter{
@ -146,7 +153,7 @@ var marshalJSONErrorCases = []Parameter{
func TestParam_MarshalJSON(t *testing.T) { func TestParam_MarshalJSON(t *testing.T) {
for _, tc := range marshalJSONTestCases { for _, tc := range marshalJSONTestCases {
res, err := json.Marshal(&tc.input) res, err := json.Marshal(tc.input)
assert.NoError(t, err) assert.NoError(t, err)
var actual, expected Parameter var actual, expected Parameter
assert.NoError(t, json.Unmarshal(res, &actual)) assert.NoError(t, json.Unmarshal(res, &actual))