自定义Json转换器,没有默认的构造函数和.NET Native

时间:2015-12-07 08:39:45

标签: .net json.net win-universal-app .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;
    }
}

在释放模式下触发此转换器时。它会出现“创建错误”异常,但在调试模式下工作正常。

我已记录我的例外情况

  

HResult:-2146233088 TypeName:Newtonsoft.Json.JsonException,   Newtonsoft.Json,Version = 8.0.0.0,Culture = neutral,   PublicKeyToken = 30ad4fe6b2a6aeed消息:   Newtonsoft.Json.JsonException:创建错误   'NestedObjectJsonConverter'。 --->   System.NullReferenceException:未将对象引用设置为实例   一个对象。在   Newtonsoft.Json.Serialization.JsonTypeReflector<> c.b__18_1(对象   param)at   System.Func.Invoke(Runtime.CompilerServices.CallSiteBinder   arg)在SharedLibrary!+ 0x3a908c at   SharedLibrary!+ 0x3b1492 at   SharedLibrary!+ 0x4d376a at   Newtonsoft.Json.Serialization.JsonTypeReflector<> c__DisplayClass18_0.b__0(对象[]   参数)---内部异常堆栈跟踪结束--- at   Newtonsoft.Json.Serialization.JsonTypeReflector<> c__DisplayClass18_0.b__0(对象[]   参数)at   System.Func.Invoke(Runtime.CompilerServices.CallSiteBinder   arg)at   Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter(对象   attributeProvider)at   Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes(Json.Serialization.JsonProperty   property,Object attributeProvider,String name,Type declaringType,   Json.MemberSerialization memberSerialization,Boolean&   allowNonPublicAccess)   Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty(Reflection.MemberInfo   成员,Json.MemberSerialization memberSerialization)at   Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(类型   type,Json.MemberSerialization memberSerialization)at   Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(类型   objectType)at   Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(类型   objectType)at   Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(类型   输入)   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(类型   输入)   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Json.JsonReader   reader,Type objectType,Boolean checkAdditionalContent)at   Newtonsoft.Json.JsonSerializer.DeserializeInternal(Json.JsonReader   reader,输入objectType)at   Newtonsoft.Json.JsonSerializer.Deserialize(Json.JsonReader reader,   输入objectType)at   Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型类型,   Json.JsonSerializerSettings设置)

似乎在.NET Native中发现参数化构造函数是此问题的根本原因。但这只是一个.NET Native或JSON.NET?

1 个答案:

答案 0 :(得分:0)

这是一个.net Native bug。它已在当前版本中修复