首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Serde json值忽略None上的属性

Serde json值忽略None上的属性
EN

Stack Overflow用户
提问于 2022-01-02 17:59:58
回答 1查看 711关注 0票数 4

给定以下JsonValue:

代码语言:javascript
复制
let mut schema = json!({
    "level": "strict",
    "rule": {}
});

,我们将动态地将值插入到这个JsonValue中。

代码语言:javascript
复制
let value: json!({
    "type": property.r#type,
    "minLength": property.min_length,
    "maxLength": property.max_length,
    "enum": property.r#enum
});

schema["rule"]
    .as_object_mut()
    .unwrap()
    .insert(
        String::from(property.name), 
        value
    );

// Struct for Property
#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaDocumentProperty
{
    pub name: String,
    pub r#type: Option<String>,
    pub min_length: Option<u32>,
    pub max_length: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#enum: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<SchemaDocumentPropertyArray>
}

当前输出为minLengthmaxLengthenum为None的输出:

代码语言:javascript
复制
{
    "type": "string",
    "minLength": null,
    "maxLength": null,
    "enum": null
}

我想要的输出:

代码语言:javascript
复制
{
    "type": "string"
}

我想省略JsonValue宏中的所有None值。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70558431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档