Git Product home page Git Product logo

serde-partial's Introduction

serde-partial

One of the few things that still require boilerplate when using Serde is partial serialization.

Let's say we have an API route which returns product information. We want to return the stock only to admins and not to visitors. There are a few options here; have a second struct with a subset of the fields which also derives Serialize, make the stock field an Option with #[serde(skip_serializing_if = "Option::is_none")] and set to None for visitors, or use something like the serde_json::json! macro and do manual serialization.

None of these options are particularly attractive. Having to maintain a struct and handle conversion for each subset of fields is a lot of boilerplate. Making a field that's always present optional just for serialization is a hack at best. Manual serialization kind of defeats the purpose of serde derives.

serde-partial aims to make partial serialization (almost) as clean an concise as complete serialization while also being no_std compatible. Using this crate this problem could be solved in a single line.

use serde::Serialize;
use serde_partial::SerializePartial;

#[derive(Serialize, SerializePartial)]
pub struct Product {
    name: String,
    price: u32,
    stock: u32,
}

fn get_product(id: i32) -> Product {
    todo!()
}

fn product_api(id: i32, is_manager: bool) -> String {
    let product = get_product(id);
    if is_manager {
        serde_json::to_string(&product).unwrap()
    } else {
        serde_json::to_string(&product.without_fields(|p| [p.stock])).unwrap()
    }
}

Check out the with_fields documentation for more details and examples.

serde-partial's People

Contributors

raftario avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

isgasho

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.