开源!一款功能强大的高性能二进制序列化器Bssom.Net (6)

方法用于 将给定的值序列化为Bssom二进制,高性能的内部实现,以下是部分常用方法,每个方法都拥有CancellationToken的重载

//直接对对象进行序列化,将返回一个被序列化后的字节数组 object value = RandomHelper.RandomValue<object>(); byte[] binary = BssomSerializer.Serialize(value, option: BssomSerializerOptions.Default); //将对象序列化到指定的字节数组中,若容量不够将自动扩容,最终返回序列化的字节数 object value = RandomHelper.RandomValue<object>(); byte[] buf = local(); int serializeSize = BssomSerializer.Serialize(ref buf, 0, value, option: BssomSerializerOptions.Default); //将对象序列化到自定义的写入器中 object value = RandomHelper.RandomValue<object>(); IBssomBufferWriter writer = new Impl(); BssomSerializer.Serialize(value, writer, option: BssomSerializerOptions.Default); //使用序列化上下文进行序列化 object value = RandomHelper.RandomValue<object>(); BssomSerializeContext context = new BssomSerializeContext(BssomSerializerOptions.Default); byte[] binary = BssomSerializer.Serialize(ref context, value); //将对象序列化到流中 object value = RandomHelper.RandomValue<object>(); Stream stream = new MemoryStream(); BssomSerializer.Serialize(stream, value, option: BssomSerializerOptions.Default); //异步的将对象序列化到流中 object value = RandomHelper.RandomValue<object>(); Stream stream = new MemoryStream(); await BssomSerializer.SerializeAsync(stream, value, option: BssomSerializerOptions.Default); Deserialize

方法用于 将给定的Bssom缓冲区反序列化为对象,高性能的内部实现,以下是部分常用方法,每个方法都拥有CancellationToken的重载

//从给定的字节数组中反序列化对象 byte[] buf = remote(); T value = BssomSerializer.Deserialize<T>(buf, 0, out int readSize, option: BssomSerializerOptions.Default); //从给定的buffer中反序列化对象 IBssomBuffer buffer = remote(); object value = BssomSerializer.Deserialize<object>(buffer, option: BssomSerializerOptions.Default); //使用上下文从给定的buffer中反序列化对象 BssomDeserializeContext context = new BssomDeserializeContext(BssomSerializerOptions.Default); IBssomBuffer buffer = remote(); object value = BssomSerializer.Deserialize<object>(ref context, buffer); //从流中反序列化对象 Stream stream = remote(); object value = BssomSerializer.Deserialize<object>(stream, option: BssomSerializerOptions.Default); //异步的从流中反序列化对象 Stream stream = remote(); object value = await BssomSerializer.DeserializeAsync<object>(stream, option: BssomSerializerOptions.Default); //传递一个Type, 从流中反序列化对象为指定的Type类型 Stream stream = remote(); Type type = typeof(class); object value = BssomSerializer.Deserialize(stream, type, option: BssomSerializerOptions.Default); //传递一个Type, 异步的从流中反序列化对象为指定的Type类型 Stream stream = remote(); Type type = typeof(class); object value = await BssomSerializer.DeserializeAsync(stream, type, option: BssomSerializerOptions.Default); ReadValue

方法用于 在二进制数据中仅读取某一个值,如果你只想读取对象中的某一个值,而不用完整的反序列化它,那么这个方法非常有用

//通过内嵌的简单字段访问语言,获取Dict中的一个Key对应的值 var val = new Dictionary<string, object>() { { "A",(int)3}, { "B",(DateTime)DateTime.MaxValue}, }; var buf = BssomSerializer.Serialize(val); var bsfm = new BssomFieldMarshaller(buf); BssomFieldOffsetInfo fieldOffInfo = bsfm.IndexOf("[A]") bsfm.ReadValue<int>(fieldOffInfo).Is(3); //通过内嵌的简单字段访问语言,获取class中的一个属性的值 var val = new MyClass() { Name = "bssom", Nature = "Binary" }; var buf = BssomSerializer.Serialize(val); var bsfm = new BssomFieldMarshaller(buf); BssomFieldOffsetInfo fieldOffInfo = bsfm.IndexOf("[Name]") bsfm.ReadValue<string>(fieldOffInfo).Is("bssom"); //通过内嵌的简单字段访问语言,获取数组中的一个属性的值 var val = new object[] { (int)1,(double)2.2 } var buf = BssomSerializer.Serialize(val); var bsfm = new BssomFieldMarshaller(buf); BssomFieldOffsetInfo fieldOffInfo = bsfm.IndexOf("$1") bsfm.ReadValue<double>(fieldOffInfo).Is((double)2.2); //通过内嵌的简单字段访问语言,组合获取一个对象 var val = new MyClass() { Name = "bssom", Nature = "Binary", Data = new int[] { 3, 2, 1} }; var buf = BssomSerializer.Serialize(val); var bsfm = new BssomFieldMarshaller(buf); BssomFieldOffsetInfo fieldOffInfo = bsfm.IndexOf("[Data]$1") bsfm.ReadValue<int>(fieldOffInfo).Is(2); //通过自定义的字段访问形式,组合获取一个对象 var val = new Dictionary<object, object>() { { DateTime.Parse("2018-01-01"), new object[]{'A','B'} }, { "Charec",(DateTime)DateTime.MaxValue}, }; var buf = BssomSerializer.Serialize(val); var bsfm = new BssomFieldMarshaller(buf); IIndexOfInputSource input = new IndexOfObjectsInputSource(new Entry[]{ new Entry(DateTime.Parse("2018-01-01"),ValueIsMapKey: true), new Entry(1,ValueIsMapKey: false), }) BssomFieldOffsetInfo fieldOffInfo = bsfm.IndexOf(input) bsfm.ReadValue<int>(fieldOffInfo).Is('B'); ReadAllMapKeys

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwxfj.html