How do I serialize an enum without including the name of the enum variant?

走远了吗. 提交于 2019-12-17 10:06:01

问题


I am trying to serialize an enum to a JSON string. I implemented Serialize trait for my enum as it is described in the docs, but I always get {"offset":{"Int":0}} instead of the desired {"offset":0}.

extern crate serde;
extern crate serde_json;

use std::collections::HashMap;

use serde::ser::{Serialize, Serializer};

#[derive(Debug)]
enum TValue<'a> {
    String(&'a str),
    Int(&'a i32),
}

impl<'a> Serialize for TValue<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            TValue::String(ref s) => serializer.serialize_newtype_variant("TValue", 0, "String", s),
            TValue::Int(i) => serializer.serialize_newtype_variant("TValue", 1, "Int", &i),
        }
    }
}

fn main() {
    let offset: i32 = 0;
    let mut request_body = HashMap::new();
    request_body.insert("offset", TValue::Int(&offset));
    let serialized = serde_json::to_string(&request_body).unwrap();
    println!("{}", serialized); // {"offset":{"Int":0}}
}

回答1:


You can use the untagged attribute which will produce the desired output. You won't need to implement Serialize yourself with this:

#[derive(Debug, Serialize)]
#[serde(untagged)]
enum TValue<'a> {
    String(&'a str),
    Int(&'a i32),
}

If you wanted to implement Serialize yourself, I believe you want to skip your variant so you should not use serialize_newtype_variant() as it exposes your variant. You should use serialize_str() and serialize_i32() directly:

impl<'a> Serialize for TValue<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            TValue::String(s) => serializer.serialize_str(s),
            TValue::Int(i) => serializer.serialize_i32(*i),
        }
    }
}

It produces the desired output:

{"offset":0}


来源:https://stackoverflow.com/questions/52034764/how-do-i-serialize-an-enum-without-including-the-name-of-the-enum-variant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!