mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-14 03:31:02 +00:00
s/disallow_unknown/deny_unknown_fields/
This commit is contained in:
@@ -600,7 +600,7 @@ Structure Annotations:
|
|||||||
|
|
||||||
| Annotation | Function |
|
| Annotation | Function |
|
||||||
| ---------- | -------- |
|
| ---------- | -------- |
|
||||||
| `#[serde(disallow_unknown)` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. |
|
| `#[serde(deny_unknown_fields)` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. |
|
||||||
|
|
||||||
|
|
||||||
Serialization Formats Using Serde
|
Serialization Formats Using Serde
|
||||||
|
|||||||
+10
-10
@@ -247,23 +247,23 @@ impl<'a> FieldAttrsBuilder<'a> {
|
|||||||
/// Represents container (e.g. struct) attribute information
|
/// Represents container (e.g. struct) attribute information
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ContainerAttrs {
|
pub struct ContainerAttrs {
|
||||||
disallow_unknown: bool,
|
deny_unknown_fields: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContainerAttrs {
|
impl ContainerAttrs {
|
||||||
pub fn disallow_unknown(&self) -> bool {
|
pub fn deny_unknown_fields(&self) -> bool {
|
||||||
self.disallow_unknown
|
self.deny_unknown_fields
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ContainerAttrsBuilder {
|
pub struct ContainerAttrsBuilder {
|
||||||
disallow_unknown: bool,
|
deny_unknown_fields: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContainerAttrsBuilder {
|
impl ContainerAttrsBuilder {
|
||||||
pub fn new() -> ContainerAttrsBuilder {
|
pub fn new() -> ContainerAttrsBuilder {
|
||||||
ContainerAttrsBuilder {
|
ContainerAttrsBuilder {
|
||||||
disallow_unknown: false,
|
deny_unknown_fields: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,8 +285,8 @@ impl ContainerAttrsBuilder {
|
|||||||
|
|
||||||
pub fn meta_item(self, meta_item: &P<ast::MetaItem>) -> ContainerAttrsBuilder {
|
pub fn meta_item(self, meta_item: &P<ast::MetaItem>) -> ContainerAttrsBuilder {
|
||||||
match meta_item.node {
|
match meta_item.node {
|
||||||
ast::MetaWord(ref name) if name == &"disallow_unknown" => {
|
ast::MetaWord(ref name) if name == &"deny_unknown_fields" => {
|
||||||
self.disallow_unknown()
|
self.deny_unknown_fields()
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Ignore unknown meta variables for now.
|
// Ignore unknown meta variables for now.
|
||||||
@@ -295,14 +295,14 @@ impl ContainerAttrsBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn disallow_unknown(mut self) -> ContainerAttrsBuilder {
|
pub fn deny_unknown_fields(mut self) -> ContainerAttrsBuilder {
|
||||||
self.disallow_unknown = true;
|
self.deny_unknown_fields = true;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> ContainerAttrs {
|
pub fn build(self) -> ContainerAttrs {
|
||||||
ContainerAttrs {
|
ContainerAttrs {
|
||||||
disallow_unknown: self.disallow_unknown,
|
deny_unknown_fields: self.deny_unknown_fields,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -567,7 +567,7 @@ fn deserialize_item_enum(
|
|||||||
const VARIANTS: &'static [&'static str] = $variants_expr;
|
const VARIANTS: &'static [&'static str] = $variants_expr;
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let ignored_arm = if !container_attrs.disallow_unknown() {
|
let ignored_arm = if !container_attrs.deny_unknown_fields() {
|
||||||
Some(quote_arm!(cx, __Field::__ignore => { Err(::serde::de::Error::end_of_stream()) }))
|
Some(quote_arm!(cx, __Field::__ignore => { Err(::serde::de::Error::end_of_stream()) }))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -800,7 +800,7 @@ fn deserialize_field_visitor(
|
|||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let ignore_variant = if !container_attrs.disallow_unknown() {
|
let ignore_variant = if !container_attrs.deny_unknown_fields() {
|
||||||
let skip_ident = builder.id("__ignore");
|
let skip_ident = builder.id("__ignore");
|
||||||
Some(builder.variant(skip_ident).unit())
|
Some(builder.variant(skip_ident).unit())
|
||||||
} else {
|
} else {
|
||||||
@@ -848,7 +848,7 @@ fn deserialize_field_visitor(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let fallthrough_arm_expr = if !container_attrs.disallow_unknown() {
|
let fallthrough_arm_expr = if !container_attrs.deny_unknown_fields() {
|
||||||
quote_expr!(cx, Ok(__Field::__ignore))
|
quote_expr!(cx, Ok(__Field::__ignore))
|
||||||
} else {
|
} else {
|
||||||
quote_expr!(cx, Err(::serde::de::Error::unknown_field(value)))
|
quote_expr!(cx, Err(::serde::de::Error::unknown_field(value)))
|
||||||
@@ -1012,7 +1012,7 @@ fn deserialize_map(
|
|||||||
|
|
||||||
|
|
||||||
// Visit ignored values to consume them
|
// Visit ignored values to consume them
|
||||||
let ignored_arm = if !container_attrs.disallow_unknown() {
|
let ignored_arm = if !container_attrs.deny_unknown_fields() {
|
||||||
Some(quote_arm!(cx,
|
Some(quote_arm!(cx,
|
||||||
_ => { try!(visitor.visit_value::<::serde::de::impls::IgnoredAny>()); }
|
_ => { try!(visitor.visit_value::<::serde::de::impls::IgnoredAny>()); }
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ struct Default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
#[serde(disallow_unknown)]
|
#[serde(deny_unknown_fields)]
|
||||||
struct DisallowUnknown {
|
struct DisallowUnknown {
|
||||||
a1: i32,
|
a1: i32,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user