vm: add MarshalJSON to the Stack

To easily dump it in a known format.
This commit is contained in:
Roman Khimov 2019-10-29 18:26:59 +03:00
parent 47f66dfbf3
commit 94776b8a1f
2 changed files with 11 additions and 2 deletions

View file

@ -9,7 +9,7 @@ type stackItem struct {
Type string `json:"type"`
}
func buildStackOutput(s *Stack) string {
func stackToArray(s *Stack) []stackItem {
items := make([]stackItem, 0, s.Len())
s.Iter(func(e *Element) {
items = append(items, stackItem{
@ -17,7 +17,10 @@ func buildStackOutput(s *Stack) string {
Type: e.value.String(),
})
})
return items
}
b, _ := json.MarshalIndent(items, "", " ")
func buildStackOutput(s *Stack) string {
b, _ := json.MarshalIndent(stackToArray(s), "", " ")
return string(b)
}

View file

@ -1,6 +1,7 @@
package vm
import (
"encoding/json"
"fmt"
"math/big"
@ -334,3 +335,8 @@ func (s *Stack) popSigElements() ([][]byte, error) {
}
return elems, nil
}
// MarshalJSON implements JSON marshalling interface.
func (s *Stack) MarshalJSON() ([]byte, error) {
return json.Marshal(stackToArray(s))
}