Exemplo n.º 1
0
        /// <summary>
        /// Serializes a given value with the specified buffer writer.
        /// </summary>
        /// <param name="writer">The buffer writer to serialize with.</param>
        /// <param name="value">The value to serialize.</param>
        /// <param name="options">The options. Use <c>null</c> to use default options.</param>
        public static void Serialize <T>(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options = null)
        {
            options = options ?? DefaultOptions;
            bool originalOldSpecValue = writer.OldSpec;

            if (options.OldSpec.HasValue)
            {
                writer.OldSpec = options.OldSpec.Value;
            }

            try
            {
                if (options.UseLZ4Compression)
                {
                    using (var scratch = new Nerdbank.Streams.Sequence <byte>())
                    {
                        MessagePackWriter scratchWriter = writer.Clone(scratch);
                        options.Resolver.GetFormatterWithVerify <T>().Serialize(ref scratchWriter, value, options);
                        scratchWriter.Flush();
                        ToLZ4BinaryCore(scratch.AsReadOnlySequence, ref writer);
                    }
                }
                else
                {
                    options.Resolver.GetFormatterWithVerify <T>().Serialize(ref writer, value, options);
                }
            }
            finally
            {
                writer.OldSpec = originalOldSpecValue;
            }
        }
 /// <summary>
 /// Serializes a given value with the specified buffer writer.
 /// </summary>
 /// <param name="writer">The buffer writer to serialize with.</param>
 /// <param name="value">The value to serialize.</param>
 /// <param name="options">The options. Use <c>null</c> to use default options.</param>
 public static void Serialize <T>(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options = null)
 {
     options = options ?? MessagePackSerializerOptions.Default;
     if (options.UseLZ4Compression)
     {
         using (var scratch = new Nerdbank.Streams.Sequence <byte>())
         {
             var scratchWriter = writer.Clone(scratch);
             options.Resolver.GetFormatterWithVerify <T>().Serialize(ref scratchWriter, value, options.Resolver);
             scratchWriter.Flush();
             ToLZ4BinaryCore(scratch.AsReadOnlySequence, ref writer);
         }
     }
     else
     {
         options.Resolver.GetFormatterWithVerify <T>().Serialize(ref writer, value, options.Resolver);
     }
 }