没有默认构造函数和 .NET Native 的自定义 Json 转换器
Custom Json Converter with no default Constructor and .NET Native
我正在通用 Windows 平台 (Win10) 上编写应用程序。并且在 运行 发布模式下使用自定义 Json 转换器时出现问题,这意味着通过 .NET Native 进行编译。这是我的转换器代码
public class NestedObjectJsonConverter : JsonConverter
{
private string childNodeName;
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartObject)
{
return serializer.Deserialize(reader, objectType);
}
var rootObject = JObject.Load(reader);
var childToken = rootObject[childNodeName];
return ServiceModelJsonConvert.DeserializeObject(childToken, objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
writer.WritePropertyName(childNodeName);
writer.WriteRawValue(JsonConvert.SerializeObject(value));
writer.WriteEndObject();
}
public NestedObjectJsonConverter(string childNodeName)
{
this.childNodeName = childNodeName;
}
}
在释放模式下触发此转换器时。它得到 "creation error" 异常,但它在调试模式下工作正常。
我已经记录了我的异常消息
HResult : -2146233088 TypeName : Newtonsoft.Json.JsonException,
Newtonsoft.Json, Version=8.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed Message :
Newtonsoft.Json.JsonException: Error creating
'NestedObjectJsonConverter'. --->
System.NullReferenceException: Object reference not set to an instance
of an object. at
Newtonsoft.Json.Serialization.JsonTypeReflector.<>c.b__18_1(Object
param) at
System.Func.Invoke(Runtime.CompilerServices.CallSiteBinder
arg) at SharedLibrary!+0x3a908c at
SharedLibrary!+0x3b1492 at
SharedLibrary!+0x4d376a at
Newtonsoft.Json.Serialization.JsonTypeReflector.<>c__DisplayClass18_0.b__0(Object[]
parameters) --- End of inner exception stack trace --- at
Newtonsoft.Json.Serialization.JsonTypeReflector.<>c__DisplayClass18_0.b__0(Object[]
parameters) at
System.Func.Invoke(Runtime.CompilerServices.CallSiteBinder
arg) at
Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter(Object
attributeProvider) at
Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes(Json.Serialization.JsonProperty
property, Object attributeProvider, String name, Type declaringType,
Json.MemberSerialization memberSerialization, Boolean&
allowNonPublicAccess) at
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty(Reflection.MemberInfo
member, Json.MemberSerialization memberSerialization) at
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(Type
type, Json.MemberSerialization memberSerialization) at
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type
objectType) at
Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type
objectType) at
Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type
type) at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(Type
type) at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Json.JsonReader
reader, Type objectType, Boolean checkAdditionalContent) at
Newtonsoft.Json.JsonSerializer.DeserializeInternal(Json.JsonReader
reader, Type objectType) at
Newtonsoft.Json.JsonSerializer.Deserialize(Json.JsonReader reader,
Type objectType) at
Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type,
Json.JsonSerializerSettings settings)
似乎找到参数化构造函数是 .NET Native 中此问题的根本原因。但这是 .NET Native 或 JSON.NET ?
这是 .net 本机错误。它已在当前版本中修复
我正在通用 Windows 平台 (Win10) 上编写应用程序。并且在 运行 发布模式下使用自定义 Json 转换器时出现问题,这意味着通过 .NET Native 进行编译。这是我的转换器代码
public class NestedObjectJsonConverter : JsonConverter
{
private string childNodeName;
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartObject)
{
return serializer.Deserialize(reader, objectType);
}
var rootObject = JObject.Load(reader);
var childToken = rootObject[childNodeName];
return ServiceModelJsonConvert.DeserializeObject(childToken, objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
writer.WritePropertyName(childNodeName);
writer.WriteRawValue(JsonConvert.SerializeObject(value));
writer.WriteEndObject();
}
public NestedObjectJsonConverter(string childNodeName)
{
this.childNodeName = childNodeName;
}
}
在释放模式下触发此转换器时。它得到 "creation error" 异常,但它在调试模式下工作正常。
我已经记录了我的异常消息
HResult : -2146233088 TypeName : Newtonsoft.Json.JsonException, Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed Message : Newtonsoft.Json.JsonException: Error creating 'NestedObjectJsonConverter'. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Newtonsoft.Json.Serialization.JsonTypeReflector.<>c.b__18_1(Object param) at System.Func.Invoke(Runtime.CompilerServices.CallSiteBinder arg) at SharedLibrary!+0x3a908c at SharedLibrary!+0x3b1492 at SharedLibrary!+0x4d376a at Newtonsoft.Json.Serialization.JsonTypeReflector.<>c__DisplayClass18_0.b__0(Object[] parameters) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.JsonTypeReflector.<>c__DisplayClass18_0.b__0(Object[] parameters) at System.Func.Invoke(Runtime.CompilerServices.CallSiteBinder arg) at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter(Object attributeProvider) at Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes(Json.Serialization.JsonProperty property, Object attributeProvider, String name, Type declaringType, Json.MemberSerialization memberSerialization, Boolean& allowNonPublicAccess) at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty(Reflection.MemberInfo member, Json.MemberSerialization memberSerialization) at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(Type type, Json.MemberSerialization memberSerialization) at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType) at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType) at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(Type type) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Json.JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(Json.JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(Json.JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, Json.JsonSerializerSettings settings)
似乎找到参数化构造函数是 .NET Native 中此问题的根本原因。但这是 .NET Native 或 JSON.NET ?
这是 .net 本机错误。它已在当前版本中修复