Struct JsonRef

Source
pub struct JsonRef { /* private fields */ }
Expand description

Main struct that holds configuration for a JSONScheama derefferencing.

Instantiate with

use jsonref::JsonRef;
let jsonref = JsonRef::new();

Configuration is done through the set_ methods on the struct.

Implementations§

Source§

impl JsonRef

Source

pub fn new() -> JsonRef

Create a new instance of JsonRef.

Source

pub fn set_reference_key(&mut self, reference_key: &str)

Set a key to store the data that the $ref replaced.

This example uses __reference__ as the key.

use serde_json::json;

let mut input  = json!(
    {"properties": {"prop1": {"title": "name"},
                    "prop2": {"$ref": "#/properties/prop1", "title": "old_title"}}
    }
);

let expected = json!(
    {"properties": {"prop1": {"title": "name"},
                    "prop2": {"title": "name", "__reference__": {"title": "old_title"}}}
    }
);
                                                                                          
let mut jsonref = JsonRef::new();

jsonref.set_reference_key("__reference__");

jsonref.deref_value(&mut input).unwrap();
                                                                                          
assert_eq!(input, expected)
Source

pub fn deref_value(&mut self, value: &mut Value) -> Result<(), Error>

deref a serde_json value directly. Uses the current working directory for any relative refs.

Source

pub fn deref_url(&mut self, url: &str) -> Result<Value, Error>

deref from a URL:

let mut jsonref = JsonRef::new();
let input_url = jsonref.deref_url("https://gist.githubusercontent.com/kindly/91e09f88ced65aaca1a15d85a56a28f9/raw/52f8477435cff0b73c54aacc70926c101ce6c685/base.json").unwrap();
Source

pub fn deref_file(&mut self, file_path: &str) -> Result<Value, Error>

deref from a File:


let mut jsonref = JsonRef::new();
let file_example = jsonref
    .deref_file("fixtures/nested_relative/base.json")
    .unwrap();

Trait Implementations§

Source§

impl Debug for JsonRef

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,