not-fl3 / nanoserde

Serialisation library with zero dependencies
689 stars 39 forks source link

serde_json: serialize None as null if there is default attribute #111

Closed birhburh closed 3 weeks ago

birhburh commented 1 month ago

During porting of parsing lottie files to this project from lottie-rs I comparing serialized data and nanoserde was skipping None fields with default attribute during serialization, but serde serializes them as null

not-fl3 commented 1 month ago

Thanks for PR!

I do not have a very strong opinion, but I feel like I would rather have this behavior being optional, maybe some special attribute about serializing as "null"?

not-fl3 commented 1 month ago

oh, why do I always vargue in nanoserde's PRs :facepalm: you are right, null is the way!

I just opened JSON standard and it says pretty clear that null is supposed to be serialized as null and nothing about removing fields.

I still feel like it would be nice to have an option to skip None's in serialized json, even if they got a default value, but maybe there should be an attribute for skipping, not for adding?

birhburh commented 1 month ago

Checked how they skip on None in serde and of course i forgot about #[serde(skip_serializing_if = "Option::is_none")] Adding this to my nanoserde todo list

not-fl3 commented 1 month ago

#[serde(skip_serializing_if = "Option::is_none")] looks good, but I think it would make more sense to have it another way around: skip serializing by default, and write null if the attribute is set. This way we will avoid a breaking change while support all the use cases where this null is important

birhburh commented 4 weeks ago

Added serialize_none_as_null for struct and fields, and changed test accordingly

knickish commented 3 weeks ago

Thanks @birhburh, sorry was slow to review