我正在使用包含Guid类型的私有字段和该字段的公共属性的Json.NET反序列化对象.当我的Guid的值在我的json中为null时,我想将Guid.Empty分配给我的字段.
public class MyClass { private Guid property; public Guid Property { get { return property; } set { if (value == null) { property = Guid.Empty; } else { property = value; } } } }
但是反序列化器想要访问私有字段,因为当我尝试反序列化时出现此错误:
Error converting value {null} to type ‘System.Guid’. Path
‘[0].property’,line 6,position 26.
如何让它忽略私有字段并使用公共属性呢?
解决方法
Json.NET拒绝为Guid设置空值,因为它是一个不可为空的值类型.尝试在
Immediate Window中键入(Guid)null,您将看到一条错误消息,指示无法在.Net中进行此转换.
要解决此问题,您有以下几种选择:
>创建一个Guid?可空代理属性.只要它具有[JsonProperty]
属性,它就可以是私有的:
public class MyClass { [JsonIgnore] public Guid Property { get; set; } [JsonProperty("Property")] Guid? NullableProperty { get { return Property == Guid.Empty ? null : (Guid?)Property; } set { Property = (value == null ? Guid.Empty : value.Value); } } }
>创建一个将空Json标记转换为默认Guid值的JsonConverter
:
public class NullToDefaultConverter<T> : JsonConverter where T : struct { public override bool CanConvert(Type objectType) { return objectType == typeof(T); } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { var token = JToken.Load(reader); if (token == null || token.Type == JTokenType.Null) return default(T); return token.ToObject(objectType); } // Return false instead if you don't want default values to be written as null public override bool CanWrite { get { return true; } } public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { if (EqualityComparer<T>.Default.Equals((T)value,default(T))) writer.WriteNull(); else writer.WriteValue(value); } }
然后将其应用于您的类型,如下所示:
public class MyClass { [JsonConverter(typeof(NullToDefaultConverter<Guid>))] public Guid Property { get; set; } }
或者,您可以通过将转换器添加到JsonSerializerSettings.Converters
将转换器应用于T类型的所有值.并且,要全局注册此类转换器,请参阅例如Registering a custom JsonConverter globally in Json.Net用于控制台应用程序,How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?用于Web API,或Setting JsonConvert.DefaultSettings asp net core 2.0 not working as expected用于ASP.NET Core.