Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Integers get serialized as floats by to_value #98

Closed
btv opened this issue Aug 22, 2018 · 4 comments
Closed

Integers get serialized as floats by to_value #98

btv opened this issue Aug 22, 2018 · 4 comments

Comments

@btv
Copy link

btv commented Aug 22, 2018

Hi, I'm not sure what I'm doing wrong there, but I would appreciate some help.

I've got this struct that that serializes data just fine:

#[derive(Serialize, Deserialize, Debug)]
struct Monitor {
  spoof_host: Option<String>,
  shell_command: String,
  #[serde(default = "default_max_checks")]
  max_check_attempts: usize,
  notify: Option<Value>,
  run_every: usize,
}

When viewing the data becoming a Monitor datatype, the data is in its propper format (int):

run_every: 60

However, when writing the data to a file, the numbers become floats:

run_every: 60.0

This is also true for the "max_check_attemps" field as well. Any help with this would be appreciated. Please let me know if I can help in anyway, I'm happy to share more information if requested.

Thanks in advance.

@dtolnay
Copy link
Owner

dtolnay commented Aug 22, 2018

I was not able to reproduce this. Here is what I tried:

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_yaml = "0.7"
#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_yaml;

use serde_yaml::Value;

#[derive(Serialize, Deserialize, Debug)]
struct Monitor {
    spoof_host: Option<String>,
    shell_command: String,
    max_check_attempts: usize,
    notify: Option<Value>,
    run_every: usize,
}

fn main() {
    println!("{}", serde_yaml::to_string(&Monitor {
        spoof_host: None,
        shell_command: "sh".to_owned(),
        max_check_attempts: 60,
        notify: None,
        run_every: 60,
    }).unwrap());
}
---
spoof_host: ~
shell_command: sh
max_check_attempts: 60
notify: ~
run_every: 60

Can you spot anything different from the way this happens in your code?

@btv
Copy link
Author

btv commented Aug 22, 2018

Hi David,

Thank you for the quick response. I guess I have some differences in my code & it might be because I'm not exactly sure what I'm doing. Within my script, I take Monitor type & reconvert it back to a Value to be help within a BTreeMap.

The problem seems to happen when I convert it back from a Monitor to a Value. So within two lines of the code I placed:

        println!("{:?}", &monitor);
        println!("{:?}", to_value(&monitor).unwrap());

and in the output, I get:

...
run_every: 60 }

and

...
String("run_every"): Number(Float(60.0))}

I guess the main difference is using "serde_yaml::to_value" instead of "to_string" like you are.

@dtolnay dtolnay changed the title Int's deserialzied as floats Integers get serialized as floats by to_value Aug 22, 2018
@dtolnay
Copy link
Owner

dtolnay commented Aug 22, 2018

Thanks! I released 0.8.0 with a fix.

@btv
Copy link
Author

btv commented Aug 24, 2018 via email

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

No branches or pull requests

2 participants