Expand description
A dynamic value type for deser.
Value can hold any value of the deser data model. It’s useful for
data whose structure is not known up front, to inspect or transform data
before it’s deserialized into a type, or to convert between formats:
use deser_value::{Value, value};
let mut config: Value =
deser_json::from_str(r#"{"name": "app", "port": 8080}"#).unwrap();
config["port"] = value!(9090);
config["tags"] = value!(["web", "prod"]);
assert_eq!(
deser_json::to_string(&config).unwrap(),
r#"{"name":"app","port":9090,"tags":["web","prod"]}"#
);Values are converted from and into other types with to_value and
from_value. To configure the conversion (for instance to add
layers) use the Serializer and the Deserializer. Values can be
built with the value! macro.
§Retained Information
Values try to retain as much information as possible, which means that a value that is deserialized and serialized again comes out the same:
- Map keys can be any value (like integers in CBOR) and maps retain the order of their entries.
- Values that extend the data model (like date-times, UUIDs or exact
numbers, see
deser::ext) retain their type. - Maps and sequences retain their
Order. - Bytes retain their fallback.
- Event data, which is information that is
attached to values but not part of the data model (for instance CBOR
tags or formatting hints), is retained in the
Metadata of values. - If the format tracks locations (see
TrackLocations), values retain theirSpanin the input. Types that are deserialized from such values report errors at the original location:
use deser::{Context, Deserialize, TrackLocations};
use deser_value::{Value, from_value};
#[derive(Debug, Deserialize)]
struct Config {
port: u16,
}
let config = deser_json::DeserializerConfig::builder()
.context(Context::with(TrackLocations(true)))
.build();
let value: Value = config.from_str("{\n \"port\": \"80\"\n}").unwrap();
let err = from_value::<Config>(&value).unwrap_err();
assert_eq!((err.line(), err.column()), (Some(2), Some(11)));§Duplicate Keys
Map keys are unique. If a key is given more than once, the
DuplicateKeys policy of the
deserialization decides: by default the deserialization fails, otherwise
the first or the last value is used. The keys of multimaps (like the
parameters of query strings) collect their values instead, see
Seq::is_repeated.
Macros§
Structs§
- Deserializer
- Deserializes types from a
Value. - Into
Iter - An owning iterator over the entries of a
Map. - Iter
- An iterator over the entries of a
Map. - IterMut
- An iterator over the entries of a
Mapwith mutable values. - Keys
- An iterator over the keys of a
Map. - Map
- A map of values.
- Meta
- Meta data of a
Value. - Seq
- A sequence of values.
- Serializer
- Serializes values into
Values. - Span
- The location of a value in its input.
- Value
- A dynamic value.
- Values
- An iterator over the values of a
Map. - Values
Mut - An iterator over the mutable values of a
Map.
Enums§
Traits§
- MapKey
- A type that map keys can be looked up with.
- Value
Index - A type that can index into a
Value.
Functions§
- from_
value - Deserializes a type from a value.
- to_
value - Serializes a value into a
Value.