/// <summary> /// <para>将给定的值序列化到<paramref name="buffer"/>中</para> /// <para>Serializes a given value with the <paramref name="buffer"/></para> /// </summary> /// <param name="buffer">序列化所使用的buffer. The value to serialize</param> /// <param name="bufOffset">buffer起始写入的偏移量. The offset that the buffer starts writing to</param> /// <param name="value">要序列化的值. The value to serialize</param> /// <param name="option">使用的配置,如果为<c>null</c>,则使用默认配置. The options. Use <c>null</c> to use default options</param> /// <param name="cancellationToken">取消该操作的令牌. The token to cancel the operation</param> /// <returns>序列化所花费的字节数. The number of bytes spent on serialization</returns> public static int Serialize <T>(ref byte[] buffer, int bufOffset, T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (bufOffset > buffer.Length - 1) { throw new ArgumentException(nameof(bufOffset)); } if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); using (ExpandableBufferWriter exbuffer = new ExpandableBufferWriter(buffer, bufOffset)) { BssomWriter writer = new BssomWriter(exbuffer); option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); buffer = exbuffer.GetBufferedArrayWithKeepFirstBuffer(); return((int)exbuffer.Buffered); } }
/// <summary> /// <para>将给定的值序列化到<paramref name="bufferWriter"/></para> /// <para>Serializes a given value with the specified <paramref name="bufferWriter"/></para> /// </summary> /// <param name="context">序列化所需要的上下文. The context required for the serialization </param> /// <param name="value">要序列化的值. The value to serialize</param> /// <param name="bufferWriter">要序列化的缓冲区写入器. The buffer writer to serialize with</param> public static void Serialize <T>(ref BssomSerializeContext context, T value, IBssomBufferWriter bufferWriter) { if (context.Option == null) { context.Option = BssomSerializerOptions.Default; } BssomWriter writer = new BssomWriter(bufferWriter); context.Option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); }
/// <summary> /// <para>将给定的值序列化到内置的缓冲区</para> /// <para>Serializes a given value with the built-in buffer</para> /// </summary> /// <param name="context">序列化所需要的上下文. The context required for the serialization </param> /// <param name="value">要序列化的值. The value to serialize</param> /// <returns>具有序列化值的字节数组. A byte array with the serialized value</returns> public static byte[] Serialize <T>(ref BssomSerializeContext context, T value) { if (context.Option == null) { context.Option = BssomSerializerOptions.Default; } ExpandableBufferWriter buffer = ExpandableBufferWriter.CreateGlobar(); BssomWriter writer = new BssomWriter(buffer); context.Option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); return(buffer.GetBufferedArray()); }
/// <summary> /// <para>将给定的值序列化到内置的缓冲区中</para> /// <para>Serializes a given value with the built-in buffer</para> /// </summary> /// <param name="value">要序列化的值. The value to serialize</param> /// <param name="option">使用的配置,如果为<c>null</c>,则使用默认配置. The options. Use <c>null</c> to use default options</param> /// <param name="cancellationToken">取消该操作的令牌. The token to cancel the operation</param> /// <returns>具有序列化值的字节数组. A byte array with the serialized value</returns> public static byte[] Serialize <T>(T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); using (ExpandableBufferWriter buffer = ExpandableBufferWriter.CreateGlobar()) { BssomWriter writer = new BssomWriter(buffer); option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); return(buffer.GetBufferedArray()); } }
/// <summary> /// <para>将给定的值序列化到<paramref name="bufferWriter"/></para> /// <para>Serializes a given value with the specified <paramref name="bufferWriter"/></para> /// </summary> /// <param name="value">要序列化的值. The value to serialize</param> /// <param name="bufferWriter">要序列化的缓冲区写入器. The buffer writer to serialize with</param> /// <param name="option">使用的配置,如果为<c>null</c>,则使用默认配置. The options. Use <c>null</c> to use default options</param> /// <param name="cancellationToken">取消该操作的令牌. The token to cancel the operation</param> public static void Serialize <T>(T value, IBssomBufferWriter bufferWriter, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (bufferWriter == null) { throw new ArgumentNullException(nameof(bufferWriter)); } if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); BssomWriter writer = new BssomWriter(bufferWriter); option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); }