simd-lite / simd-json-derive

high performance Serialize and Deserialize derives
Apache License 2.0
30 stars 7 forks source link

Result implementation doesn't match serde's #38

Closed julianbraha closed 1 year ago

julianbraha commented 1 year ago

Hi, thanks for getting to issue #35 so quickly! However, I'm having some problems with the changes introduced in PR #36. I've created a minimal example to illustrate:

This works fine, using serde::Deserialize:

use serde::Deserialize;
#[derive(serde::Deserialize)]
struct myFirstStruct {
    status_ok: Result<(), i32>,
    status_err: Result<(), i32>
}

fn main() {
    let mut json = String::from("{\"status_ok\":{\"Ok\": null},\"status_err\":{\"Err\": 7}}");

    let myfirststruct: myFirstStruct = serde_json::from_str(&mut json).unwrap();

    println!("{:?}", myfirststruct.status_ok);
    println!("{:?}", myfirststruct.status_err);
}

But when using simd_json_derive::Deserialize, this gives a runtime error:

use simd_json_derive::Deserialize;
#[derive(simd_json_derive::Deserialize)] 
struct myFirstStruct {
    status_ok: Result<(), i32>,
    status_err: Result<(), i32>
}

fn main() {
    let mut json = String::from("{\"status_ok\":{\"Ok\": null},\"status_err\":{\"Err\": 7}}");

    let myfirststruct = myFirstStruct::from_str(&mut json).unwrap();

    println!("{:?}", myfirststruct.status_ok);
    println!("{:?}", myfirststruct.status_err);
}

Here's the error it leads to:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { index: 0, character: None, error: ExpectedMap }', src/main.rs:11:60
Licenser commented 1 year ago

it serialises / deserializes from "ok" and "err" not "Ok" and "Err" (Which is probably wrong but right at the same time, I'll make another change to allow both versions)

Licenser commented 1 year ago

It'll default to uppercase for encoding since that's more in line with the behaviour of other enums, but I'll leave the code to also decode "ok" and "err" in addition to "Ok" and "Err".

Good catch 👍

Licenser commented 1 year ago

GH seems to have issues right now this will be 0.7.2 when the servers calm down :)