mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-06 19:55:10 +00:00
Merge pull request #1242 from nspcc-dev/fix/json
Implement lossless stackitem to json conversion
This commit is contained in:
commit
fa5ff8dd95
27 changed files with 562 additions and 208 deletions
|
@ -6,7 +6,6 @@ import (
|
|||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
)
|
||||
|
||||
|
@ -406,18 +405,24 @@ func (s *Stack) PopSigElements() ([][]byte, error) {
|
|||
return elems, nil
|
||||
}
|
||||
|
||||
// ToContractParameters converts Stack to slice of smartcontract.Parameter.
|
||||
func (s *Stack) ToContractParameters() []smartcontract.Parameter {
|
||||
items := make([]smartcontract.Parameter, 0, s.Len())
|
||||
// ToArray converts stack to an array of stackitems with top item being the last.
|
||||
func (s *Stack) ToArray() []stackitem.Item {
|
||||
items := make([]stackitem.Item, 0, s.len)
|
||||
s.IterBack(func(e *Element) {
|
||||
// Each item is independent.
|
||||
seen := make(map[stackitem.Item]bool)
|
||||
items = append(items, smartcontract.ParameterFromStackItem(e.value, seen))
|
||||
items = append(items, e.Item())
|
||||
})
|
||||
return items
|
||||
}
|
||||
|
||||
// MarshalJSON implements JSON marshalling interface.
|
||||
func (s *Stack) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.ToContractParameters())
|
||||
items := s.ToArray()
|
||||
arr := make([]json.RawMessage, len(items))
|
||||
for i := range items {
|
||||
data, err := stackitem.ToJSONWithTypes(items[i])
|
||||
if err == nil {
|
||||
arr[i] = data
|
||||
}
|
||||
}
|
||||
return json.Marshal(arr)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue