Struct jtd_infer::Hints[][src]

pub struct Hints<'a> { /* fields omitted */ }

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

impl<'a> Hints<'a>[src]

pub fn new(
    default_num_type: NumType,
    enums: HintSet<'a>,
    values: HintSet<'a>,
    discriminator: HintSet<'a>
) -> Self
[src]

Constructs a new set of Hints.

Auto Trait Implementations

impl<'a> RefUnwindSafe for Hints<'a>[src]

impl<'a> Send for Hints<'a>[src]

impl<'a> Sync for Hints<'a>[src]

impl<'a> Unpin for Hints<'a>[src]

impl<'a> UnwindSafe for Hints<'a>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.