nef: support JSON serialization
This commit is contained in:
parent
ca86b78536
commit
11191c0a08
2 changed files with 34 additions and 6 deletions
|
@ -36,16 +36,16 @@ const (
|
|||
|
||||
// File represents compiled contract file structure according to the NEF3 standard.
|
||||
type File struct {
|
||||
Header Header
|
||||
Script []byte
|
||||
Checksum uint32
|
||||
Header
|
||||
Script []byte `json:"script"`
|
||||
Checksum uint32 `json:"checksum"`
|
||||
}
|
||||
|
||||
// Header represents File header.
|
||||
type Header struct {
|
||||
Magic uint32
|
||||
Compiler string
|
||||
Version string
|
||||
Magic uint32 `json:"magic"`
|
||||
Compiler string `json:"compiler"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// NewFile returns new NEF3 file with script specified.
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package nef
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
||||
|
@ -74,3 +77,28 @@ func TestBytesFromBytes(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestMarshalUnmarshalJSON(t *testing.T) {
|
||||
expected := &File{
|
||||
Header: Header{
|
||||
Magic: Magic,
|
||||
Compiler: "test.compiler",
|
||||
Version: "test.ver",
|
||||
},
|
||||
Script: []byte{1, 2, 3, 4},
|
||||
}
|
||||
expected.Checksum = expected.CalculateChecksum()
|
||||
|
||||
data, err := json.Marshal(expected)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, `{
|
||||
"magic":`+strconv.FormatUint(uint64(Magic), 10)+`,
|
||||
"compiler": "test.compiler",
|
||||
"version": "test.ver",
|
||||
"script": "`+base64.StdEncoding.EncodeToString(expected.Script)+`",
|
||||
"checksum":`+strconv.FormatUint(uint64(expected.Checksum), 10)+`}`, string(data))
|
||||
|
||||
actual := new(File)
|
||||
require.NoError(t, json.Unmarshal(data, actual))
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue