从不同位置序列化和反序列化对象 [英] serialize and deserialize objects from different location

查看:43
本文介绍了从不同位置序列化和反序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单独的程序,需要共享信息.共享将通过一个应用程序将 XML序列化的对象放入数据库中,而另一个应用程序在另一台计算机上进行检索来完成.这些对象共享相同的变量,但是 properties 方法不同.

I have two separate programs that need to share information. This sharing will be done by one app placing an XML serialized object in a database, and the other app retrieving it on a different machine. The objects share the same variables but the properties and methods are different.

  • 两个程序之间的类必须有多精确匹配?
  • 是逐行匹配还是只是变量,属性和方法名称?

我最终使用了 Newtonsoft.Json 库而不是 xml ,并使用了< JsonObject(MemberSerialization.OptIn)> JsonProperty()属性可控制序列化的内容.

I ended up using the Newtonsoft.Json library instead of xml and used the <JsonObject(MemberSerialization.OptIn)> and JsonProperty() attributes to control what got serialized.

推荐答案

您没有指定要使用的序列化类型.

You did not specify which kind of serialization you were after.

标准NET二进制串行器不太适合2个不同程序集之间的数据交换.当您反序列化时,会出现类似于 [Culture].[Assembly].[Version] .SourceClass 的错误,无法反序列化为 [Culture].[Assembly].[版本] .DestClass .即使这些类是相同的,也会发生这种情况.

The standard NET binary serializer is not well suited for data exchange between 2 different assemblies. When you go to deserialize, you'll get an an error similar to [Culture].[Assembly].[Version].SourceClass cannot be deserialized to [Culture].[Assembly].[Version].DestClass. This will happen even if the classes are identical.

有几种解决方法.A)在两侧使用相同的服务DLL进行序列化B)通过使用覆盖报告匹配的Culture-Assembly-Version-Class来诱使它进行反序列化,但这似乎有些可疑或C)使用XML序列化,但这使得以获得非常罗y的输出,这也是可读的.

There are several ways around this. A) Use the same service DLL on both sides to do the serializing B) trick it into deserializing by using an override to report a matching Culture-Assembly-Version-Class, but that seems dodgy or C) use XML serialization, but that makes for very wordy output, which is also readable.

对于二进制序列化,而不是.NET二进制格式化程序,有 ProtoBuf-NET 速度更快,产生的输出要小得多,并且使用几乎相同的语法.

For Binary Serialization, rather than the NET binary formatter, there is ProtoBuf-NET which is faster, produces much smaller output and uses nearly identical syntax.

两个程序之间的类必须如何精确匹配

ProtoBuf使用数字索引而不是属性名称,因此它们不必太相似.当然必须有一些相似性,否则目的地可能不知道数据代表什么.该类中的代码可以保持不变.

ProtoBuf uses a numeric index rather than property name, so they shouldn't have to be too similar. Of course there has to be some similarity or the destination may not have a clue what the data represents. The code in the class can be quite different because it stays put.

这篇关于从不同位置序列化和反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆