dtolnay/serde-yaml

[BUG] Serialization emits invalid file with missing sections

Opened this issue · 3 comments

Danvil commented

I have a large application where I dump about 30 MB worth of smaller objects into a file. All objects are using serde auto-derive [derive(Serialize, Deserialize)].

I am using serde_yaml = "0.9" and the code which writes my file looks like this:

let mut file = std::fs::OpenOptions::new()
  .write(true)
  .create(true)
  .open(filename)
  .context(filename.to_owned())?;
let mut out = BufWriter::new(file);
serde_yaml::to_writer(&mut out, &my_stuff).context("serialization to YAML")?;
out.flush()?;

(Note: I tried and it also happens without the BufWriter.)

my_stuff above is a large map with about 20000 objects.

Unfortunately this emits an invalid YAML file where there are obviously some pieces missing. Here is an excerpt of the file which was written:

    SelectionMarker:
      color:
        r: 1.0
        g: 1.0
        b: 1.0
      is_visible: true
      is_selected: false
:
      food: 30.0
      food_current: 30.0
      water: 20.0

There is a single line with a stray : and there is some data missing at that location.

So far I can say: it does seem to happen exactly once in the file and it happens towards the end of the file (this was line 1727330 of 1743267).

"Stray :" is not necessarily invalid YAML. You are just showing an excerpt of the output but the following is valid YAML syntax:

?
    SelectionMarker:
      color:
        r: 1.0
        g: 1.0
        b: 1.0
      is_visible: true
      is_selected: false
:
      food: 30.0
      food_current: 30.0
      water: 20.0

Since what you showed is valid YAML, this issue is not going to be actionable unless you can provide a minimal repro of incorrect behavior.

Danvil commented

There is no ? before in the file. There is simply content missing at the location where the : appears in the file. The emitted file does not pass the serialize - deserialize test and panics during deserialization with serde_yaml.

When I switch to JSON by simply replacing serde_yaml::to_writer with serde_json::to_writer_pretty it is working fine.