Tell Nancy to serialize enums into strings

浪尽此生 提交于 2019-12-10 16:53:43

问题


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

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:

Create the customization class:

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;
    }
}

Register it during bootstrap:

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.

Is there a simpler way?


回答1:


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();
    }
}

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

Hope this helps



来源:https://stackoverflow.com/questions/35193759/tell-nancy-to-serialize-enums-into-strings

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