Trait nop_json::ValidateJson[][src]

pub trait ValidateJson: Sized {
    fn validate_json(self) -> Result<Self, String> { ... }
}

I auto-implement this trait for all types. During deserialization process, you may want to complain on invalid fields combination. I call ok_from_json() right after i deserialized some object with #[derive(TryFromJson)]. You can implement this function on your structs/enums.

Examples

use nop_json::{Reader, TryFromJson};
use std::io;

#[derive(TryFromJson, Debug)]
struct FromTo {from: i32, to: i32}

impl nop_json::ValidateJson for FromTo
{	fn validate_json(self) -> Result<Self, String>
	{	if self.from <= self.to
		{	Ok(self)
		}
		else
		{	Err("to must be after from".to_string())
		}
	}
}

let mut reader = Reader::new(r#" {"from": 0, "to": 10}   {"from": 3, "to": -1} "#.bytes());
let obj_0: io::Result<FromTo> = reader.read();
let obj_1: io::Result<FromTo> = reader.read();
assert!(obj_0.is_ok());
assert!(obj_1.is_err());

Provided methods

fn validate_json(self) -> Result<Self, String>[src]

Loading content...

Implementors

Loading content...