.NET Core 序列化对象输出字节数大小比较

写代码验证了一下 .NET Core 中序列化对象输出字节数大小,.NET Core 版本是 3.0.100-preview8-013656 ,对象属性使用了 Guid 与 DateTime 类型,胜出的是 Newtonsoft.Json.Bson ,序列化输出的字节数是 79

更新:后来同事用 MessagePack 对比了一下,结果让人大吃一惊,MessagePack 序列化输出的字节数只有31,甩其他序列化工具一条街。

//MessagePack
CompositeResolver.RegisterAndSetAsDefault(
    UnsafeBinaryResolver.Instance,
    StandardResolver.Instance);
var msgPack = MessagePackSerializer.Serialize(token);
Console.WriteLine(Encoding.ASCII.GetString(bytes));
Console.WriteLine(msgPack.Length);
//Length is 31

注:使用 MessagePack 时需要给每个属性加上 [Key] 标记

代码如下

[Serializable]
public class Token
{
    public List TokenItems { get; set; } = new List();

    public void Add(Guid userId)
    {
        TokenItems.Add(new TokenItem { UserId = userId, Expiration = DateTime.Now });
    }
}

public class StringToken
{
    public List TokenItems { get; set; } = new List();

    public void Add(Guid userId)
    {
        TokenItems.Add(userId.ToString() + ";" + DateTime.Now.ToString("yyyy-MM-dd"));
    }
}

[Serializable]
public class TokenItem
{
    public Guid UserId { get; set; }
    public DateTime Expiration { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var token = new Token();
        token.Add(Guid.NewGuid());

        //System.Text.Json in .NET Core
        var json = System.Text.Json.JsonSerializer.Serialize(token);
        Console.WriteLine(json);
        var byteCount = Encoding.ASCII.GetByteCount(json);
        Console.WriteLine(byteCount);
        //Length is 115

        //Json.NET
        json = JsonConvert.SerializeObject(token);
        Console.WriteLine(json);
        byteCount = Encoding.ASCII.GetByteCount(json);
        Console.WriteLine(byteCount);
        //Length is 115

        //Newtonsoft.Json.Bson
        using var ms = new MemoryStream();
        using var writer = new BsonDataWriter(ms);
        var serializer = new Newtonsoft.Json.JsonSerializer();
        serializer.Serialize(writer, token);
        var bytes = ms.ToArray();
        Console.WriteLine(Encoding.ASCII.GetString(bytes));
        Console.WriteLine(bytes.Length);
        //Length is 79
        var data = Convert.ToBase64String(bytes);
        Console.WriteLine(data);
        Console.WriteLine(data.Length);
        //Length is 108

        //BinaryFormatter
        using var ms2 = new MemoryStream();
        new BinaryFormatter().Serialize(ms2, token);
        bytes = ms2.ToArray();
        Console.WriteLine(Encoding.ASCII.GetString(bytes));
        Console.WriteLine(bytes.Length);
        //Length is 756

        //Newtonsoft.Json.Bson with StringToken
        var stringToken = new StringToken();
        stringToken.Add(Guid.NewGuid());
        using var ms3 = new MemoryStream();
        using var writer3 = new BsonDataWriter(ms3);            
        serializer.Serialize(writer3, stringToken);
        bytes = ms3.ToArray();
        Console.WriteLine(Encoding.ASCII.GetString(bytes));
        Console.WriteLine(bytes.Length);
        //Length is 77
        data = Convert.ToBase64String(bytes);
        Console.WriteLine(data);
        Console.WriteLine(data.Length);
        //Length is 104
    }
}

你可能感兴趣的:(.NET Core 序列化对象输出字节数大小比较)