Trait nop_json::DebugToJson[][src]

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

    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

Provided methods

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

Implementors