1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
use crate::lib::{json, Value};
use crate::internal::{type_name};
///
pub fn x_to_string(v: &str) -> Value {
json!(v)
}
#[doc(hidden)]
pub fn json_array_to_string_x(vec: Vec<Value>) -> String {
let mut iter = vec.into_iter();
match iter.next() {
Some(v) => {
let mut s = "".to_owned();
if v.is_null() {
s.push_str("null");
} else {
s.push_str(&*to_string_x(v));
}
for v in iter {
s.push(',');
if v.is_null() {
s.push_str("null");
} else {
s.push_str(&*to_string_x(v));
}
}
s
}
None => "".into(),
}
}
///
pub fn to_string_x(v: Value) -> String {
match v {
Value::Null => "".into(),
Value::Bool(b) => {
if b {
"true".into()
} else {
"false".into()
}
}
Value::Number(n) => n.to_string(),
Value::String(s) => s,
Value::Array(vec) => json_array_to_string_x(vec),
Value::Object(o) => type_name(&o).into(), // I don't think put [object Object] here is a good idea, so...
}
}
/// See lodash [toString](https://lodash.com/docs/#toString)
pub fn to_string(v: Value) -> Value {
Value::String(to_string_x(v))
}
/// Based on [x_to_string()]
#[macro_export]
macro_rules! x_to_string {
() => {
json!("")
};
($a:expr $(,)*) => {
$crate::x_to_string($a)
};
($a:expr, $($rest:tt)*) => {
$crate::x_to_string($a)
};
}
/// Based on [to_string_x()]
///
/// Examples:
///
/// ```rust
/// #[macro_use] extern crate serde_json_lodash;
/// use serde_json::json;
/// assert_eq!(
/// to_string_x!(json!(null)),
/// "".to_owned()
/// );
/// assert_eq!(
/// to_string_x!(json!(-0)),
/// "0".to_owned() // In rust world, -0 is 0
/// );
/// assert_eq!(
/// to_string_x!(json!([1, 2, 3])),
/// "1,2,3".to_owned()
/// );
/// ```
///
/// More examples:
///
/// ```rust
/// # #[macro_use] extern crate serde_json_lodash;
/// # use serde_json::json;
/// assert_eq!(to_string_x!(), "".to_owned());
/// ```
#[macro_export]
macro_rules! to_string_x {
() => {
"".to_owned()
};
($a:expr $(,)*) => {
$crate::to_string_x($a)
};
($a:expr, $($rest:tt)*) => {
$crate::to_string_x($a)
};
}
/// Based on [to_string()]
///
/// Examples:
///
/// ```rust
/// #[macro_use] extern crate serde_json_lodash;
/// use serde_json::json;
///
/// assert_eq!(
/// to_string!(json!(null)),
/// json!("")
/// );
/// assert_eq!(
/// to_string!(json!(-0)),
/// json!("0") // In rust world, -0 is 0
/// );
/// assert_eq!(
/// to_string!(json!([1, 2, 3])),
/// json!("1,2,3")
/// );
/// ```
///
/// More examples:
///
/// ```rust
/// # #[macro_use] extern crate serde_json_lodash;
/// # use serde_json::json;
/// assert_eq!(to_string!(), json!(""));
/// assert_eq!(to_string!(json!(null)), json!(""));
/// assert_eq!(to_string!(json!(false)), json!("false"));
/// assert_eq!(to_string!(json!(-0)), json!("0")); // rust world -0 is 0
/// assert_eq!(to_string!(json!("")), json!(""));
/// assert_eq!(to_string!(json!([])), json!(""));
/// assert_eq!(to_string!(json!([null,"A",{}])), json!("null,A,serde_json::map::Map<alloc::string::String, serde_json::value::Value>"));
/// assert_eq!(to_string!(json!({})), json!("serde_json::map::Map<alloc::string::String, serde_json::value::Value>"));
/// ```
#[macro_export]
macro_rules! to_string {
() => {
json!("")
};
($a:expr $(,)*) => {
$crate::to_string($a)
};
($a:expr, $($rest:tt)*) => {
$crate::to_string($a)
};
}