Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to differentiate None and Some(Value::Null) #1143

Closed
dariocurr opened this issue Jun 19, 2024 · 3 comments
Closed

Unable to differentiate None and Some(Value::Null) #1143

dariocurr opened this issue Jun 19, 2024 · 3 comments

Comments

@dariocurr
Copy link

dariocurr commented Jun 19, 2024

As the following example shows, I am unable to differentiate between two different cases:

  • when a null value is explicitly specified
  • when a value that could be a Value is omitted
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct MyStruct {
    value: Option<serde_json::Value>
}


fn main() {
    
    let my1: MyStruct =serde_json::from_str("{}").unwrap();
    assert_eq!(my1, MyStruct {value: None}); // OK

    let my2: MyStruct = serde_json::from_str("{\"value\": null}").unwrap();
    assert_eq!(my2, MyStruct {value: Some(serde_json::Value::Null)}); // Error
    
    
}

This happens because null is deserialized as None instead of Some(Value::Null)

@dariocurr dariocurr changed the title Unable to differentiate Noneand Some(Value::Null Unable to differentiate Noneand Some(Value::Null) Jun 19, 2024
@dariocurr dariocurr changed the title Unable to differentiate Noneand Some(Value::Null) Unable to differentiate None and Some(Value::Null) Jun 23, 2024
@dariocurr
Copy link
Author

Any news?

@dtolnay
Copy link
Member

dtolnay commented Jul 23, 2024

Hi @dariocurr, sorry that no one was able to provide guidance here. You could try taking this question to any of the resources shown in https://www.rust-lang.org/community, and someone will be able to help you get this working in the way you'd like.

@dtolnay dtolnay closed this as completed Jul 23, 2024
@dariocurr
Copy link
Author

IMHO this is a bug. Anyway, thank you, I'll find a solution 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants