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

JSON serialization could skip serializing "null" fields #43

Closed
erickt opened this issue Mar 20, 2015 · 2 comments
Closed

JSON serialization could skip serializing "null" fields #43

erickt opened this issue Mar 20, 2015 · 2 comments

Comments

@erickt
Copy link
Member

erickt commented Mar 20, 2015

Right now we serialize a type like:

struct Foo {
    x: ()
}

As:

{"x": null}

But many json deserializers (like serde) support treating missing fields as a null value. If we could do this efficiently in serde, then we could cut down on the size of our serialized output. Unfortunately this would require us to add some form of lookahead to Serializer, which might be tricky.

@oli-obk
Copy link
Member

oli-obk commented Jan 13, 2016

Copying your comment from the PR here, so it's not lost

Another option would be when serializing fields, the Serializer passes an outer closure to the Serialize impl. If it decides to serialize the value, then it passes an inner closure to outer one. The outer one actually serializes the field name, then calls the inner one to do the actual serialization. Not calling the closure is equivalent to not serializing the key-value pair.

@dtolnay
Copy link
Member

dtolnay commented Jun 13, 2016

The current behavior makes sense as the default. The desired behavior can be had with skip_serializing or skip_serializing_if attributes.

@dtolnay dtolnay closed this as completed Jun 13, 2016
rubdos pushed a commit to rubdos/serde that referenced this issue Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants