forked from TrueCloudLab/neoneo-go
Merge pull request #1609 from nspcc-dev/fix/json
stackitem: fix JSON encoding
This commit is contained in:
commit
742c15cf0b
2 changed files with 14 additions and 19 deletions
|
@ -88,12 +88,16 @@ func toJSON(buf *io.BufBinWriter, item Item) {
|
|||
return
|
||||
}
|
||||
w.WriteBytes([]byte(it.value.String()))
|
||||
case *ByteArray:
|
||||
case *ByteArray, *Buffer:
|
||||
w.WriteB('"')
|
||||
val := it.Value().([]byte)
|
||||
b := make([]byte, base64.StdEncoding.EncodedLen(len(val)))
|
||||
base64.StdEncoding.Encode(b, val)
|
||||
w.WriteBytes(b)
|
||||
s, err := ToString(it)
|
||||
if err != nil {
|
||||
if buf.Err == nil {
|
||||
buf.Err = err
|
||||
}
|
||||
return
|
||||
}
|
||||
w.WriteBytes([]byte(s))
|
||||
w.WriteB('"')
|
||||
case *Bool:
|
||||
if it.value {
|
||||
|
@ -158,11 +162,7 @@ func (d *decoder) decode() (Item, error) {
|
|||
return nil, nil
|
||||
}
|
||||
case string:
|
||||
b, err := base64.StdEncoding.DecodeString(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewByteArray(b), nil
|
||||
return NewByteArray([]byte(t)), nil
|
||||
case float64:
|
||||
if math.Floor(t) != t {
|
||||
return nil, fmt.Errorf("real value is not allowed: %v", t)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package stackitem
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
|
@ -28,10 +27,9 @@ func getTestDecodeFunc(js string, expected ...interface{}) func(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestFromToJSON(t *testing.T) {
|
||||
var testBase64 = base64.StdEncoding.EncodeToString([]byte("test"))
|
||||
t.Run("ByteString", func(t *testing.T) {
|
||||
t.Run("Empty", getTestDecodeFunc(`""`, []byte{}))
|
||||
t.Run("Base64", getTestDecodeFunc(`"`+testBase64+`"`, "test"))
|
||||
t.Run("Base64", getTestDecodeFunc(`"test"`, "test"))
|
||||
})
|
||||
t.Run("BigInteger", func(t *testing.T) {
|
||||
t.Run("ZeroFloat", getTestDecodeFunc(`12.000`, 12, nil))
|
||||
|
@ -46,7 +44,7 @@ func TestFromToJSON(t *testing.T) {
|
|||
t.Run("Null", getTestDecodeFunc(`null`, Null{}))
|
||||
t.Run("Array", func(t *testing.T) {
|
||||
t.Run("Empty", getTestDecodeFunc(`[]`, NewArray([]Item{})))
|
||||
t.Run("Simple", getTestDecodeFunc((`[1,"`+testBase64+`",true,null]`),
|
||||
t.Run("Simple", getTestDecodeFunc((`[1,"test",true,null]`),
|
||||
NewArray([]Item{NewBigInteger(big.NewInt(1)), NewByteArray([]byte("test")), NewBool(true), Null{}})))
|
||||
t.Run("Nested", getTestDecodeFunc(`[[],[{},null]]`,
|
||||
NewArray([]Item{NewArray([]Item{}), NewArray([]Item{NewMap(), Null{}})})))
|
||||
|
@ -59,11 +57,10 @@ func TestFromToJSON(t *testing.T) {
|
|||
large.Add(NewByteArray([]byte("arr")), NewArray([]Item{NewByteArray([]byte("test"))}))
|
||||
t.Run("Empty", getTestDecodeFunc(`{}`, NewMap()))
|
||||
t.Run("Small", getTestDecodeFunc(`{"a":3}`, small))
|
||||
t.Run("Big", getTestDecodeFunc(`{"3":{"a":3},"arr":["`+testBase64+`"]}`, large))
|
||||
t.Run("Big", getTestDecodeFunc(`{"3":{"a":3},"arr":["test"]}`, large))
|
||||
})
|
||||
t.Run("Invalid", func(t *testing.T) {
|
||||
t.Run("Empty", getTestDecodeFunc(``, nil))
|
||||
t.Run("InvalidString", getTestDecodeFunc(`"not a base64"`, nil))
|
||||
t.Run("InvalidArray", getTestDecodeFunc(`[}`, nil))
|
||||
t.Run("InvalidMap", getTestDecodeFunc(`{]`, nil))
|
||||
t.Run("InvalidMapValue", getTestDecodeFunc(`{"a":{]}`, nil))
|
||||
|
@ -79,9 +76,7 @@ func TestFromToJSON(t *testing.T) {
|
|||
require.Error(t, err)
|
||||
})
|
||||
t.Run("BigByteArray", func(t *testing.T) {
|
||||
l := base64.StdEncoding.DecodedLen(MaxSize + 8)
|
||||
require.True(t, l < MaxSize) // check if test makes sense
|
||||
item := NewByteArray(make([]byte, l))
|
||||
item := NewByteArray(make([]byte, MaxSize))
|
||||
_, err := ToJSON(item)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue