BinarySerializer的输出字符串似乎太长了。即使我将类的属性名称缩短为单个字符长度,它也不会改变。
这是类文件:
using System;
namespace Models.Accounts
{
/// <summary>
/// PasswordResetRequest class
/// model of a password reset request
/// </summary>
[Serializable]
public class PRR
{
/// <summary>
/// request id
/// </summary>
public string I { get; set; }
/// <summary>
/// time request received
/// </summary>
public DateTime T { get; set; }
/// <summary>
/// application to which the password belongs
/// </summary>
public string A { get; set; }
/// <summary>
/// username whose password needs to be reset
/// </summary>
public string U { get; set; }
/// <summary>
/// token (guid)
/// </summary>
public Guid G { get; set; }
/// <summary>
/// token used
/// </summary>
public bool D { get; set; }
/// <summary>
/// client ip
/// </summary>
public string C { get; set; }
}
}
我正在使用this SO link进行序列化。当我使用序列化器
时 PRR request = new PRR();
request.U = "someusername";
request.G = Guid.NewGuid();
string searlizedRequest = SingletonCommon.Instance.SerializeObject(request);
我为searlizedRequest
AAEAAAD/////AQAAAAAAAAAMAgAAAEFMSVQuTW9kZWxzLCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAF0xJVC5Nb2RlbHMuQWNjb3VudHMuUFJSBwAAABI8ST5rX19CYWNraW5nRmllbGQSPFQ+a19fQmFja2luZ0ZpZWxkEjxBPmtfX0JhY2tpbmdGaWVsZBI8VT5rX19CYWNraW5nRmllbGQSPEc+a19fQmFja2luZ0ZpZWxkEjxEPmtfX0JhY2tpbmdGaWVsZBI8Qz5rX19CYWNraW5nRmllbGQBAAEBAwABDQtTeXN0ZW0uR3VpZAECAAAACgAAAAAAAAAACgYDAAAADHNvbWV1c2VybmFtZQT8////C1N5c3RlbS5HdWlkCwAAAAJfYQJfYgJfYwJfZAJfZQJfZgJfZwJfaAJfaQJfagJfawAAAAAAAAAAAAAACAcHAgICAgICAgL20za6r7D0QbKWb7tG1cjSAAoL
是否有我可以使用的输出更短的格式化程序或我可以尝试的任何其他内容,因为此字符串将成为电子邮件中发送的链接的一部分。
答案 0 :(得分:0)
也许在写到具有最小大小的文件时,您可以尝试GZipStream
压缩输出
public static void SetCompressed<T>(T data,string name)
{
BinaryFormatter bf;
using(FileStream fileStream = new FileStream(name+ ".object", FileMode.Create, FileAccess.Write))
using(GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress))
{
bf.Serialize(compressionStream, data);
}
}
public static T GetCompressed<T>(string name)
{
BinaryFormatter bf;
T Tobject;
using(FileStream fileStream = new FileStream(name + ".object", FileMode.Open, FileAccess.Read))
using(GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
{
Tobject = (T)bf.Deserialize(compressionStream);
}
return Tobject;
}
以上代码使用BinaryFormatter
与FileStream
和GZipStream
一起序列化任何对象。 GZipStream
将在一定程度上压缩输出 *。object 文件。
HACK :您可以使用 7-Zip 打开 *。object 文件,以查看其中的序列化内容。