#[macro_export]
macro_rules! json {
($($json:tt)+) => {
json_internal!($($json)+)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! json_internal {
(null) => {
$crate::Value::Null
};
(true) => {
$crate::Value::Bool(true)
};
(false) => {
$crate::Value::Bool(false)
};
([]) => {
$crate::Value::Array(vec![])
};
([ $($tt:tt)+ ]) => {
$crate::Value::Array(json_within_array!([] $($tt)+))
};
({}) => {
$crate::Value::Object($crate::Map::new())
};
({ $($tt:tt)+ }) => {
$crate::Value::Object({
let mut object = $crate::Map::new();
json_within_object!(object () $($tt)+);
object
})
};
($other:expr) => {
$crate::to_value(&$other).unwrap()
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! json_within_array {
([$($elems:expr,)*]) => {
vec![$($elems,)*]
};
([$($elems:expr),*]) => {
vec![$($elems),*]
};
([$($elems:expr,)*] null $($rest:tt)*) => {
json_within_array!([$($elems,)* json!(null)] $($rest)*)
};
([$($elems:expr,)*] true $($rest:tt)*) => {
json_within_array!([$($elems,)* json!(true)] $($rest)*)
};
([$($elems:expr,)*] false $($rest:tt)*) => {
json_within_array!([$($elems,)* json!(false)] $($rest)*)
};
([$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
json_within_array!([$($elems,)* json!([$($array)*])] $($rest)*)
};
([$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
json_within_array!([$($elems,)* json!({$($map)*})] $($rest)*)
};
([$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
json_within_array!([$($elems,)* json!($next),] $($rest)*)
};
([$($elems:expr,)*] $last:expr) => {
json_within_array!([$($elems,)* json!($last)])
};
([$($elems:expr),*] , $($rest:tt)*) => {
json_within_array!([$($elems,)*] $($rest)*)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! json_within_object {
($object:ident ()) => {};
($object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
$object.insert(($($key)+).into(), $value);
json_within_object!($object () $($rest)*);
};
($object:ident [$($key:tt)+] ($value:expr)) => {
$object.insert(($($key)+).into(), $value);
};
($object:ident ($($key:tt)+) : null $($rest:tt)*) => {
json_within_object!($object [$($key)+] (json!(null)) $($rest)*);
};
($object:ident ($($key:tt)+) : true $($rest:tt)*) => {
json_within_object!($object [$($key)+] (json!(true)) $($rest)*);
};
($object:ident ($($key:tt)+) : false $($rest:tt)*) => {
json_within_object!($object [$($key)+] (json!(false)) $($rest)*);
};
($object:ident ($($key:tt)+) : [$($array:tt)*] $($rest:tt)*) => {
json_within_object!($object [$($key)+] (json!([$($array)*])) $($rest)*);
};
($object:ident ($($key:tt)+) : {$($map:tt)*} $($rest:tt)*) => {
json_within_object!($object [$($key)+] (json!({$($map)*})) $($rest)*);
};
($object:ident ($($key:tt)+) : $value:expr , $($rest:tt)*) => {
json_within_object!($object [$($key)+] (json!($value)) , $($rest)*);
};
($object:ident ($($key:tt)+) : $value:expr) => {
json_within_object!($object [$($key)+] (json!($value)));
};
($object:ident () : $($rest:tt)*) => {
json_within_object!($object [:] (:));
};
($object:ident ($($key:tt)*) , $($rest:tt)*) => {
json_within_object!($object [,] (,));
};
($object:ident ($($key:tt)*) $tt:tt $($rest:tt)*) => {
json_within_object!($object ($($key)* $tt) $($rest)*);
};
}