serde-flexible
serde-flexible is a Rust library that enhances the flexibility of deserialization with Serde. It is designed to handle scenarios where clients may provide input data in varying formats or make mistakes with data types. This library allows you to define custom deserialization behavior for specific fields, making it easier to work with non-standard or inconsistent data.
Features
- Flexible deserialization for fields that can accept multiple formats.
- Support for uniquely reducible types (e.g., treating integers as strings during deserialization).
- Simplifies handling of real-world data from unreliable sources.
Example
use Deserialize;
use ;
All Possible Deserializers
Below are all the deserializers provided by serde-flexible, along with their descriptions:
Standard Deserializers
as_bool: Converts any input that can be interpreted as a boolean tobool. For example,"true",1, and0can all be deserialized astrueorfalse.as_f64: Converts any input that can be interpreted as a floating-point number tof64. For example,"3.14"will be deserialized as3.14.as_i64: Converts any input that can be interpreted as an integer toi64. For example,"42"and42will be deserialized as42.as_string: Converts any input that can be interpreted as a string toString. For example,123will be converted to"123".as_u64: Converts any input that can be interpreted as an unsigned integer tou64. For example,"100"and100will be deserialized as100.
Optional Deserializers
These deserializers allow the field to accept null or missing values in addition to valid inputs:
as_bool_opt: Similar toas_bool, but also allowsnullvalues, deserializing asOption<bool>.as_f64_opt: Similar toas_f64, but also allowsnullvalues, deserializing asOption<f64>.as_i64_opt: Similar toas_i64, but also allowsnullvalues, deserializing asOption<i64>.as_string_opt: Similar toas_string, but also allowsnullvalues, deserializing asOption<String>.as_u64_opt: Similar toas_u64, but also allowsnullvalues, deserializing asOption<u64>.