2020-06-11 13:27:57 +00:00
|
|
|
package stackitem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
gio "io"
|
|
|
|
"math/big"
|
2022-03-04 12:03:16 +00:00
|
|
|
"strconv"
|
2022-05-11 18:03:10 +00:00
|
|
|
"strings"
|
2020-06-11 13:27:57 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// decoder is a wrapper around json.Decoder helping to mimic C# json decoder behavior.
|
2020-06-11 13:27:57 +00:00
|
|
|
type decoder struct {
|
|
|
|
json.Decoder
|
|
|
|
|
2021-08-02 10:28:12 +00:00
|
|
|
count int
|
2020-06-11 13:27:57 +00:00
|
|
|
depth int
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxAllowedInteger is the maximum integer allowed to be encoded.
|
|
|
|
const MaxAllowedInteger = 2<<53 - 1
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MaxJSONDepth is the maximum allowed nesting level of an encoded/decoded JSON.
|
2021-07-06 21:18:00 +00:00
|
|
|
const MaxJSONDepth = 10
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ErrInvalidValue is returned when an item value doesn't fit some constraints
|
2021-07-06 21:18:00 +00:00
|
|
|
// during serialization or deserialization.
|
|
|
|
var ErrInvalidValue = errors.New("invalid value")
|
|
|
|
|
|
|
|
// ErrTooDeep is returned when JSON encoder/decoder goes beyond MaxJSONDepth in
|
|
|
|
// its processing.
|
|
|
|
var ErrTooDeep = errors.New("too deep")
|
2020-06-11 13:27:57 +00:00
|
|
|
|
|
|
|
// ToJSON encodes Item to JSON.
|
|
|
|
// It behaves as following:
|
2022-08-08 10:23:21 +00:00
|
|
|
//
|
|
|
|
// ByteArray -> base64 string
|
|
|
|
// BigInteger -> number
|
|
|
|
// Bool -> bool
|
|
|
|
// Null -> null
|
|
|
|
// Array, Struct -> array
|
|
|
|
// Map -> map with keys as UTF-8 bytes
|
2020-06-11 13:27:57 +00:00
|
|
|
func ToJSON(item Item) ([]byte, error) {
|
2021-11-30 16:51:52 +00:00
|
|
|
seen := make(map[Item]sliceNoPointer, typicalNumOfItems)
|
2021-07-09 11:04:36 +00:00
|
|
|
return toJSON(nil, seen, item)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// sliceNoPointer represents a sub-slice of a known slice.
|
|
|
|
// It doesn't contain any pointer and uses less memory than `[]byte`.
|
2021-07-09 11:04:36 +00:00
|
|
|
type sliceNoPointer struct {
|
|
|
|
start, end int
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 11:04:36 +00:00
|
|
|
func toJSON(data []byte, seen map[Item]sliceNoPointer, item Item) ([]byte, error) {
|
|
|
|
if len(data) > MaxSize {
|
|
|
|
return nil, errTooBigSize
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
2021-07-09 11:04:36 +00:00
|
|
|
|
|
|
|
if old, ok := seen[item]; ok {
|
|
|
|
if len(data)+old.end-old.start > MaxSize {
|
|
|
|
return nil, errTooBigSize
|
|
|
|
}
|
|
|
|
return append(data, data[old.start:old.end]...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
start := len(data)
|
|
|
|
var err error
|
|
|
|
|
2020-06-11 13:27:57 +00:00
|
|
|
switch it := item.(type) {
|
|
|
|
case *Array, *Struct:
|
2021-07-09 12:25:40 +00:00
|
|
|
var items []Item
|
|
|
|
if a, ok := it.(*Array); ok {
|
|
|
|
items = a.value
|
|
|
|
} else {
|
|
|
|
items = it.(*Struct).value
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, '[')
|
2020-06-11 13:27:57 +00:00
|
|
|
for i, v := range items {
|
2021-07-09 11:04:36 +00:00
|
|
|
data, err = toJSON(data, seen, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-11 13:27:57 +00:00
|
|
|
if i < len(items)-1 {
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, ',')
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, ']')
|
|
|
|
seen[item] = sliceNoPointer{start, len(data)}
|
2020-06-11 13:27:57 +00:00
|
|
|
case *Map:
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, '{')
|
2020-06-11 13:27:57 +00:00
|
|
|
for i := range it.value {
|
2020-07-29 08:28:29 +00:00
|
|
|
// map key can always be converted to []byte
|
|
|
|
// but are not always a valid UTF-8.
|
2021-07-09 11:04:36 +00:00
|
|
|
raw, err := itemToJSONString(it.value[i].Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, raw...)
|
|
|
|
data = append(data, ':')
|
|
|
|
data, err = toJSON(data, seen, it.value[i].Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-11 13:27:57 +00:00
|
|
|
if i < len(it.value)-1 {
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, ',')
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, '}')
|
|
|
|
seen[item] = sliceNoPointer{start, len(data)}
|
2020-06-11 13:27:57 +00:00
|
|
|
case *BigInteger:
|
2021-08-04 13:41:13 +00:00
|
|
|
if it.Big().CmpAbs(big.NewInt(MaxAllowedInteger)) == 1 {
|
2021-07-09 11:04:36 +00:00
|
|
|
return nil, fmt.Errorf("%w (MaxAllowedInteger)", ErrInvalidValue)
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
2021-08-04 13:41:13 +00:00
|
|
|
data = append(data, it.Big().String()...)
|
2020-12-11 10:23:07 +00:00
|
|
|
case *ByteArray, *Buffer:
|
2021-07-09 11:04:36 +00:00
|
|
|
raw, err := itemToJSONString(it)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, raw...)
|
2021-08-03 16:37:06 +00:00
|
|
|
case Bool:
|
|
|
|
if it {
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, "true"...)
|
2020-06-11 13:27:57 +00:00
|
|
|
} else {
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, "false"...)
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
|
|
|
case Null:
|
2021-07-09 11:04:36 +00:00
|
|
|
data = append(data, "null"...)
|
2020-06-11 13:27:57 +00:00
|
|
|
default:
|
2021-07-09 11:04:36 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", ErrUnserializable, it.String())
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
2021-07-09 11:04:36 +00:00
|
|
|
if len(data) > MaxSize {
|
|
|
|
return nil, errTooBigSize
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
2021-07-09 11:04:36 +00:00
|
|
|
return data, nil
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// itemToJSONString converts it to a string
|
|
|
|
// in quotation marks with control characters escaped.
|
2021-07-09 11:04:36 +00:00
|
|
|
func itemToJSONString(it Item) ([]byte, error) {
|
2021-03-01 15:30:10 +00:00
|
|
|
s, err := ToString(it)
|
|
|
|
if err != nil {
|
2021-07-09 11:04:36 +00:00
|
|
|
return nil, err
|
2021-03-01 15:30:10 +00:00
|
|
|
}
|
|
|
|
data, _ := json.Marshal(s) // error never occurs because `ToString` checks for validity
|
vm: serialize `+` rune as `\u002B` for byte-contained stackitems
This commit fixes the following Go vs C# state diff:
block 74613: value mismatch for key EwAAAHN0cmVhbXMvDg==: eyJpZCI6MTQsInN0YXJ0IjoxNjIyNTAwMjAwMDAwLCJzdG9wIjoxNjIyNTAyMDAwMDAwLCJkZXBvc2l0IjoxMDAwMDAwMDAsInJlbWFpbmluZyI6MTAwMDAwMDAwLCJzZW5kZXIiOiJmeEY4RDl2ZFU3K0gwcDV3NTlyWllMNytNSlE9IiwicmVjaXBpZW50IjoiSVV6c3pveFV0S1NGVnlZRGczSmdTQTFlbTFNPSJ9 vs eyJpZCI6MTQsInN0YXJ0IjoxNjIyNTAwMjAwMDAwLCJzdG9wIjoxNjIyNTAyMDAwMDAwLCJkZXBvc2l0IjoxMDAwMDAwMDAsInJlbWFpbmluZyI6MTAwMDAwMDAwLCJzZW5kZXIiOiJmeEY4RDl2ZFU3XHUwMDJCSDBwNXc1OXJaWUw3XHUwMDJCTUpRPSIsInJlY2lwaWVudCI6IklVenN6b3hVdEtTRlZ5WURnM0pnU0ExZW0xTT0ifQ==
I.e.:
```
{"id":14,"start":1622500200000,"stop":1622502000000,"deposit":100000000,"remaining":100000000,"sender":"fxF8D9vdU7+H0p5w59rZYL7+MJQ=","recipient":"IUzszoxUtKSFVyYDg3JgSA1em1M="}
```
vs
```
{"id":14,"start":1622500200000,"stop":1622502000000,"deposit":100000000,"remaining":100000000,"sender":"fxF8D9vdU7\u002BH0p5w59rZYL7\u002BMJQ=","recipient":"IUzszoxUtKSFVyYDg3JgSA1em1M="}
```
2021-06-08 13:40:38 +00:00
|
|
|
|
|
|
|
// ref https://github.com/neo-project/neo-modules/issues/375 and https://github.com/dotnet/runtime/issues/35281
|
2021-07-09 11:04:36 +00:00
|
|
|
return bytes.Replace(data, []byte{'+'}, []byte("\\u002B"), -1), nil
|
2021-03-01 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// FromJSON decodes an Item from JSON.
|
2020-06-11 13:27:57 +00:00
|
|
|
// It behaves as following:
|
2022-08-08 10:23:21 +00:00
|
|
|
//
|
|
|
|
// string -> ByteArray from base64
|
|
|
|
// number -> BigInteger
|
|
|
|
// bool -> Bool
|
|
|
|
// null -> Null
|
|
|
|
// array -> Array
|
|
|
|
// map -> Map, keys are UTF-8
|
2021-08-02 10:28:12 +00:00
|
|
|
func FromJSON(data []byte, maxCount int) (Item, error) {
|
|
|
|
d := decoder{
|
|
|
|
Decoder: *json.NewDecoder(bytes.NewReader(data)),
|
|
|
|
count: maxCount,
|
|
|
|
}
|
2022-05-11 18:03:10 +00:00
|
|
|
d.UseNumber()
|
2020-06-11 13:27:57 +00:00
|
|
|
if item, err := d.decode(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if _, err := d.Token(); err != gio.EOF {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, fmt.Errorf("%w: unexpected items", ErrInvalidValue)
|
2020-06-11 13:27:57 +00:00
|
|
|
} else {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) decode() (Item, error) {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-02 10:28:12 +00:00
|
|
|
|
|
|
|
d.count--
|
|
|
|
if d.count < 0 && tok != json.Delim('}') && tok != json.Delim(']') {
|
|
|
|
return nil, errTooBigElements
|
|
|
|
}
|
|
|
|
|
2020-06-11 13:27:57 +00:00
|
|
|
switch t := tok.(type) {
|
|
|
|
case json.Delim:
|
|
|
|
switch t {
|
|
|
|
case json.Delim('{'), json.Delim('['):
|
2021-07-06 21:18:00 +00:00
|
|
|
if d.depth == MaxJSONDepth {
|
|
|
|
return nil, ErrTooDeep
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
|
|
|
d.depth++
|
|
|
|
var item Item
|
|
|
|
if t == json.Delim('{') {
|
|
|
|
item, err = d.decodeMap()
|
|
|
|
} else {
|
|
|
|
item, err = d.decodeArray()
|
|
|
|
}
|
|
|
|
d.depth--
|
|
|
|
return item, err
|
|
|
|
default:
|
2021-08-02 10:28:12 +00:00
|
|
|
d.count++
|
2020-06-11 13:27:57 +00:00
|
|
|
// no error above means corresponding closing token
|
|
|
|
// was encountered for map or array respectively
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
case string:
|
2020-12-11 10:23:07 +00:00
|
|
|
return NewByteArray([]byte(t)), nil
|
2022-05-11 18:03:10 +00:00
|
|
|
case json.Number:
|
|
|
|
ts := t.String()
|
|
|
|
dot := strings.IndexByte(ts, '.')
|
|
|
|
if dot != -1 {
|
|
|
|
// As a special case numbers like 123.000 are allowed (SetString rejects them).
|
|
|
|
// And yes, that's the way C# code works also.
|
|
|
|
for _, r := range ts[dot+1:] {
|
|
|
|
if r != '0' {
|
|
|
|
return nil, fmt.Errorf("%w (real value for int)", ErrInvalidValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts = ts[:dot]
|
|
|
|
}
|
|
|
|
num, ok := new(big.Int).SetString(ts, 10)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%w (integer)", ErrInvalidValue)
|
2020-06-11 13:27:57 +00:00
|
|
|
}
|
2022-05-11 18:03:10 +00:00
|
|
|
return NewBigInteger(num), nil
|
2020-06-11 13:27:57 +00:00
|
|
|
case bool:
|
|
|
|
return NewBool(t), nil
|
|
|
|
default:
|
|
|
|
// it can be only `nil`
|
|
|
|
return Null{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) decodeArray() (*Array, error) {
|
|
|
|
items := []Item{}
|
|
|
|
for {
|
|
|
|
item, err := d.decode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if item == nil {
|
|
|
|
return NewArray(items), nil
|
|
|
|
}
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) decodeMap() (*Map, error) {
|
|
|
|
m := NewMap()
|
|
|
|
for {
|
|
|
|
key, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
k, ok := key.(string)
|
|
|
|
if !ok {
|
|
|
|
return m, nil
|
|
|
|
}
|
2021-08-02 10:28:12 +00:00
|
|
|
|
|
|
|
d.count--
|
|
|
|
if d.count < 0 {
|
|
|
|
return nil, errTooBigElements
|
|
|
|
}
|
2020-06-11 13:27:57 +00:00
|
|
|
val, err := d.decode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.Add(NewByteArray([]byte(k)), val)
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 09:39:52 +00:00
|
|
|
|
|
|
|
// ToJSONWithTypes serializes any stackitem to JSON in a lossless way.
|
|
|
|
func ToJSONWithTypes(item Item) ([]byte, error) {
|
2022-03-04 12:03:16 +00:00
|
|
|
return toJSONWithTypes(nil, item, make(map[Item]sliceNoPointer, typicalNumOfItems))
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 12:03:16 +00:00
|
|
|
func toJSONWithTypes(data []byte, item Item, seen map[Item]sliceNoPointer) ([]byte, error) {
|
|
|
|
if item == nil {
|
|
|
|
return nil, fmt.Errorf("%w: nil", ErrUnserializable)
|
|
|
|
}
|
|
|
|
if old, ok := seen[item]; ok {
|
|
|
|
if old.end == 0 {
|
|
|
|
// Compound item marshaling which has not yet finished.
|
|
|
|
return nil, ErrRecursive
|
|
|
|
}
|
|
|
|
if len(data)+old.end-old.start > MaxSize {
|
|
|
|
return nil, errTooBigSize
|
|
|
|
}
|
|
|
|
return append(data, data[old.start:old.end]...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var val string
|
|
|
|
var hasValue bool
|
|
|
|
switch item.(type) {
|
|
|
|
case Null:
|
|
|
|
val = `{"type":"Any"}`
|
|
|
|
case *Interop:
|
2022-05-25 10:20:21 +00:00
|
|
|
val = `{"type":"InteropInterface"}`
|
2022-03-04 12:03:16 +00:00
|
|
|
default:
|
|
|
|
val = `{"type":"` + item.Type().String() + `","value":`
|
|
|
|
hasValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data)+len(val) > MaxSize {
|
|
|
|
return nil, errTooBigSize
|
2021-07-06 14:33:16 +00:00
|
|
|
}
|
2022-03-04 12:03:16 +00:00
|
|
|
|
|
|
|
start := len(data)
|
|
|
|
|
|
|
|
data = append(data, val...)
|
|
|
|
if !hasValue {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Primitive stack items are appended after the switch
|
|
|
|
// to reduce the amount of size checks.
|
|
|
|
var primitive string
|
|
|
|
var isBuffer bool
|
|
|
|
var err error
|
|
|
|
|
2020-07-29 09:39:52 +00:00
|
|
|
switch it := item.(type) {
|
|
|
|
case *Array, *Struct:
|
2022-03-04 12:03:16 +00:00
|
|
|
seen[item] = sliceNoPointer{}
|
|
|
|
data = append(data, '[')
|
|
|
|
for i, elem := range it.Value().([]Item) {
|
|
|
|
if i != 0 {
|
|
|
|
data = append(data, ',')
|
|
|
|
}
|
|
|
|
data, err = toJSONWithTypes(data, elem, seen)
|
2020-07-29 09:39:52 +00:00
|
|
|
if err != nil {
|
2022-03-04 12:03:16 +00:00
|
|
|
return nil, err
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-03 16:37:06 +00:00
|
|
|
case Bool:
|
2022-03-04 12:03:16 +00:00
|
|
|
if it {
|
|
|
|
primitive = "true"
|
|
|
|
} else {
|
|
|
|
primitive = "false"
|
|
|
|
}
|
|
|
|
case *ByteArray:
|
|
|
|
primitive = `"` + base64.StdEncoding.EncodeToString(it.Value().([]byte)) + `"`
|
|
|
|
case *Buffer:
|
|
|
|
isBuffer = true
|
|
|
|
primitive = `"` + base64.StdEncoding.EncodeToString(it.Value().([]byte)) + `"`
|
2020-07-29 09:39:52 +00:00
|
|
|
case *BigInteger:
|
2022-03-04 12:03:16 +00:00
|
|
|
primitive = `"` + it.Big().String() + `"`
|
2020-07-29 09:39:52 +00:00
|
|
|
case *Map:
|
2022-03-04 12:03:16 +00:00
|
|
|
seen[item] = sliceNoPointer{}
|
|
|
|
data = append(data, '[')
|
2020-07-29 09:39:52 +00:00
|
|
|
for i := range it.value {
|
2022-03-04 12:03:16 +00:00
|
|
|
if i != 0 {
|
|
|
|
data = append(data, ',')
|
|
|
|
}
|
|
|
|
data = append(data, `{"key":`...)
|
|
|
|
data, err = toJSONWithTypes(data, it.value[i].Key, seen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, `,"value":`...)
|
|
|
|
data, err = toJSONWithTypes(data, it.value[i].Value, seen)
|
2020-07-29 09:39:52 +00:00
|
|
|
if err != nil {
|
2022-03-04 12:03:16 +00:00
|
|
|
return nil, err
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
2022-03-04 12:03:16 +00:00
|
|
|
data = append(data, '}')
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
|
|
|
case *Pointer:
|
2022-03-04 12:03:16 +00:00
|
|
|
primitive = strconv.Itoa(it.pos)
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
2022-03-04 12:03:16 +00:00
|
|
|
if len(primitive) != 0 {
|
|
|
|
if len(data)+len(primitive)+1 > MaxSize {
|
|
|
|
return nil, errTooBigSize
|
|
|
|
}
|
|
|
|
data = append(data, primitive...)
|
|
|
|
data = append(data, '}')
|
|
|
|
|
|
|
|
if isBuffer {
|
|
|
|
seen[item] = sliceNoPointer{start, len(data)}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(data)+2 > MaxSize { // also take care of '}'
|
|
|
|
return nil, errTooBigSize
|
|
|
|
}
|
|
|
|
data = append(data, ']', '}')
|
|
|
|
|
|
|
|
seen[item] = sliceNoPointer{start, len(data)}
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
2022-03-04 12:03:16 +00:00
|
|
|
return data, nil
|
2020-07-29 09:39:52 +00:00
|
|
|
}
|
2020-07-30 09:56:26 +00:00
|
|
|
|
|
|
|
type (
|
|
|
|
rawItem struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value json.RawMessage `json:"value,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMapElement struct {
|
|
|
|
Key json.RawMessage `json:"key"`
|
|
|
|
Value json.RawMessage `json:"value"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-07-06 21:18:00 +00:00
|
|
|
func mkErrValue(err error) error {
|
|
|
|
return fmt.Errorf("%w: %v", ErrInvalidValue, err)
|
|
|
|
}
|
|
|
|
|
2020-07-30 09:56:26 +00:00
|
|
|
// FromJSONWithTypes deserializes an item from typed-json representation.
|
|
|
|
func FromJSONWithTypes(data []byte) (Item, error) {
|
|
|
|
raw := new(rawItem)
|
|
|
|
if err := json.Unmarshal(data, raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
typ, err := FromString(raw.Type)
|
|
|
|
if err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, fmt.Errorf("%w: %v", ErrInvalidType, raw.Type)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case AnyT:
|
|
|
|
return Null{}, nil
|
|
|
|
case PointerT:
|
|
|
|
var pos int
|
|
|
|
if err := json.Unmarshal(raw.Value, &pos); err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
return NewPointer(pos, nil), nil
|
|
|
|
case BooleanT:
|
|
|
|
var b bool
|
|
|
|
if err := json.Unmarshal(raw.Value, &b); err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
return NewBool(b), nil
|
|
|
|
case IntegerT:
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(raw.Value, &s); err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
val, ok := new(big.Int).SetString(s, 10)
|
|
|
|
if !ok {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(errors.New("not an integer"))
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
return NewBigInteger(val), nil
|
|
|
|
case ByteArrayT, BufferT:
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(raw.Value, &s); err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
val, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
if typ == ByteArrayT {
|
|
|
|
return NewByteArray(val), nil
|
|
|
|
}
|
|
|
|
return NewBuffer(val), nil
|
|
|
|
case ArrayT, StructT:
|
|
|
|
var arr []json.RawMessage
|
|
|
|
if err := json.Unmarshal(raw.Value, &arr); err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
items := make([]Item, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
it, err := FromJSONWithTypes(arr[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items[i] = it
|
|
|
|
}
|
|
|
|
if typ == ArrayT {
|
|
|
|
return NewArray(items), nil
|
|
|
|
}
|
|
|
|
return NewStruct(items), nil
|
|
|
|
case MapT:
|
|
|
|
var arr []rawMapElement
|
|
|
|
if err := json.Unmarshal(raw.Value, &arr); err != nil {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkErrValue(err)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
m := NewMap()
|
|
|
|
for i := range arr {
|
|
|
|
key, err := FromJSONWithTypes(arr[i].Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-29 07:56:57 +00:00
|
|
|
} else if err = IsValidMapKey(key); err != nil {
|
|
|
|
return nil, err
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
value, err := FromJSONWithTypes(arr[i].Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.Add(key, value)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case InteropT:
|
|
|
|
return NewInterop(nil), nil
|
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, fmt.Errorf("%w: %v", ErrInvalidType, typ)
|
2020-07-30 09:56:26 +00:00
|
|
|
}
|
|
|
|
}
|