問題描述
我正在嘗試使用 Json.NET 反序列化一些 JSON 對象.然而,我發現,當我反序列化一個沒有我要查找的屬性的對象時,不會引發任何錯誤,但是當我訪問它們時會為這些屬性返回一個默認值.當我反序列化錯誤類型的對象時,我能夠檢測到這一點很重要.示例代碼:
I'm trying to deserialize some JSON objects using Json.NET. I've found however that when I deserialize an object that doesn't have the properties I'm looking for that no error is thrown up but a default value is returned for the properties when I access them. It's important that I'm able to detect when I've deserialized the wrong type of object. Example code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Json_Fail_Test
{
class Program
{
[JsonObject(MemberSerialization.OptOut)]
private class MyJsonObjView
{
[JsonProperty("MyJsonInt")]
public int MyJsonInt { get; set; }
}
const string correctData = @"
{
'MyJsonInt': 42
}";
const string wrongData = @"
{
'SomeOtherProperty': 'fbe8c20b'
}";
static void Main(string[] args)
{
var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData);
System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString());
var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData);
System.Console.Out.WriteLine(badObj.MyJsonInt.ToString());
}
}
}
這個程序的輸出是:420
The output of this program is: 42 0
我寧愿拋出異常而不是靜默失敗.除此之外,有沒有辦法檢測序列化是否找不到參數?
I would prefer an exception be thrown to failing silently. Short of that is there a way to detect if the serialization failed to find a parameter?
我知道我可以使用 Json 對象解析數據,然后使用鍵值查找檢查參數,但我所在的代碼庫使用上面的模式,如果可能的話,我希望保持一致.
I know I can parse the data with a Json object and then check for the parameter with a key value lookup but the codebase I'm in uses the pattern above and I'd like keep that consistent if it's possible.
推薦答案
Json.Net 序列化程序有一個 MissingMemberHandling
設置,您可以將其設置為 Error
.(默認為 Ignore
.)這將導致序列化程序在反序列化過程中遇到目標類中沒有對應屬性的 JSON 屬性時拋出 JsonSerializationException
.
The Json.Net serializer has a MissingMemberHandling
setting which you can set to Error
. (The default is Ignore
.) This will cause the serializer to throw a JsonSerializationException
during deserialization whenever it encounters a JSON property for which there is no corresponding property in the target class.
static void Main(string[] args)
{
try
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MissingMemberHandling = MissingMemberHandling.Error;
var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData, settings);
System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString());
var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData, settings);
System.Console.Out.WriteLine(badObj.MyJsonInt.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}
結果:
42
JsonSerializationException: Could not find member 'SomeOtherProperty' on object
of type 'MyJsonObjView'. Path 'SomeOtherProperty', line 3, position 33.
請參閱:MissingMemberHandling 設置.
這篇關于檢測反序列化對象是否缺少 Json.NET 中 JsonConvert 類的字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!