没有对象定义的二进制反序列化 [英] Binary deserialization without object definition

查看:111
本文介绍了没有对象定义的二进制反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取二进制序列化对象,我也没有对象定义。我在文件中找到了一个峰值并看到了属性名称,所以我手动重新创建了对象(让我们称之为SomeDataFormat)。



我最终得到了这个:



I'm trying to read a binary serialized object, that I don't have the object definition too. I took a peak into the file and saw property names, so I manually recreated the object (let's call it SomeDataFormat).

I ended up with this :

public class SomeDataFormat // 16 field
{
    public string Name{ get; set; }
    public int Country{ get; set; }
    public string UserEmail{ get; set; }
    public bool IsCaptchaDisplayed{ get; set; }
    public bool IsForgotPasswordCaptchaDisplayed{ get; set; }
    public bool IsSaveChecked{ get; set; }
    public string SessionId{ get; set; }
    public int SelectedLanguage{ get; set; }
    public int SelectedUiCulture{ get; set; }
    public int SecurityImageRefId{ get; set; }
    public int LogOnId{ get; set; }
    public bool BetaLogOn{ get; set; }
    public int Amount{ get; set; }
    public int CurrencyTo{ get; set; }
    public int Delivery{ get; set; }
    public bool displaySSN{ get; set; }
}



现在我可以像这样反序列化它:




Now I'm able to deserialize it like this :

BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Full; // original uses this
formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded; // this reduces size
FileStream readStream = new FileStream("data.dat", FileMode.Open);
SomeDataFormat data = (SomeDataFormat) formatter.Deserialize(readStream);





首先可疑的是,只有2个字符串(SessionId& UserEmail)在反序列化的* data *对象中有值。其他属性为null或只是0.这可能是有意的,但我仍然怀疑在反序列化过程中出现了问题。



第二个可疑的事情是我重新定义了这个对象,最终得到了不同的文件大小。原创(695bytes)。重新序列化的对象是698字节。所以有3bytes的区别。我应该获得与原始文件相同的文件大小。



查看原始文件和新的(重新编目的)文件:

最初的序列化文件。

重新编目的文件。



如您所见,在标题部分之后,数据似乎是以不同的顺序。例如,你可以看到电子邮件和sessionID不在同一个地方。



Q1:为什么这些值的顺序不同两个文件?

Q2:为什么比较2个序列化对象还有3个字节?

Q3:我缺少什么?我怎么能这样做?




任何tipps / help appreaciated。



First suspicious thing is that only the 2 string (SessionId&UserEmail) has value in the deserialized *data* object. The other properties are null or just 0. This might be intended, but still, I suspect that something has gone wrone during the deserialization.

The second suspicious thing is if I reserialize this object, I end up with different file sizes. Original (695bytes). Reserialized object is 698bytes. So there is 3bytes difference. I should get the same file size as the original.

Taking a look at the original, and the new (reserialized) file:
The originally serialized file.
The reserialized file.

As you can see, after the header section, the data appears to be in different order. For example, you can see that the email, and the sessionID is not at the same place.

Q1: Why are the values are in different order in the two files?
Q2: Why is there extra 3 bytes compared the 2 serialized objects?
Q3: What am I missing? How could I do this?


Any tipps/help appreaciated.

推荐答案

Binary序列化主要适合内部应用程序使用,对数据交换不是很有用。显而易见的原因是格式在不同的框架版本之间可能非常具体。更好的选择是使用xml或json格式化程序,因为这些格式或更松散。主要区别在于二进制格式使用预定义的数据长度。当此长度更改时,它可能与以前的版本不兼容。



祝你好运!
Binary serialization is mostly suitable for use by an application internally and not really useful for data interchange. The obvious reason is that the format can be very specific between different framework versions. A better alternative would be to use xml or json formatters because these formats or more loose. The main difference is that binary format uses predefined lengths for data. When this length changes it could become incompatible with previous versions.

Good luck!


这篇关于没有对象定义的二进制反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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