Skip to content

json now defaults Option<_> to None.#16971

Merged
bors merged 1 commit into
rust-lang:masterfrom
treeman:json-decode
Sep 9, 2014
Merged

json now defaults Option<_> to None.#16971
bors merged 1 commit into
rust-lang:masterfrom
treeman:json-decode

Conversation

@treeman

@treeman treeman commented Sep 3, 2014

Copy link
Copy Markdown
Contributor

Closes #12794.

Comment thread src/libsyntax/ext/deriving/decodable.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not going to work right if I have a type named Option that is not std::option::Option.

Similarly, it presumably will not work right if I rename Option, using either use std::option::Option as Foo or type Foo = std::option::Option<int>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I totally missed that.

@lilyball

lilyball commented Sep 3, 2014

Copy link
Copy Markdown
Contributor

I feel like the right approach here really is to be able to provide a default value to use if the field isn't present. This is just another instance of the need to be able to customize deriving.

@alexcrichton

Copy link
Copy Markdown
Member

Note that it is currently possible for decoders to emulate this behavior today, as it's what toml-rs does (and used quite extensively in cargo).

@treeman

treeman commented Sep 4, 2014

Copy link
Copy Markdown
Contributor Author

I agree @kballard, we need default values.

Thanks @alexcrichton, will take a peek.

@treeman

treeman commented Sep 9, 2014

Copy link
Copy Markdown
Contributor Author

Reworked the Option handling and now defaults to None for json only.

#17089 may enable us to specify default arguments, something like:

#[deriving(Decodable)]
struct Data {
    #[default = None]
    x: Option<uint>,
}

I still think it's more ergonomic to have Option default to None, at least in the json case.

@treeman treeman changed the title Derived Decodable can now handle Option<_> as optional input. json can now defaults Option<_> to None. Sep 9, 2014
@treeman treeman changed the title json can now defaults Option<_> to None. json now defaults Option<_> to None. Sep 9, 2014
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Sep 9, 2014
@bors bors merged commit 4f4a3df into rust-lang:master Sep 9, 2014
@treeman treeman deleted the json-decode branch September 10, 2014 05:16
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 31, 2024
Resolve tests per file instead of per crate in test explorer

Fix part of rust-lang#16827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modify Json Decoder to handle missing/optional Json fields if mapped to Option

4 participants