pub struct Builder<D, F = (), P = ()> { /* private fields */ }Expand description
Configures optional callbacks before deserializing a value.
Created by DeserializerExt::track. Callbacks retain their concrete types
and need no boxing. filter and peek (with the peek
feature) can be configured in either order. Call .deserialize() to execute.
| Callbacks | Behavior |
|---|---|
| Neither | Consume unknown values without calling user code. |
| Filter only | Run the filter and consume values directly. |
| Peek only | Inspect every unknown value with context None::<()>. |
| Both | Inspect selected values with the filter’s Some(context). |
Filter and peek errors reject the input. The first callback error is returned unchanged, even if nested code catches the intermediate error.
Implementations§
Source§impl<D, F, P> Builder<D, F, P>
impl<D, F, P> Builder<D, F, P>
Sourcepub fn filter<'de, C, Filter>(self, filter: Filter) -> Builder<D, Filter, P>
pub fn filter<'de, C, Filter>(self, filter: Filter) -> Builder<D, Filter, P>
Sets a callback that runs before an unknown value is consumed.
Return Some(context) to select the value for peek, None to consume
it directly, or D::Error to reject the input. The context type C is
chosen by the caller; peek receives Some(context) for selected values.
Without a peek callback, both successful results consume values directly.
Calling this method again replaces the previous filter.
use serde::{Deserialize, de::Error};
use serde_unknown::DeserializerExt;
#[derive(Deserialize)]
struct Config { port: u16 }
let mut deserializer = serde_json::Deserializer::from_str(
r#"{"port":8080,"forbidden":true}"#,
);
let result: Result<Config, _> = (&mut deserializer)
.track()
.filter(|path| {
if path.matches(&["<root>", "forbidden"]) {
return Err(Error::custom("forbidden field"));
}
Ok(None::<()>)
})
.deserialize();
assert_eq!(result.err().unwrap().to_string(), "forbidden field");Sourcepub fn peek<'de, C, Peek>(self, peek: Peek) -> Builder<D, F, Peek>
pub fn peek<'de, C, Peek>(self, peek: Peek) -> Builder<D, F, Peek>
Sets a callback that receives an owned JSON value for selected fields.
Requires the peek feature. A filter supplies Some(context); returning
None from the filter skips this callback. Without a filter, every unknown
value is selected and the context is None with type Option<()>.
The callback runs after the complete subtree is parsed. It is not called
if parsing fails. Return Ok(()) to continue or D::Error to reject the
input. Calling this method again replaces the previous peek callback.
Inspection can allocate and requires deserialize_any support and values
representable by serde_json::Value. Duplicate object keys and some
format-specific representations are not preserved.
use serde::Deserialize;
use serde_unknown::DeserializerExt;
#[derive(Deserialize)]
struct Config { port: u16 }
let mut deserializer = serde_json::Deserializer::from_str(
r#"{"port":8080,"future":{"enabled":true},"other":false}"#,
);
let config: Config = (&mut deserializer)
.track()
.filter(|path| Ok(path.matches(&["<root>", "future"]).then_some("future")))
.peek(|_, context, value| {
assert_eq!(context, Some("future"));
assert_eq!(value, serde_json::json!({"enabled": true}));
Ok(())
})
.deserialize()?;
deserializer.end()?;
assert_eq!(config.port, 8080);Source§impl<'de, D: Deserializer<'de>> Builder<D>
impl<'de, D: Deserializer<'de>> Builder<D>
Sourcepub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
pub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
Deserializes T using the configured callbacks.
Returns the first callback error unchanged, or the underlying deserializer’s result when no callback rejects the input.
Source§impl<'de, D: Deserializer<'de>, F, C> Builder<D, F>
impl<'de, D: Deserializer<'de>, F, C> Builder<D, F>
Sourcepub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
pub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
Deserializes T using the configured callbacks.
Returns the first callback error unchanged, or the underlying deserializer’s result when no callback rejects the input.
Source§impl<'de, D: Deserializer<'de>, P> Builder<D, (), P>
impl<'de, D: Deserializer<'de>, P> Builder<D, (), P>
Sourcepub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
pub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
Deserializes T using the configured callbacks.
Returns the first callback error unchanged, or the underlying deserializer’s result when no callback rejects the input.
Source§impl<'de, D: Deserializer<'de>, F, P, C> Builder<D, F, P>
impl<'de, D: Deserializer<'de>, F, P, C> Builder<D, F, P>
Sourcepub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
pub fn deserialize<T: Deserialize<'de>>(self) -> Result<T, D::Error>
Deserializes T using the configured callbacks.
Returns the first callback error unchanged, or the underlying deserializer’s result when no callback rejects the input.