예제 #1
0
        static void SerializeWithLengthPrefixExt <T>(Stream stream, T data, IFormatterResolver resolver)
        {
            const int ExtTypeCode = 111; // sample ext code

            byte[] buffer    = null;
            var    formatter = resolver.GetFormatter <T>();
            var    dataSize  = formatter.Serialize(ref buffer, 0, data, resolver);

            var headerLength = MessagePackBinary.GetExtensionFormatHeaderLength(dataSize);

            MessagePackBinary.EnsureCapacity(ref buffer, 0, headerLength);
            Buffer.BlockCopy(buffer, 0, buffer, headerLength, dataSize);
            MessagePackBinary.WriteExtensionFormatHeader(ref buffer, 0, ExtTypeCode, dataSize);

            stream.Write(buffer, 0, dataSize + headerLength);
        }
        static void SerializeWithLengthPrefixExt <T>(Stream stream, T data, IFormatterResolver resolver)
        {
            const int ExtTypeCode = 111; // sample ext code

            var serializedData = MessagePackSerializer.Serialize(data, resolver);
            var tmp            = MessagePackSerializer.Deserialize <T>(serializedData, resolver);

            var dataSize = serializedData.Length;
            var idx      = 0;
            var writer   = new MessagePackWriter(16);

            var headerLength = MessagePackBinary.GetExtensionFormatHeaderLength(dataSize);

            writer.Ensure(idx, headerLength + dataSize);
            writer.WriteExtensionFormatHeader(ExtTypeCode, dataSize, ref idx);
            UnsafeMemory.WriteRaw(ref writer, serializedData, ref idx);
            var buffer = writer.ToArray(idx);

            stream.Write(buffer);
        }