scale-value ยท
This crate provides a Value
type, which is a runtime representation that is compatible with scale_info::TypeDef
. It somewhat analogous to a serde_json::Value
, which is a runtime representation of JSON values, but with a focus on SCALE encoded values instead of JSON encoded values. Unlike JSON however, SCALE encoding is not self describing, and so we need additional type information to tell us how to encode and decode values.
It is expected that this crate will commonly be used in conjunction with the scale-info and frame-metadata crates.
The scale-info crate allows us to define types and add them to a type registry, which in turn is used to tell us how to SCALE encode and decode Value
s.
The frame-metadata crate contains all of the type information we need in order to be able to SCALE encode and decode Value
s into the various parameters needed in extrinsics and such.
Crate features (enabled by default):
serde
: AllowValue
s to be converted from and to static Rust types (where possible), or serialized and deserialized to other formats like JSON, via serde.from_string
: Allow strings to be parsed intoValues
using the same format from which values can be converted to strings via.to_string()
. Examples:- Boolean types parse from
true
andfalse
. - Strings and chars are supported with
"Hello\n there"
and'a'
. - Numbers like
1_234_567
and-123
are supported. - Composite types (structs/tuples) look like
{ hello: 123, "there": true }
and('a', 'b', true)
. - Finally, enum variants look like
Hello { foo: 1, bar: 2 }
andFoo(1,2,3)
.
- Boolean types parse from
Examples
Manually creating a type registry, and then using it to SCALE encode and decode some runtime constructed Value
type to/from SCALE bytes.
// Turn a type into an ID and type registry using `scale-info`:
// Some type which we have derived SCALE type information about:
// We can build a type registry containing just this type:
let = ;
use Value;
// Next, we can construct a runtime value of a similar shape:
let value = named_variant;
// Given the type registry and ID, we can try to convert our Value into SCALE bytes:
let mut bytes = Vec new;
encode_as_type.unwrap;
// We can also go the other way, and decode out bytes back into the same Value:
let new_value = decode_as_type.unwrap;
// The two values should equal each other (`.remove_context()` just removes the additional
// type information handed back when the value is decoded):
assert_eq!;
Using the serde
feature to convert a Value
to/from some rust type via serde:
use Value;
use ;
// Some type we want to be able to serialize/deserialize:
// First, serialize a Value into the rust type:
let value = named_variant;
let foo1: Foo = from_value.unwrap;
assert_eq!;
// Next, deserialize the rust type back into a Value:
let new_value = to_value.unwrap;
assert_eq!;
Check out the documentation for a full API reference and more examples.