告诉南希将枚举序列化为字符串

告诉南希将枚举序列化为字符串

本文介绍了告诉南希将枚举序列化为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Nancy在生成JSON响应时将枚举序列化为整数.我需要将枚举序列化为字符串.

Nancy by default serialize enums into integers when generating JSON response. I need to serialize enums into strings.

有一种方法可以通过创建 JavaScriptPrimitiveConverter 来自定义Nancy的JSON序列化.例如,这是我为一个枚举自定义序列化的工作:

There is a way to customize Nancy's JSON serialization by creating JavaScriptPrimitiveConverter. For example this is what I did to customize serialization for ONE enum:

创建自定义类:

public class JsonConvertEnum : JavaScriptPrimitiveConverter
{
    public override IEnumerable<Type> SupportedTypes
    {
        get
        {
            yield return typeof(MyFirstEnum);
        }
    }

    public override object Deserialize(
        object primitiveValue, Type type, JavaScriptSerializer serializer)
    {
        if (type == typeof(MyFirstEnum))
        {
            var serialized = (string)primitiveValue;
            MyFirstEnum deserialized;
            if (Enum.TryParse(serialized, out deserialized))
            {
                return deserialized;
            }
            else
            {
                return null;
            }
        }

        return null;
    }

    public override object Serialize(
        object obj, JavaScriptSerializer serializer)
    {
        if (obj is MyFirstEnum)
        {
            var deserialized = (MyFirstEnum)obj;
            var serialized = deserialized.ToString();
            return serialized;
        }

        return null;
    }
}

在引导过程中注册它:

protected override void ApplicationStartup(
    Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
    base.ApplicationStartup(container, pipelines);
    Nancy.Json.JsonSettings.PrimitiveConverters.Add(new JsonConvertEnum());
}

我需要对所有枚举执行此操作.

I need to do this for ALL of my enums.

有没有更简单的方法?

推荐答案

我没有时间自己进行测试,但是以下代码应适用于所有Enum类型

I haven't had the time to test it myself, but the following code should work for all Enum types

public class JsonConvertEnum : JavaScriptPrimitiveConverter
{
    public override IEnumerable<Type> SupportedTypes
    {
        get
        {
            yield return typeof(Enum);
        }
    }

    public override object Deserialize(
        object primitiveValue, Type type, JavaScriptSerializer serializer)
    {
        if (!type.IsEnum)
        {
            return null;
        }

        return Enum.Parse(type, (string)primitiveValue);
    }

    public override object Serialize(
        object obj, JavaScriptSerializer serializer)
    {
        if (!obj.GetType().IsEnum)
        {
            return null;
        }

        return obj.ToString();
    }
}

基本上,它使用Type元数据来确定它是否是Enum,然后利用Enum.Parse(...)将其从原始值转换回正确的枚举.要将Enum转换为string,只需将值转换为string

Basically it uses the Type metadata to determine if it is an Enum or not and then makes use of Enum.Parse(...) to convert it from the primitive value back to the correct enum. To convert from Enum to string all you have to do is to cast the value to a string

使用三元运算符可以使它更简洁,但为了清楚起见,我保留了更详细的版本

It can be made more terse by using the ternary operator, but I left the more verbose version for clarity

希望这对您有帮助

这篇关于告诉南希将枚举序列化为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!