SerJson generates trailing "," when serializing struct with Option
marot opened this issue · comments
Here is a test to demonstrate the issue:
#[derive(SerJson)]
struct TestStruct {
a: Option<String>,
b: Option<String>,
}
#[test]
fn test_serialization() {
let test_struct = TestStruct {
a: Some("string".to_owned()),
b: None,
};
let test_struct2 = TestStruct {
a: None,
b: Some("string".to_owned()),
};
let result = test_struct.serialize_json();
let result2 = test_struct2.serialize_json();
// This one is correct
assert_eq!(result2, "{\"b\":\"string\"}");
// This one has an additional trailing ","
assert_eq!(result, "{\"a\":\"string\"}");
}
I think the problem is that in derive_ser_json_struct
the index of the last item is determined using
let last = struct_.fields.len() - 1;
but when options are involved, the last index might be lower.