Trait DebugToJson

Source
pub trait DebugToJson {
    // Required method
    fn fmt(&self, out: &mut Formatter<'_>) -> Result;

    // Provided method
    fn to_json_string(&self) -> String
       where Self: Sized { ... }
}
Expand description

Trait that can be automatically derived for structs and enums. When it’s derived, a Debug implementation is also added, so the object can be printed with println!.

Example:

use nop_json::DebugToJson;

#[derive(DebugToJson)]
struct Point {x: i32, y: i32}

let point = Point {x: 1, y: 2};
println!("{:?}", point);

Here is what automatic implementation expands to. If you write:

use nop_json::DebugToJson;

#[derive(DebugToJson)]
struct Point {x: i32, y: i32}

The implementation will be:

use nop_json::DebugToJson;

struct Point {x: i32, y: i32}

impl DebugToJson for Point
{	fn fmt(&self, out: &mut std::fmt::Formatter) -> std::fmt::Result
	{	write!(out, "{{\"x\":")?;
		DebugToJson::fmt(&self.x, out)?;
		write!(out, ",\"y\":")?;
		DebugToJson::fmt(&self.y, out)?;
		write!(out, "}}")
	}
}
impl std::fmt::Debug for Point
{	fn fmt(&self, out: &mut std::fmt::Formatter) -> std::fmt::Result
	{	DebugToJson::fmt(self, out)
	}
}

For more information, see main page.

Required Methods§

Source

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

Provided Methods§

Source

fn to_json_string(&self) -> String
where Self: Sized,

Any type that implements DebugToJson can be converted to JSON string. For example DebugToJson is implemented for primitive types:

use nop_json::DebugToJson;

let smth = true;
assert_eq!(smth.to_json_string(), "true".to_string());

For custom types:

use nop_json::DebugToJson;

#[derive(DebugToJson)]
struct Something {value: i32}

let smth = Something {value: 123};
assert_eq!(smth.to_json_string(), "{\"value\":123}".to_string());

Implementations on Foreign Types§

Source§

impl DebugToJson for bool

Source§

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

Source§

impl DebugToJson for char

Source§

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

Source§

impl DebugToJson for f32

Source§

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

Source§

impl DebugToJson for f64

Source§

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

Source§

impl DebugToJson for i8

Source§

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

Source§

impl DebugToJson for i16

Source§

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

Source§

impl DebugToJson for i32

Source§

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

Source§

impl DebugToJson for i64

Source§

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

Source§

impl DebugToJson for i128

Source§

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

Source§

impl DebugToJson for isize

Source§

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

Source§

impl DebugToJson for u8

Source§

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

Source§

impl DebugToJson for u16

Source§

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

Source§

impl DebugToJson for u32

Source§

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

Source§

impl DebugToJson for u64

Source§

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

Source§

impl DebugToJson for u128

Source§

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

Source§

impl DebugToJson for ()

Source§

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

Source§

impl DebugToJson for usize

Source§

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

Source§

impl DebugToJson for String

Source§

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

Source§

impl<A, B> DebugToJson for (A, B)
where A: DebugToJson, B: DebugToJson,

Source§

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

Source§

impl<A, B, C> DebugToJson for (A, B, C)

Source§

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

Source§

impl<T> DebugToJson for Option<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for Box<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for BTreeMap<String, T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for BTreeSet<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for LinkedList<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for VecDeque<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for Rc<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for Arc<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for Vec<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for HashMap<String, T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for HashSet<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for Mutex<T>
where T: DebugToJson,

Source§

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

Source§

impl<T> DebugToJson for RwLock<T>
where T: DebugToJson,

Source§

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

Implementors§