Upgrade of NewtonSoft JSON.NET not implicitly serialising protected members

后端 未结 1 461
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 06:20

I have just updated my version of NewtonSoft JSON.NET from version 3.0.0 to 3.5.0 and I have noticed that protected members are not implicitly serialised.

I have the

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 06:44

    I had this same problem today. Luckily Ayende had the fix and I am sharing it with you. When you configure the serializer, change the DefaultMembersSearchFlags property on the ContractResolver:

    var serializer = new JsonSerializer
                            {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                                ContractResolver = new DefaultContractResolver
                                    {
                                        DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                                    },
                                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
    
                                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                            };
    

    0 讨论(0)
提交回复
热议问题