Пример #1
0
        /// <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);
            }
        }
Пример #2
0
        /// <summary>
        /// <para>将给定的值序列化到<paramref name="stream"/>中</para>
        /// <para>Serializes a given value with the specified <paramref name="stream"/></para>
        /// </summary>
        /// <param name="stream">要序列化的流. The stream to serialize with</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>具有序列化值的字节数组. A byte array with the serialized value</returns>
        public static void Serialize <T>(Stream stream, T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default)
        {
            if (option == null)
            {
                option = BssomSerializerOptions.Default;
            }

            BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken);

            Serialize(ref context, value, stream);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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());
        }
Пример #5
0
 /// <summary>
 /// <para>将给定的值序列化到<paramref name="stream"/>中</para>
 /// <para>Serializes a given value with the specified <paramref name="stream"/></para>
 /// </summary>
 /// <param name="context">序列化所需要的上下文. The context required for the serialization </param>
 /// <param name="value">要序列化的值. The value to serialize</param>
 /// <param name="stream">要序列化的流. The stream to serialize with</param>
 public static void Serialize <T>(ref BssomSerializeContext context, T value, Stream stream)
 {
     using (ExpandableBufferWriter buffer = ExpandableBufferWriter.CreateGlobar())
     {
         Serialize(ref context, value, buffer);
         try
         {
             buffer.CopyTo(stream, context.CancellationToken);
         }
         catch (Exception ex)
         {
             BssomSerializationOperationException.CopyStreamError(ex);
         }
     }
 }
Пример #6
0
        /// <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());
            }
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        /// <summary>
        /// <para>异步的将给定的值序列化到<paramref name="stream"/>中</para>
        /// <para>Asynchronously serializes a given value with the specified <paramref name="stream"/></para>
        /// </summary>
        /// <param name="stream">要序列化的流. The stream to serialize with</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>具有序列化值的字节数组. A byte array with the serialized value</returns>
        public static async Task SerializeAsync <T>(Stream stream, 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())
            {
                Serialize(ref context, value, buffer);
                try
                {
                    await buffer.CopyToAsync(stream, context.CancellationToken);
                }
                catch (Exception ex)
                {
                    BssomSerializationOperationException.CopyStreamError(ex);
                }
            }
        }