pub struct Hints<'a> { /* private fields */ }Expand description
Hints for Inferrer.
By default, Inferrer will never produce enum, values,
or discriminator forms. Hints tell Inferrer to use
these forms. See HintSet for details on how you can specify the “paths”
to the pieces of the input that should use these forms.
default_num_type tells Inferrer what numeric type
to attempt to use by default when it encounters a JSON number. This default
will be ignored if it doesn’t contain the example data. When the default is
ignored, the inferrer will infer the narrowest numerical type possible for
input data, preferring unsigned integers over signed integers.
To adapt the example used at the crate-level docs, here’s how you
could change Inferrer behavior using hints:
use serde_json::json;
use jtd_infer::{Inferrer, Hints, HintSet, NumType};
let enum_path = vec!["bar".to_string()];
let mut inferrer = Inferrer::new(Hints::new(
NumType::Float32,
HintSet::new(vec![&enum_path]),
HintSet::new(vec![]),
HintSet::new(vec![]),
));
inferrer = inferrer.infer(json!({ "foo": true, "bar": "xxx" }));
inferrer = inferrer.infer(json!({ "foo": false, "bar": null, "baz": 5 }));
let inference = inferrer.into_schema();
assert_eq!(
json!({
"properties": {
"foo": { "type": "boolean" },
"bar": { "enum": ["xxx"], "nullable": true }, // now an enum
},
"optionalProperties": {
"baz": { "type": "float32" }, // instead of uint8
},
}),
serde_json::to_value(inference.into_serde_schema()).unwrap(),
)Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Hints<'a>
impl<'a> RefUnwindSafe for Hints<'a>
impl<'a> Send for Hints<'a>
impl<'a> Sync for Hints<'a>
impl<'a> Unpin for Hints<'a>
impl<'a> UnwindSafe for Hints<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more