update serde_macros from origin
This commit is contained in:
committed by
Martin Algesten
parent
10fb4a1649
commit
a6f64188dc
@@ -11,11 +11,9 @@
|
|||||||
|
|
||||||
/// Construct a `serde_json::Value` from a JSON literal.
|
/// Construct a `serde_json::Value` from a JSON literal.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```edition2018
|
||||||
/// # #[macro_use]
|
/// # use serde_json::json;
|
||||||
/// # extern crate serde_json;
|
|
||||||
/// #
|
/// #
|
||||||
/// # fn main() {
|
|
||||||
/// let value = json!({
|
/// let value = json!({
|
||||||
/// "code": 200,
|
/// "code": 200,
|
||||||
/// "success": true,
|
/// "success": true,
|
||||||
@@ -26,7 +24,6 @@
|
|||||||
/// ]
|
/// ]
|
||||||
/// }
|
/// }
|
||||||
/// });
|
/// });
|
||||||
/// # }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Variables or expressions can be interpolated into the JSON literal. Any type
|
/// Variables or expressions can be interpolated into the JSON literal. Any type
|
||||||
@@ -36,40 +33,34 @@
|
|||||||
/// interpolated type decides to fail, or if the interpolated type contains a
|
/// interpolated type decides to fail, or if the interpolated type contains a
|
||||||
/// map with non-string keys, the `json!` macro will panic.
|
/// map with non-string keys, the `json!` macro will panic.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```edition2018
|
||||||
/// # #[macro_use]
|
/// # use serde_json::json;
|
||||||
/// # extern crate serde_json;
|
|
||||||
/// #
|
/// #
|
||||||
/// # fn main() {
|
|
||||||
/// let code = 200;
|
/// let code = 200;
|
||||||
/// let features = vec!["serde", "json"];
|
/// let features = vec!["serde", "json"];
|
||||||
///
|
///
|
||||||
/// let value = json!({
|
/// let value = json!({
|
||||||
/// "code": code,
|
/// "code": code,
|
||||||
/// "success": code == 200,
|
/// "success": code == 200,
|
||||||
/// "payload": {
|
/// "payload": {
|
||||||
/// features[0]: features[1]
|
/// features[0]: features[1]
|
||||||
/// }
|
/// }
|
||||||
/// });
|
/// });
|
||||||
/// # }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Trailing commas are allowed inside both arrays and objects.
|
/// Trailing commas are allowed inside both arrays and objects.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```edition2018
|
||||||
/// # #[macro_use]
|
/// # use serde_json::json;
|
||||||
/// # extern crate serde_json;
|
|
||||||
/// #
|
/// #
|
||||||
/// # fn main() {
|
|
||||||
/// let value = json!([
|
/// let value = json!([
|
||||||
/// "notice",
|
/// "notice",
|
||||||
/// "the",
|
/// "the",
|
||||||
/// "trailing",
|
/// "trailing",
|
||||||
/// "comma -->",
|
/// "comma -->",
|
||||||
/// ]);
|
/// ]);
|
||||||
/// # }
|
|
||||||
/// ```
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export(local_inner_macros)]
|
||||||
macro_rules! json {
|
macro_rules! json {
|
||||||
// Hide distracting implementation details from the generated rustdoc.
|
// Hide distracting implementation details from the generated rustdoc.
|
||||||
($($json:tt)+) => {
|
($($json:tt)+) => {
|
||||||
@@ -84,7 +75,7 @@ macro_rules! json {
|
|||||||
//
|
//
|
||||||
// Changes are fine as long as `json_internal!` does not call any new helper
|
// Changes are fine as long as `json_internal!` does not call any new helper
|
||||||
// macros and can still be invoked as `json_internal!($($json)+)`.
|
// macros and can still be invoked as `json_internal!($($json)+)`.
|
||||||
#[macro_export]
|
#[macro_export(local_inner_macros)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
macro_rules! json_internal {
|
macro_rules! json_internal {
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@@ -96,12 +87,12 @@ macro_rules! json_internal {
|
|||||||
|
|
||||||
// Done with trailing comma.
|
// Done with trailing comma.
|
||||||
(@array [$($elems:expr,)*]) => {
|
(@array [$($elems:expr,)*]) => {
|
||||||
vec![$($elems,)*]
|
json_internal_vec![$($elems,)*]
|
||||||
};
|
};
|
||||||
|
|
||||||
// Done without trailing comma.
|
// Done without trailing comma.
|
||||||
(@array [$($elems:expr),*]) => {
|
(@array [$($elems:expr),*]) => {
|
||||||
vec![$($elems),*]
|
json_internal_vec![$($elems),*]
|
||||||
};
|
};
|
||||||
|
|
||||||
// Next element is `null`.
|
// Next element is `null`.
|
||||||
@@ -144,6 +135,11 @@ macro_rules! json_internal {
|
|||||||
json_internal!(@array [$($elems,)*] $($rest)*)
|
json_internal!(@array [$($elems,)*] $($rest)*)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Unexpected token after most recent element.
|
||||||
|
(@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
|
||||||
|
json_unexpected!($unexpected)
|
||||||
|
};
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// TT muncher for parsing the inside of an object {...}. Each entry is
|
// TT muncher for parsing the inside of an object {...}. Each entry is
|
||||||
// inserted into the given map variable.
|
// inserted into the given map variable.
|
||||||
@@ -159,13 +155,18 @@ macro_rules! json_internal {
|
|||||||
|
|
||||||
// Insert the current entry followed by trailing comma.
|
// Insert the current entry followed by trailing comma.
|
||||||
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
|
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
|
||||||
$object.insert(($($key)+).into(), $value);
|
let _ = $object.insert(($($key)+).into(), $value);
|
||||||
json_internal!(@object $object () ($($rest)*) ($($rest)*));
|
json_internal!(@object $object () ($($rest)*) ($($rest)*));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Current entry followed by unexpected token.
|
||||||
|
(@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
|
||||||
|
json_unexpected!($unexpected);
|
||||||
|
};
|
||||||
|
|
||||||
// Insert the last entry without trailing comma.
|
// Insert the last entry without trailing comma.
|
||||||
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
|
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
|
||||||
$object.insert(($($key)+).into(), $value);
|
let _ = $object.insert(($($key)+).into(), $value);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Next value is `null`.
|
// Next value is `null`.
|
||||||
@@ -219,13 +220,13 @@ macro_rules! json_internal {
|
|||||||
// Misplaced colon. Trigger a reasonable error message.
|
// Misplaced colon. Trigger a reasonable error message.
|
||||||
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
|
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
|
||||||
// Takes no arguments so "no rules expected the token `:`".
|
// Takes no arguments so "no rules expected the token `:`".
|
||||||
unimplemented!($colon);
|
json_unexpected!($colon);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Found a comma inside a key. Trigger a reasonable error message.
|
// Found a comma inside a key. Trigger a reasonable error message.
|
||||||
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
|
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
|
||||||
// Takes no arguments so "no rules expected the token `,`".
|
// Takes no arguments so "no rules expected the token `,`".
|
||||||
unimplemented!($comma);
|
json_unexpected!($comma);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Key is fully parenthesized. This avoids clippy double_parens false
|
// Key is fully parenthesized. This avoids clippy double_parens false
|
||||||
@@ -258,7 +259,7 @@ macro_rules! json_internal {
|
|||||||
};
|
};
|
||||||
|
|
||||||
([]) => {
|
([]) => {
|
||||||
$crate::SerdeValue::Array(vec![])
|
$crate::SerdeValue::Array(json_internal_vec![])
|
||||||
};
|
};
|
||||||
|
|
||||||
([ $($tt:tt)+ ]) => {
|
([ $($tt:tt)+ ]) => {
|
||||||
@@ -283,3 +284,20 @@ macro_rules! json_internal {
|
|||||||
$crate::serde_to_value(&$other).unwrap()
|
$crate::serde_to_value(&$other).unwrap()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The json_internal macro above cannot invoke vec directly because it uses
|
||||||
|
// local_inner_macros. A vec invocation there would resolve to $crate::vec.
|
||||||
|
// Instead invoke vec here outside of local_inner_macros.
|
||||||
|
#[macro_export]
|
||||||
|
#[doc(hidden)]
|
||||||
|
macro_rules! json_internal_vec {
|
||||||
|
($($content:tt)*) => {
|
||||||
|
vec![$($content)*]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
#[doc(hidden)]
|
||||||
|
macro_rules! json_unexpected {
|
||||||
|
() => {};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user