Serialization/deserialization with serde-str
So, I've finally gotten to seriously testing this feature in a project (I'm not exactly sure what I'm doing different from when I was giving a test last time, but something's off), here's an example:
use fixed::types::I32F32 as Dec;
use fixed_macro::types::I32F32 as dec;
use serde_tuple::{Serialize_tuple, Deserialize_tuple};
use serde::{Serialize, Deserialize};
#[derive(Deserialize)]
struct Empty {}
#[derive(Copy, Clone, Debug, Serialize_tuple, Deserialize_tuple)]
struct A { x: Dec, y: Dec }
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
struct B { x: Dec, y: Dec }
fn main() {
println!("{}", serde_json::to_string(&dec!(-2.34)).unwrap());
println!("{:?}", serde_json::from_str::<Dec>(r#""-2.34""#));
let a = A { x: dec!(1.23), y: dec!(-2.34) };
println!("{:?}", a);
println!("{}", serde_json::to_string(&a).unwrap());
let b = B { x: dec!(1.23), y: dec!(-2.34) };
println!("{:?}", b);
println!("{}", serde_json::to_string(&b).unwrap());
println!("{:?}", serde_json::from_str::<A>(r#"["1.23","-2.34"]"#));
println!("{:?}", serde_json::from_str::<B>(r#"{"x":"1.23","y":"-2.34"}"#));
}
This yields:
{"value":"-2.34"}
Err(Error("invalid type: string \"-2.34\", expected struct FixedI64", line: 1, column: 7))
A { x: 1.23, y: -2.34 }
[{"value":"1.23"},{"value":"-2.34"}]
B { x: 1.23, y: -2.34 }
{"x":{"value":"1.23"},"y":{"value":"-2.34"}}
Err(Error("invalid type: string \"1.23\", expected struct FixedI64", line: 1, column: 7))
Err(Error("invalid type: string \"1.23\", expected struct FixedI64", line: 1, column: 11))
Hmmm.. am I missing something obvious or is it actually impossible to get it to emit and accept just strings, like "-2.34"
? (because that was really the whole point)