本文介绍了让JSON.NET和Serializable属性共同努力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSON.NET并在过去一些麻烦的WebAPI期间对象序列化。之后做一些研究,我发现这个类是标有[Serializable接口。当我删除这个反序列化就没事了。

I'm using JSON.NET and had some troubles in the past during WEBAPI objects deserialization. After doing some research I've found that the class was marked with [Serializable]. When I removed this the deserialization was just fine.

有关更详细的信息可以在这里找到:

More detailed information about this can be found here:

Why不会的Web API反序列化这一点,但JSON.Net会?

现在涉及到我使用的BinaryFormatter来创建此对象类计算的散列值的问题。
但是BinaryFormatter的要求类必须标记为[Serializable接口]。

Now it comes to the problem that I use binaryformatter to create a hash value calculated from this object class.But Binaryformatter requires that the class must be marked as [Serializable].

您能推荐我任何的方法来使双方的事情在同一时间工作?

Could you recommend me any approach to make both things work at the same time?

推荐答案

找到了解决办法:

首先,检查你的Newtonsoft.JSON版本大于4.5,或只是更新的NuGet

First, check that your Newtonsoft.JSON version is greater than 4.5 or just update with NuGET

根据版本的笔记,既可以共同使用一些额外的注解从这个版本开始。

According to the version notes, both can work together starting from this version using some extra annotations.

http://james.newtonking.com/archive/2012/04/11/json-net-4-5-release-2-serializable-support-and-bug-fixes

现在,如果你正在序列具有属性,不希望新的行为类型,它可以被使用的JsonObjectAttribute类型覆盖

"Now if you are serializing types that have the attribute and don’t want the new behaviour, it can either be overridden on a type using the JsonObjectAttribute"

[JsonObject]
[Serializable]
public class Foobar {

现在可以使用JSON.NET,在我的情况下,用[Serializable]属性的BinaryFormatter。

Now it is possible to use JSON.NET and, in my case, the binaryformatter with the [Serializable] attribute.

这篇关于让JSON.NET和Serializable属性共同努力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:45