GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bytedream/serde-inline-default: Serde default values via inline attribute declaration

BranchesTags
Open more actions menu

Latest commit

 

History

38 Commits
38 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE-APACHE
LICENSE-APACHE
 
 
LICENSE-MIT
LICENSE-MIT
 
 
README.md
README.md
 
 

Repository files navigation

serde-inline-default ci crates.io crates.io downloads docs

A tiny crate to set default values for serde struct fields via inline attribute declaration.

Overview

This crate is an approach to do what serde-rs/serde#368 purposes: Defining default values for struct fields via inline declaration instead of creating a separate function for it.

So instead of writing something like this, which can get very verbose quickly with many fields:

#[derive(Deserialize)]
struct Test {
    #[serde(default = "value_default")]
    value: u32
}

fn value_default() -> u32 { 42 }

you can just do this:

#[serde_inline_default]
#[derive(Deserialize)]
struct Test {
    #[serde_inline_default(42)]
    value: u32
}

Important

#[serde_inline_default] must be set before #[derive(Deserialize)]/#[derive(Serialize)]!

Internally, #[serde_inline_default(...)] gets expanded to a function which returns the set value and the attribute is replaced with #[serde(default = "<function name>")]. So this macro is just some syntax sugar for you, but can get quite handy if you want to keep your code clean or write declarative macros / macro_rules!.

Alternatives

This crate isn't perfect. Thus, you might be more satisfied with alternatives serde provides.

With #[serde(default)] + impl Default on a struct, serde uses the default implementation of the struct to get default values for each field (docs):

#[derive(Deserialize)]
#[serde(default)]
struct Test {
    value: u32
}

impl Default for Test {
    fn default() -> Self {
        Self {
            value: 42
        }
    }
}

If you still need/want serde-inline-default features, you also can combine them with #[serde(default)) and impl Default:

#[serde_inline_default]
#[derive(Deserialize)]
#[serde(default)]
struct Test {
    value: u32,
    #[serde_inline_default(0)]
    other_value: u32,
}

impl Default for Test {
    fn default() -> Self {
        Self {
            value: 42,
            other_value: 42
        }
    }
}

License

This project is licensed under either of the following licenses, at your option:

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)

About

Serde default values via inline attribute declaration

Topics

rustrust-serdeserdeserde-jsonserde-serialization

Resources

Readme
Activity

Stars

39 stars

Watchers

1 watching

Forks

2 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.