#[non_exhaustive]pub enum DuplicateKeys {
Error,
First,
Last,
KeepAll,
}Expand description
What to do when an object names the same key twice.
RFC 8259 says names within an object SHOULD be unique and leaves the
behaviour for duplicates to the implementation; implementations disagree,
which makes duplicates a classic way to smuggle a different value past one
reader than another sees. The default is therefore to refuse them.
Keys are compared after unescaping, so "a" and "\u0061" are the same
key.
The enum is #[non_exhaustive]: a later version may add a policy.
§Examples
use jsonx_lib::{DuplicateKeys, ErrorKind, Parser};
let text = r#"{"a": 1, "b": 2, "a": 3}"#;
let err = Parser::new().parse(text).unwrap_err();
assert_eq!((err.kind(), err.offset()), (ErrorKind::DuplicateKey, 17));
let first = Parser::new().duplicate_keys(DuplicateKeys::First).parse(text)?;
assert_eq!(first.to_string(), r#"{"a":1,"b":2}"#);
let last = Parser::new().duplicate_keys(DuplicateKeys::Last).parse(text)?;
assert_eq!(last.to_string(), r#"{"a":3,"b":2}"#);
let all = Parser::new().duplicate_keys(DuplicateKeys::KeepAll).parse(text)?;
assert_eq!(all.to_string(), r#"{"a":1,"b":2,"a":3}"#);Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Error
Refuse the document with ErrorKind::DuplicateKey at the second
occurrence’s opening quote. The default.
First
Keep the first occurrence and its value; drop the later ones.
Last
Keep the last occurrence’s value, at the first occurrence’s position, as inserting each member into an ordered map would.
KeepAll
Keep every member as written. The writer reproduces the duplicates.