How can I force serde to parse a JSON Number as a Rust u16 type?
Below I parse a JSON file. json_data is of type Value. This only has a as_u64() method, no as_u16(). As a result I first have to parse the JSON Number as a u64, then try and parse that as u16. This seems fairly convoluted. Is there some way I can force serde_json to parse this value as a u16 to avoid this?
(As a side note, the JSON data has already been validated using a JSON schema, which checks the value of x is within the bounds of u16).
$ cat test.json
{
"x": 100
}
use serde_json::Value;
use std::fs::File;
use std::io::BufReader;
pub struct Test {
pub x: u16,
}
fn main() {
let filename = "test.json";
let file =
File::open(filename).unwrap_or_else(|e| panic!("Unable to open file {}: {}", filename, e));
let reader = BufReader::new(file);
let json_data: Value = serde_json::from_reader(reader)
.unwrap_or_else(|e| panic!("Unable to parse JSON file {}: {}", filename, e));
assert!(json_data["x"].is_u64());
let mut t = Test { x: 5 };
if json_data["x"].is_u64() {
let _new_x: u64 = json_data["x"].as_u64().expect("Unable to parse x");
t.x = u16::try_from(_new_x).expect("Unable to convert x to u16");
}
}