pub enum Value {
Show 30 variants Bool(bool), I8(i8), I16(i16), I32(i32), I64(i64), I128(i128), U8(u8), U16(u16), U32(u32), U64(u64), U128(u128), F32(f32), F64(f64), Char(char), Str(String), Bytes(Vec<u8>), None, Some(Box<Value>), Unit, UnitStruct(&'static str), UnitVariant { name: &'static str, variant_index: u32, variant: &'static str, }, NewtypeStruct(&'static strBox<Value>), NewtypeVariant { name: &'static str, variant_index: u32, variant: &'static str, value: Box<Value>, }, Seq(Vec<Value>), Tuple(Vec<Value>), TupleStruct(&'static strVec<Value>), TupleVariant { name: &'static str, variant_index: u32, variant: &'static str, fields: Vec<Value>, }, Map(IndexMap<Value, Value>), Struct(&'static strIndexMap<&'static str, Value>), StructVariant { name: &'static str, variant_index: u32, variant: &'static str, fields: IndexMap<&'static str, Value>, },
}
Expand description

Value is the internal represents of serde’s data format.

Value is the one-to-one map to serde’s data format. Theoretically, Value can be converted from/to any serde format.

Value is a inter data represents which means:

Value should be constructed or consumed by into_value or from_value.

  • t.into_value() -> Value
  • into_value(t) -> Value
  • T::from_value(v) -> T
  • from_value(v) -> T

Value also implements serde::Serialize and serde::Deserialize. Serialize and Deserialize on Value that converted from T will be the same with T.

Examples

Conversion between T and Value

use anyhow::Result;
use serde_bridge::{from_value, into_value, FromValue, IntoValue, Value};

fn main() -> Result<()> {
    let v = bool::from_value(Value::Bool(true))?;
    assert!(v);

    let v: bool = from_value(Value::Bool(true))?;
    assert!(v);

    let v = true.into_value()?;
    assert_eq!(v, Value::Bool(true));

    let v = into_value(true)?;
    assert_eq!(v, Value::Bool(true));

    Ok(())
}

Transparent Serialize and Deserialize

use anyhow::Result;
use serde_bridge::{from_value, into_value, FromValue, IntoValue, Value};
use serde_json;

fn main() -> Result<()> {
    let raw = serde_json::to_string(&true)?;
    let value = serde_json::to_string(&true.into_value()?)?;
    assert_eq!(raw, value);

    let raw: bool = serde_json::from_str("true")?;
    let value: Value = serde_json::from_str("true")?;
    assert_eq!(raw, bool::from_value(value)?);

    Ok(())
}

Variants

Bool(bool)

primitive types for bool: false/true

I8(i8)

primitive types for i8

I16(i16)

primitive types for i16

I32(i32)

primitive types for i32

I64(i64)

primitive types for i64

I128(i128)

primitive types for i128

U8(u8)

primitive types for u8

U16(u16)

primitive types for u16

U32(u32)

primitive types for u32

U64(u64)

primitive types for u64

U128(u128)

primitive types for u128

F32(f32)

primitive types for f32

F64(f64)

primitive types for f64

Char(char)

primitive types for char

Str(String)

string type

UTF-8 bytes with a length and no null terminator. May contain 0-bytes.

Bytes(Vec<u8>)

byte array

Similar to strings, during deserialization byte arrays can be transient, owned, or borrowed.

None

None part of an Option

Some(Box<Value>)

Some part of an Option

Note

We use Box here to workaround recursive data type.

Unit

The type of () in Rust.

It represents an anonymous value containing no data.

UnitStruct(&'static str)

For example struct Unit or PhantomData<T>.

It represents a named value containing no data.

UnitVariant

Fields

name: &'static str
variant_index: u32
variant: &'static str

For example the E::A and E::B in enum E { A, B }.

NewtypeStruct(&'static strBox<Value>)

For example struct Millimeters(u8).

NewtypeVariant

Fields

name: &'static str
variant_index: u32
variant: &'static str
value: Box<Value>

For example the E::N in enum E { N(u8) }.

Note

We use Box here to workaround recursive data type.

Seq(Vec<Value>)

A variably sized heterogeneous sequence of values, for example Vec<T> or HashSet<T>

Tuple(Vec<Value>)

A statically sized heterogeneous sequence of values for which the length will be known at deserialization time without looking at the serialized data.

For example (u8,) or (String, u64, Vec<T>) or [u64; 10].

TupleStruct(&'static strVec<Value>)

A named tuple, for example struct Rgb(u8, u8, u8).

TupleVariant

Fields

name: &'static str
variant_index: u32
variant: &'static str
fields: Vec<Value>

For example the E::T in enum E { T(u8, u8) }.

Map(IndexMap<Value, Value>)

A variably sized heterogeneous key-value pairing, for example BTreeMap<K, V>

Struct(&'static strIndexMap<&'static str, Value>)

A statically sized heterogeneous key-value pairing in which the keys are compile-time constant strings and will be known at deserialization time without looking at the serialized data.

For example struct S { r: u8, g: u8, b: u8 }.

StructVariant

Fields

name: &'static str
variant_index: u32
variant: &'static str
fields: IndexMap<&'static str, Value>

For example the E::S in enum E { S { r: u8, g: u8, b: u8 } }.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Implement Hash for Value so that we can use value as hash key.

Notes

Not all variants supports hash.

FIXME

does this implementation correct?

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Implement transparent serde::Serialize for Value.

Serialize on Value that converted from T will be the same with T.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.