DisplayJson

Trait DisplayJson 

Source
pub trait DisplayJson {
    // Required method
    fn fmt(&self, f: &mut JsonFormatter<'_, '_>) -> Result;
}
Expand description

A variant of the Display trait for JSON.

This trait allows Rust types to be formatted as valid JSON. Unlike the standard Display trait, DisplayJson is designed for JSON serialization and supports proper escaping, indentation, and other JSON-specific formatting features.

§Implementation Notes

nojson provides built-in implementations for many common Rust types:

  • Basic types (booleans, integers, floats, strings)
  • Collection types (arrays, vectors, sets, maps)
  • Nullable types (via Option<T>)
  • Reference types

§Examples

Implementing DisplayJson for a struct:

use nojson::{DisplayJson, JsonFormatter, Json};

struct Person {
    name: String,
    age: u32,
    email: Option<String>,
}

impl DisplayJson for Person {
    fn fmt(&self, f: &mut JsonFormatter<'_, '_>) -> std::fmt::Result {
        f.object(|f| {
            f.member("name", &self.name)?;
            f.member("age", &self.age)?;
            f.member("email", &self.email)
        })
    }
}

// Now you can use it with `Json` wrapper
let person = Person {
    name: "Alice".to_string(),
    age: 30,
    email: Some("alice@example.com".to_string()),
};

assert_eq!(
    Json(&person).to_string(),
    r#"{"name":"Alice","age":30,"email":"alice@example.com"}"#
);

Generating JSON in-place using json():

use nojson::{DisplayJson, json};
use std::collections::BTreeMap;

// Build a JSON object with pretty-printing.
let object = json(|f| {
    f.set_indent_size(2);
    f.set_spacing(true);
    f.object(|f| {
        f.member("name", "Example")?;
        f.member("counts", &[1, 2, 3])?;
        f.member("config", json(|f| f.object(|f| {
            f.member("enabled", true);
            f.member("visible", false)
        })))
    })
});

// Generate a JSON text from the object.
let text = format!("\n{}", object);
assert_eq!(text, r#"
{
  "name": "Example",
  "counts": [
    1,
    2,
    3
  ],
  "config": {
    "enabled": true,
    "visible": false
  }
}"#);

Required Methods§

Source

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

Formats the value as JSON into the provided formatter.

This method is similar to Display::fmt(), but accepts a JsonFormatter which provides additional methods for JSON-specific formatting.

Implementations on Foreign Types§

Source§

impl DisplayJson for IpAddr

Source§

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

Source§

impl DisplayJson for SocketAddr

Source§

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

Source§

impl DisplayJson for bool

Source§

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

Source§

impl DisplayJson for char

Source§

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

Source§

impl DisplayJson for f32

Source§

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

Source§

impl DisplayJson for f64

Source§

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

Source§

impl DisplayJson for i8

Source§

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

Source§

impl DisplayJson for i16

Source§

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

Source§

impl DisplayJson for i32

Source§

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

Source§

impl DisplayJson for i64

Source§

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

Source§

impl DisplayJson for i128

Source§

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

Source§

impl DisplayJson for isize

Source§

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

Source§

impl DisplayJson for str

Source§

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

Source§

impl DisplayJson for u8

Source§

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

Source§

impl DisplayJson for u16

Source§

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

Source§

impl DisplayJson for u32

Source§

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

Source§

impl DisplayJson for u64

Source§

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

Source§

impl DisplayJson for u128

Source§

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

Source§

impl DisplayJson for ()

Source§

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

Source§

impl DisplayJson for usize

Source§

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

Source§

impl DisplayJson for String

Source§

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

Source§

impl DisplayJson for Ipv4Addr

Source§

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

Source§

impl DisplayJson for Ipv6Addr

Source§

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

Source§

impl DisplayJson for SocketAddrV4

Source§

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

Source§

impl DisplayJson for SocketAddrV6

Source§

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

Source§

impl DisplayJson for Path

Source§

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

Source§

impl DisplayJson for PathBuf

Source§

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

Source§

impl DisplayJson for NonZeroI8

Source§

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

Source§

impl DisplayJson for NonZeroI16

Source§

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

Source§

impl DisplayJson for NonZeroI32

Source§

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

Source§

impl DisplayJson for NonZeroI64

Source§

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

Source§

impl DisplayJson for NonZeroI128

Source§

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

Source§

impl DisplayJson for NonZeroIsize

Source§

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

Source§

impl DisplayJson for NonZeroU8

Source§

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

Source§

impl DisplayJson for NonZeroU16

Source§

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

Source§

impl DisplayJson for NonZeroU32

Source§

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

Source§

impl DisplayJson for NonZeroU64

Source§

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

Source§

impl DisplayJson for NonZeroU128

Source§

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

Source§

impl DisplayJson for NonZeroUsize

Source§

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

Source§

impl<'a, T: ?Sized + DisplayJson + ToOwned> DisplayJson for Cow<'a, T>

Source§

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

Source§

impl<K: Display, V: DisplayJson> DisplayJson for BTreeMap<K, V>

Source§

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

Source§

impl<K: Display, V: DisplayJson> DisplayJson for HashMap<K, V>

Source§

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

Source§

impl<T: DisplayJson + ?Sized> DisplayJson for &T

Source§

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

Source§

impl<T: DisplayJson + ?Sized> DisplayJson for &mut T

Source§

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

Source§

impl<T: DisplayJson + ?Sized> DisplayJson for Box<T>

Source§

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

Source§

impl<T: DisplayJson + ?Sized> DisplayJson for Rc<T>

Source§

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

Source§

impl<T: DisplayJson + ?Sized> DisplayJson for Arc<T>

Source§

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

Source§

impl<T: DisplayJson> DisplayJson for Option<T>

Source§

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

Source§

impl<T: DisplayJson> DisplayJson for [T]

Source§

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

Source§

impl<T: DisplayJson> DisplayJson for BTreeSet<T>

Source§

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

Source§

impl<T: DisplayJson> DisplayJson for VecDeque<T>

Source§

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

Source§

impl<T: DisplayJson> DisplayJson for Vec<T>

Source§

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

Source§

impl<T: DisplayJson> DisplayJson for HashSet<T>

Source§

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

Source§

impl<T: DisplayJson, const N: usize> DisplayJson for [T; N]

Source§

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

Implementors§