Skip to content

Optionally serialize options #9

@nikclayton-dfinity

Description

@nikclayton-dfinity

[Posting as new issue because I think the comment on the other issue may have been missed]

I don't think this is quite right.

Consider the following:

#[derive(Clone, Debug, Default, Serialize, KV, SerdeValue)]
struct A {
    #[serde(skip_serializing_if = "Option::is_none")]
    b: Option<B>
}

#[derive(Clone, Debug, Default, Serialize, SerdeValue)]
struct B {
    #[serde(skip_serializing_if = "Option::is_none")]
    first: Option<String>,
    second: Option<String>
}

[...]

let a = A::default();

With (at least) slog_json, if I try and log that (info!(logger, "msg"; a);) I get the output

{
  "b": null
}

I would expect {} as the output.

If I partially construct B like this:

let a = A {
    b: Some(B {
        first: Some("hello world".into()),
        second: None,
    }),
};

and log a again I get:

{
   "b": {
     "first": "hello world"
   }
}

So skipping the option on the second field has worked.

If I remove the KV from the derive line, and write the log like info!(logger, "msg"; "a" => a); then the optional fields are correctly skipped, but everything is now under the "a" key in the output.

Originally posted by @nikclayton-dfinity in #5 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions