我如何使用保留关键字作为标识符在我的JSON模型类?我的、标识符、如何使用、模型

2023-09-03 00:34:15 作者:单曲循环不再见

我从来没有使用的Web API之前,但我需要一个Web服务,将接受/返回JSON对象,并使用这个似乎是一个合理的事情。它看起来pretty的简单(如果不是我的目的有点大材小用的),但我需要处理的数据结构看起来是这样的:

I have never used Web API before, but I need a web service that will accept/return JSON objects and using this seemed like a reasonable thing. It looked pretty simple (if not a bit of overkill for my purposes), but a data structure I need to deal with looks something like:

{
    "values":["foo", "bar"],
    "default":"bar"
}

于是,我去了做一个模型对象:

And so I went to make a Model object:

class DropDownValues {
    public string[] values { get; set; }
    public string default { get; set; }
}

问题是,默认似乎是被保护的关键字。必须有某种方式来解决这个问题,对吧?

Problem is that default seems to be a protected keyword. There must be some way to get around that, right?

推荐答案

我会建议去不同的方式。保持你的C#的对象模型尽可能多的标准尽可能的(我不会用 @ 符号和 C#的关键字为属性名)的。

I would suggest to go different way. Keep your C# object model as much standard as possible (I wouldn't use @ sign and C# keywords as property name).

我们可以单独序列化(JSON)的世界和C#的对象 - 只要使用Json.NET功能

We can separate the serialized (JSON) world and C# objects - just by using the Json.NET features.

之一simpliest使用的装饰属性:

One of the simpliest to use is decoration with Attribute:

[JsonProperty(PropertyName = "default")]
public string DefaultValue { get; set; }

在这种情况下,我们必须引用Newtonsoft.Json项目。如果一定要POCO,我们可以引入 CustomResolver derrived从 DefaultContractResolver 并定义这些转换有...

In this case we have to reference Newtonsoft.Json in the project. If it must be POCO, we can introduce CustomResolver derrived from DefaultContractResolver and define these conversions there...

但在这种情况下,关注的分离是多一点纯洁的解决方案,我想说

But separation of concern in this case is a bit more pure solution, I would say

编辑:JSON解析器合同草案 (见注释)的

重要注意事项: Newtonsoft.Json 是Web API的一部分。它不仅是一个开源的,但即使是MS队的赌注为核心的JSON序列化。

Important NOTE: Newtonsoft.Json is part of the Web API. Not only it is an open source, but even MS team bet on that as a core JSON serializer.

1)Newtonsoft.Json(作为Web.API的一部分)已经安装在你的解决方案。所以,你不必下载(的NuGet)分开。这将永远是你的文件夹。因此,使用属性只是添加引用。正是在那里......

1) Newtonsoft.Json (as a part of the Web.API) is already installed in your solution. So you do not have to downloaded (nuget) separately. It would always be in your packages folder. So, to use the attribute is just adding the reference. It is there...

2)有一个小草案如何做属性的东西,同时保持POCO 。当我试着在这里解释: POCO的,行为和Peristance Igorance ,让POCO(如我们做的利润分层体系结构与NHibernate在数据层),我们就可以的替换的属性与合同解析器。我们的POCO库没有引用任何

2) There is a small draft how to do the attribute stuff, while keeping the POCO. As I've tried explain here: POCO's, behavior and Peristance Igorance, to keep POCO (e.g. we do profit from layered Architecture with NHibernate on a data layer), we can replace attributes with a Contract Resolver. Our POCO library does not have to reference anything

我们只需要做延长服务层:

We just have to do extend the service layer:

public class MyResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(
         MemberInfo member,
         MemberSerialization memberSerialization)
    {

        var jProperty = base.CreateProperty(member, memberSerialization);

        var propertyInfo = member as PropertyInfo;
        if (propertyInfo == null)
        {
            return jProperty;
        }

        // just adjust in case if Property name is DefaultValue
        var isDefaultValueProeprty =
                  propertyInfo.Name.Equals("DefaultValue");

        if(isDefaultValueProeprty)
        {
            jProperty.PropertyName = "default";
        }

        return jProperty;
    }
    ...

我们所提供的相同信息,这样的 serailizer 的作为与 [JsonPropertyAttribute]

This way we've provided the same information to serailizer as with the [JsonPropertyAttribute].

现在,我们只需要使用它。有很多种方法(如全球),但我们可以做到这一点的只有一个控制器:

Now, we just have to use it. There are many ways (e.g. global) but we can do it for a controller only:

protected override void Initialize(HttpControllerContext context)
{
  base.Initialize(context);

  var jSettings = context.Configuration.Formatters.JsonFormatter.SerializerSettings;
  jSettings.ContractResolver = MyResolver;
}
 
精彩推荐
图片推荐