3 ms·
I don't know about other languages but this is perfectly fine in Rust. It is actually the default formatting for enums (unions) and does streaming parsing.
by kevincox 5y ago
I don't know about other languages but this is perfectly fine in Rust. It is actually the default formatting for enums (unions) and does streaming parsing.
#[derive(Debug,serde::Deserialize)]
struct MyMessage {
data: i32,
}
#[derive(Debug,serde::Deserialize)]
enum Example {
MyMessageType(MyMessage)
}
fn main() {
dbg!(serde_json::from_str::<Example>(r#"{"MyMessageType":{"data":5}}"#));
}
Running:
[src/main.rs:12] serde_json::from_str::<Example>(r#"{"MyMessageType":{"data":5}}"#) = Ok(
MyMessageType(
MyMessage {
data: 5,
},
),
)
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cc01d0de9934266ba4b8fb6eb123b96c https://play.rust-lang.org/?version=stable&mode=debug&editio...
I'm not sure why you are forced to read it into a Map<String, JsonNode> unless you are doing your parsing without type information in which case any solution is going to require a map.