在 WebAPI 中的模型上使用 Serializable 属性 [英] Using Serializable attribute on Model in WebAPI

查看:24
本文介绍了在 WebAPI 中的模型上使用 Serializable 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下场景:我正在使用 WebAPI 并根据模型将 JSON 结果返回给消费者.我现在有额外的要求将模型序列化为 base64,以便能够将它们保存在缓存中和/或将它们用于审计目的.问题是,当我将 [Serializable] 属性添加到模型以便将模型转换为 Base64 时,JSON 输出更改如下:

I have the following scenario: I am using WebAPI and returning JSON results to the consumer based on a model. I now have the additional requirement to serialize the models to base64 to be able to persist them in cache and/or use them for auditing purposes. Problem is that when I add the [Serializable] attribute to the model so for converting the the model to Base64, the JSON output changes as follows:

模型:

[Serializable]
public class ResortModel
{
    public int ResortKey { get; set; }

    public string ResortName { get; set; }
}

如果没有 [Serializable] 属性,JSON 输出为:

Without the [Serializable] attribute the JSON output is:

{
    "ResortKey": 1,
    "ResortName": "Resort A"
}

使用 [Serializable] 属性,JSON 输出为:

With the [Serializable] attribute the JSON output is:

{
    "<ResortKey>k__BackingField": 1,
    "<ResortName>k__BackingField": "Resort A"
}

如何在不更改 JSON 输出的情况下使用 [Serializable] 属性?

How would I be able to use the [Serializable] attribute without changing the output of the JSON?

推荐答案

默认情况下,Json.NET 忽略 Serializable 属性.但是,根据 this answer 的评论,Maggie Ying(在下面引用是因为评论并不意味着持续),WebAPI 会覆盖该行为,这会导致您的输出.

By default, Json.NET ignores the Serializable attribute. However, according to a comment to this answer by Maggie Ying (quoted below because comments are not meant to last), WebAPI overrides that behavior, which causes your output.

Json.NET 序列化程序默认将 IgnoreSerializableAttribute 设置为 true.在 WebAPI 中,我们将其设置为 false.您遇到此问题的原因是 Json.NET 忽略属性:Json.NET 现在检测具有 SerializableAttribute 的类型并序列化该类型的所有字段,包括公共和私有,并忽略属性"(引自 詹姆斯.newtonking.com/archive/2012/04/11/...)

Json.NET serializer by default set the IgnoreSerializableAttribute to true. In WebAPI, we set that to false. The reason why you hit this issue is because Json.NET ignores properties: "Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the properties" (quoted from james.newtonking.com/archive/2012/04/11/…)

在没有 WebAPI 的情况下演示相同行为的简单示例如下所示:

A simple example that demonstrates the same behavior without WebAPI can look like this:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
namespace Scratch
{
    [Serializable]
    class Foo
    {
        public string Bar { get; set; }
    }

    class Program
    {
        static void Main()
        {
            var foo = new Foo() { Bar = "Blah" };
            Console.WriteLine(JsonConvert.SerializeObject(foo, new JsonSerializerSettings()
                {
                    ContractResolver = new DefaultContractResolver()
                    {
                        IgnoreSerializableAttribute = false
                    }
                }));
        }
    }
}

有几种方法可以解决此行为.一种是用一个简单的 JsonObject 属性来装饰你的模型:

There are several ways around this behavior. One is to decorate your model with a plain JsonObject attribute:

[Serializable]
[JsonObject]
class Foo
{
    public string Bar { get; set; }
}

另一种方法是覆盖 Application_Start() 中的默认设置.根据这个答案,默认设置应该这样做:

Another way is to override the default settings in your Application_Start(). According to this answer, the default settings should do it:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();

如果这不起作用,您可以明确说明:

If that doesn't work, you could be explicit about it:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings()
    {
        ContractResolver = new DefaultContractResolver()
        {
            IgnoreSerializableAttribute = true
        }
    };

这篇关于在 WebAPI 中的模型上使用 Serializable 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆