From 70f30d88f13eb3e71f1a5c57dc8d11bc1710c8eb Mon Sep 17 00:00:00 2001 From: waruto Date: Tue, 17 Jan 2023 20:24:55 +0800 Subject: [PATCH] add key-to-lowercase for borrowed value --- src/value/borrowed.rs | 13 +++++++++++++ src/value/owned.rs | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/value/borrowed.rs b/src/value/borrowed.rs index 5b379599..48ba36b9 100644 --- a/src/value/borrowed.rs +++ b/src/value/borrowed.rs @@ -425,6 +425,9 @@ impl<'de> BorrowDeserializer<'de> { // element so we eat this for _ in 0..len { if let Node::String(key) = unsafe { self.0.next_() } { + #[cfg(feature = "key-to-lowercase")] + let key = key.to_ascii_lowercase(); + #[cfg(not(feature = "value-no-dup-keys"))] res.insert_nocheck(key.into(), self.parse()); #[cfg(feature = "value-no-dup-keys")] @@ -1051,4 +1054,14 @@ mod test { let v: Value = v.into(); assert_eq!(v, 42); } + + #[cfg(feature = "key-to-lowercase")] + #[test] + fn test_key_to_lowercase() { + let mut data = br#"{"A": 1}"#.to_vec(); + let ret = crate::to_borrowed_value(&mut data); + assert!(ret.is_ok()); + let v = ret.unwrap(); + assert_eq!(v.get_i32("a"), Some(1)); + } } diff --git a/src/value/owned.rs b/src/value/owned.rs index b5023402..5e759360 100644 --- a/src/value/owned.rs +++ b/src/value/owned.rs @@ -363,14 +363,12 @@ impl<'de> OwnedDeserializer<'de> { for _ in 0..len { if let Node::String(key) = unsafe { self.de.next_() } { #[cfg(feature = "key-to-lowercase")] - let k = key.to_ascii_lowercase(); - #[cfg(not(feature = "key-to-lowercase"))] - let k = key.to_string(); + let key = key.to_ascii_lowercase(); #[cfg(not(feature = "value-no-dup-keys"))] - res.insert_nocheck(k, self.parse()); + res.insert_nocheck(key.into(), self.parse()); #[cfg(feature = "value-no-dup-keys")] - res.insert(k, self.parse()); + res.insert(key.into(), self.parse()); } else { unreachable!(); } @@ -978,4 +976,14 @@ mod test { let v: Value = v.into(); assert_eq!(v, 42); } + + #[cfg(feature = "key-to-lowercase")] + #[test] + fn test_key_to_lowercase() { + let mut data = br#"{"A": 1}"#.to_vec(); + let ret = crate::to_owned_value(&mut data); + assert!(ret.is_ok()); + let v = ret.unwrap(); + assert_eq!(v.get_i32("a"), Some(1)); + } }