Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/serde_json/tests/regression/issue845.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / serde_json / tests / regression / issue845.rs` 74 lines ยท 1919 bytes Raw Blame History 1 #![allow(clippy::trait_duplication_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/8757 2 3 use serde::{Deserialize, Deserializer}; 4 use std::fmt::{self, Display}; 5 use std::marker::PhantomData; 6 use std::str::FromStr; 7 8 pub struct NumberVisitor { 9 marker: PhantomData, 10 } 11 12 impl<'de, T> serde::de::Visitor<'de> for NumberVisitor 13 where 14 T: TryFrom + TryFrom + FromStr, 15 >::Error: Display, 16 >::Error: Display, 17 ::Err: Display, 18 { 19 type Value = T; 20 21 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 22 formatter.write_str("an integer or string") 23 } 24 25 fn visit_u64(self, v: u64) -> Result 26 where 27 E: serde::de::Error, 28 { 29 T::try_from(v).map_err(serde::de::Error::custom) 30 } 31 32 fn visit_i64(self, v: i64) -> Result 33 where 34 E: serde::de::Error, 35 { 36 T::try_from(v).map_err(serde::de::Error::custom) 37 } 38 39 fn visit_str(self, v: &str) -> Result 40 where 41 E: serde::de::Error, 42 { 43 v.parse().map_err(serde::de::Error::custom) 44 } 45 } 46 47 fn deserialize_integer_or_string<'de, D, T>(deserializer: D) -> Result 48 where 49 D: Deserializer<'de>, 50 T: TryFrom + TryFrom + FromStr, 51 >::Error: Display, 52 >::Error: Display, 53 ::Err: Display, 54 { 55 deserializer.deserialize_any(NumberVisitor { 56 marker: PhantomData, 57 }) 58 } 59 60 #[derive(Deserialize, Debug)] 61 pub struct Struct { 62 #[serde(deserialize_with = "deserialize_integer_or_string")] 63 #[allow(dead_code)] 64 pub i: i64, 65 } 66 67 #[test] 68 fn test() { 69 let j = r#" {"i":100} "#; 70 println!("{:?}", serde_json::from_str::(j).unwrap()); 71 72 let j = r#" {"i":"100"} "#; 73 println!("{:?}", serde_json::from_str::(j).unwrap()); 74 }