反序列化期间忽略目标模型类中不存在的JSON枚举值 [英] Ignoring JSON enum value that doesn't exist in target model class during deserialization

查看:150
本文介绍了反序列化期间忽略目标模型类中不存在的JSON枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用C#开发API包装程序,我使用Newtonsoft.Json库将JSON数据反序列化为响应模型类.在响应模型类中,我有一个子项目列表,每个子项目都包含一个子项目列表.这些定义如下:

So I'm working on a API wrapper with C#, I deserialize JSON data to a response model class using Newtonsoft.Json library. In the response model class, I have a list of sub-items, which each of contain a list of sub-items. These are defined like this:

public List<StatModel> Stats { get; set; }

每个StatModel都有一个属性,该属性基本上等于名称:

each StatModel has a property which basically equals the name:

public Stat Stat { get; set; }

这些会自动反序列化,因为每个Stat是在这样的枚举中定义的:

these are automatically deserialized, because each Stat is defined in an enum like this:

[EnumMember(Value = "Avg Walk Distance")]
AverageWalkDistance,

现在的问题是,如果实际的API中发生了某些变化,则包装程序将不起作用,因为它没有为指定的Stat定义.因此,这意味着,如果他们将新的Stat添加到API,则包装器将无法工作,直到我手动为其添加定义,如上面的代码块中所示.

Now the problem is, if something changes in the actual API, the wrapper doesn't work since it doesn't have definition for the specified Stat. So this means if they add a new Stat to the API, the wrapper won't work until I manually add definition for it, like in the above code block.

所以问题是,如何忽略没有可用的相应Stat属性的值,或者我可以以某种方式重新设计整个设计,从而避免这种情况发生?我猜我必须自己定义所有新值.

So the question is, how can I ignore values that don't have corresponding Stat property available, or can I somehow re-design the whole thing so this doesn't happen? I'm guessing I have to define all new values by myself either way.

这是GitHub上的项目,可让我更好地理解我的实际意思: https://github.com /eklypss/PUBGSharp

Here's the project on GitHub to get a better understanding of what I actually mean: https://github.com/eklypss/PUBGSharp

请求者进行反序列化,并返回一个StatResponse,其中包含一个称为StatsRoot的子项列表,每个子项都有自己的StatModels列表,它们是导致此问题的实际stat对象.每种Stat类型都在Enum/Stat.cs文件中定义.

The Requester does the deserializing and returns a StatResponse, which has a list of sub-items called StatsRoot which each have their own list of StatModels which are the actual stat objects causing this issue. Each type of Stat is defined in the Enum/Stat.cs file.

推荐答案

如果您不想创建自己的

If you don't want to create your own tolerant version of StringEnumConverter, you could use Json.NET's exception handling features:

public class StatModel
{
    const string StatName = "label";

    [JsonProperty(StatName)]
    [JsonConverter(typeof(StringEnumConverter))]
    public Stat Stat { get; set; }

    public string Value { get; set; }
    public int? Rank { get; set; }
    public double? Percentile { get; set; }

    [OnError]
    void OnError(StreamingContext context, ErrorContext errorContext)
    {
        if (errorContext.OriginalObject == this && StatName.Equals(errorContext.Member))
        {
            errorContext.Handled = true;
        }
    }
}

在反序列化StatModel或其任何嵌套对象时引发异常时,将调用其OnError()方法来处理错误.该方法检查是否引发了异常

When an exception is thrown while deserializing StatModel or any of its nested objects, its OnError() method will be called to possibly handle the error. The method checks to see whether the exception was thrown

  1. 反序列化此特定对象时,并且
  2. 反序列化"label"成员时.

如果是,则将异常吞入.您还可以借此机会在模型中设置一个标志,指示Stat属性无效.

If so, the exception is swallowed. You could also take the opportunity to set a flag in the model indicating that the Stat property was invalid.

这篇关于反序列化期间忽略目标模型类中不存在的JSON枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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