Skip to main content

ToJsonKey

Trait ToJsonKey 

Source
pub trait ToJsonKey {
    // Required method
    fn to_json_key(&self) -> Cow<'_, str>;
}
Expand description

A type usable as a JSON object key, for ToJson on maps.

§Examples

use std::borrow::Cow;
use std::collections::BTreeMap;
use jsonx_lib::ToJsonKey;

#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct Uri(String);

impl ToJsonKey for Uri {
    fn to_json_key(&self) -> Cow<'_, str> {
        Cow::Borrowed(&self.0)
    }
}

let mut changes = BTreeMap::new();
changes.insert(Uri("file:///a.rs".into()), vec![1, 2]);
assert_eq!(jsonx_lib::to_string(&changes), r#"{"file:///a.rs":[1,2]}"#);

Required Methods§

Source

fn to_json_key(&self) -> Cow<'_, str>

The key’s text (escaped by the writer).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ToJsonKey for Box<str>

Source§

fn to_json_key(&self) -> Cow<'_, str>

Source§

impl ToJsonKey for Cow<'_, str>

Source§

fn to_json_key(&self) -> Cow<'_, str>

Source§

impl ToJsonKey for String

Source§

fn to_json_key(&self) -> Cow<'_, str>

Source§

impl ToJsonKey for char

Source§

fn to_json_key(&self) -> Cow<'_, str>

Source§

impl ToJsonKey for str

Source§

fn to_json_key(&self) -> Cow<'_, str>

Source§

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

Source§

fn to_json_key(&self) -> Cow<'_, str>

Implementors§