mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
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:
parent
8de0332107
commit
9097a1a23d
3 changed files with 25 additions and 3 deletions
|
@ -1,9 +1,11 @@
|
|||
package native
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"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/vm/stackitem"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -51,3 +53,12 @@ func TestToStackItem(t *testing.T) {
|
|||
actual = stackitem.NewArray([]stackitem.Item{})
|
||||
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))
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ type rawParameter struct {
|
|||
}
|
||||
|
||||
// MarshalJSON implements Marshaler interface.
|
||||
func (p *Parameter) MarshalJSON() ([]byte, error) {
|
||||
func (p Parameter) MarshalJSON() ([]byte, error) {
|
||||
var (
|
||||
resultRawValue json.RawMessage
|
||||
resultErr error
|
||||
|
@ -83,7 +83,11 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
case ArrayType:
|
||||
var value = p.Value.([]Parameter)
|
||||
if value == nil {
|
||||
resultRawValue, resultErr = json.Marshal([]Parameter{})
|
||||
} else {
|
||||
resultRawValue, resultErr = json.Marshal(value)
|
||||
}
|
||||
case MapType:
|
||||
ppair := p.Value.([]ParameterPair)
|
||||
resultRawValue, resultErr = json.Marshal(ppair)
|
||||
|
|
|
@ -131,6 +131,13 @@ var marshalJSONTestCases = []struct {
|
|||
},
|
||||
result: `{"type":"InteropInterface","value":null}`,
|
||||
},
|
||||
{
|
||||
input: Parameter{
|
||||
Type: ArrayType,
|
||||
Value: []Parameter{},
|
||||
},
|
||||
result: `{"type":"Array","value":[]}`,
|
||||
},
|
||||
}
|
||||
|
||||
var marshalJSONErrorCases = []Parameter{
|
||||
|
@ -146,7 +153,7 @@ var marshalJSONErrorCases = []Parameter{
|
|||
|
||||
func TestParam_MarshalJSON(t *testing.T) {
|
||||
for _, tc := range marshalJSONTestCases {
|
||||
res, err := json.Marshal(&tc.input)
|
||||
res, err := json.Marshal(tc.input)
|
||||
assert.NoError(t, err)
|
||||
var actual, expected Parameter
|
||||
assert.NoError(t, json.Unmarshal(res, &actual))
|
||||
|
|
Loading…
Reference in a new issue