macro_rules! json_typed { (owned, $($json:tt)+) => { ... }; (borrowed, $($json:tt)+) => { ... }; }
Expand description
Constructs a simd_json::Value
from a JSON literal and allows specifying whether it generates
an owned or borrowed variant.
Adapted from: https://github.com/serde-rs/json/blob/5b5f95831d9e0d769367b30b76a686339bffd209/src/macros.rs
Create an owned value of the form:
let value: OwnedValue = json_typed!(owned, {
"code": 200,
"success": true,
"payload": {
"features": [
"serde",
"json"
]
}
});
Create a borrowed value of the form:
let value: BorrowedValue = json_typed!(borrowed, {
"code": 200,
"success": true,
"payload": {
"features": [
"serde",
"json"
]
}
});
Variables or expressions can be interpolated into the JSON literal. Any type
interpolated into an array element or object value must implement Serde’s
Serialize
trait, while any type interpolated into a object key must
implement Into<String>
. If the Serialize
implementation of the
interpolated type decides to fail, or if the interpolated type contains a
map with non-string keys, the json!
macro will panic.
let code = 200;
let features = vec!["serde", "json"];
let value = json_typed!(owned, {
"code": code,
"success": code == 200,
"payload": {
features[0]: features[1]
}
});
Trailing commas are allowed inside both arrays and objects.
let value = json_typed!(borrowed, [
"notice",
"the",
"trailing",
"comma -->",
]);
It works for both Borrowed and owned values natively without the
need for calling into()
unlike the json!
macro which supports
owned values only.
let owned_value: OwnedValue = json_typed!(owned, {
"code": 200,
"success": true,
"payload": {
"features": [
"serde",
"json"
]
}
});
let borrowed_value: BorrowedValue = json_typed!(borrowed, {
"code": 200,
"success": true,
"payload": {
"features": [
"serde",
"json"
]
},
"empty_obj": {},
"empty_array": [],
}).into();