public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { var header = reader.ReadExtensionFormatHeader(); if (header.TypeCode != TypeCode) { throw new MessagePackSerializationException("Extension TypeCode is invalid. typeCode: " + header.TypeCode); } if (header.Length != sizeof(T)) { throw new MessagePackSerializationException("Extension Length is invalid. actual: " + header.Length + ", expected: " + sizeof(T)); } var sequence = reader.ReadRaw(sizeof(T)); if (sequence.IsSingleSegment) { return(Unsafe.As <byte, T>(ref Unsafe.AsRef(sequence.FirstSpan[0]))); } T answer; sequence.CopyTo(new Span <byte>(&answer, sizeof(T))); return(answer); }
public ReadOnlySequence <T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return(ReadOnlySequence <T> .Empty); } var header = reader.ReadExtensionFormatHeader(); if (header.TypeCode != TypeCode) { throw new MessagePackSerializationException("Extension TypeCode is invalid. typeCode: " + header.TypeCode); } if (header.Length == 0) { return(ReadOnlySequence <T> .Empty); } var elementCount = header.Length / sizeof(T); if (elementCount * sizeof(T) != header.Length) { throw new MessagePackSerializationException("Extension Length is invalid. actual: " + header.Length + ", element size: " + sizeof(T)); } var answer = new T[elementCount]; reader.ReadRaw(header.Length).CopyTo(MemoryMarshal.AsBytes(answer.AsSpan())); return(new ReadOnlySequence <T>(answer)); }
public void CompressionData() { var originalData = Enumerable.Range(1, 1000).Select(x => x).ToArray(); var lz4Data = lz4Serializer.Serialize(originalData); var reader = new MessagePackReader(lz4Data); reader.NextMessagePackType.Is(MessagePackType.Extension); var header = reader.ReadExtensionFormatHeader(); header.TypeCode.Is((sbyte)LZ4MessagePackSerializer.ExtensionTypeCode); var decompress = lz4Serializer.Deserialize <int[]>(lz4Data); decompress.IsCollection(originalData); }
public void CompressionData() { var originalData = Enumerable.Range(1, 1000).Select(x => x).ToArray(); var lz4Data = MessagePackSerializer.Serialize(originalData, MessagePackSerializerOptions.LZ4Standard); PeekMessagePackType(lz4Data).Is(MessagePackType.Extension); var lz4DataReader = new MessagePackReader(lz4Data); ExtensionHeader header = lz4DataReader.ReadExtensionFormatHeader(); header.TypeCode.Is(MessagePackSerializer.LZ4ExtensionTypeCode); var decompress = MessagePackSerializer.Deserialize <int[]>(lz4Data, MessagePackSerializerOptions.LZ4Standard); decompress.Is(originalData); }
public void WriteExtensionFormatHeader_NegativeExtension() { var sequence = new Sequence <byte>(); var writer = new MessagePackWriter(sequence); var header = new ExtensionHeader(-1, 10); writer.WriteExtensionFormatHeader(header); writer.Flush(); var written = sequence.AsReadOnlySequence; var reader = new MessagePackReader(written); var readHeader = reader.ReadExtensionFormatHeader(); Assert.Equal(header.TypeCode, readHeader.TypeCode); Assert.Equal(header.Length, readHeader.Length); }
public void NonGenericAPI() { var originalData = Enumerable.Range(1, 100).Select(x => new FirstSimpleData { Prop1 = x * x, Prop2 = "hoge", Prop3 = x }).ToArray(); var lz4Data = LZ4MessagePackSerializer.NonGeneric.Serialize(typeof(FirstSimpleData[]), originalData); var reader = new MessagePackReader(lz4Data); reader.GetMessagePackType().Is(MessagePackType.Extension); var header = reader.ReadExtensionFormatHeader(); header.TypeCode.Is((sbyte)LZ4MessagePackSerializer.ExtensionTypeCode); var decompress = LZ4MessagePackSerializer.NonGeneric.Deserialize(typeof(FirstSimpleData[]), lz4Data); decompress.IsStructuralEqual(originalData); }
public void NonGenericAPI() { FirstSimpleData[] originalData = Enumerable.Range(1, 100).Select(x => new FirstSimpleData { Prop1 = x * x, Prop2 = "hoge", Prop3 = x }).ToArray(); var lz4Data = MessagePackSerializer.Serialize(typeof(FirstSimpleData[]), originalData, MessagePackSerializerOptions.LZ4Standard); PeekMessagePackType(lz4Data).Is(MessagePackType.Extension); var lz4DataReader = new MessagePackReader(lz4Data); ExtensionHeader header = lz4DataReader.ReadExtensionFormatHeader(); header.TypeCode.Is(MessagePackSerializer.LZ4ExtensionTypeCode); var decompress = MessagePackSerializer.Deserialize(typeof(FirstSimpleData[]), lz4Data, MessagePackSerializerOptions.LZ4Standard); decompress.IsStructuralEqual(originalData); }
public void ReadExtensionFormatHeader_MitigatesLargeAllocations() { var sequence = new Sequence <byte>(); var writer = new MessagePackWriter(sequence); writer.WriteExtensionFormatHeader(new ExtensionHeader(3, 1)); writer.WriteRaw(new byte[1]); writer.Flush(); Assert.Throws <EndOfStreamException>(() => { var truncatedReader = new MessagePackReader(sequence.AsReadOnlySequence.Slice(0, sequence.Length - 1)); truncatedReader.ReadExtensionFormatHeader(); }); var reader = new MessagePackReader(sequence); reader.ReadExtensionFormatHeader(); }
public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return(null); } if (reader.NextMessagePackType == MessagePackType.Extension) { MessagePackReader peekReader = reader.CreatePeekReader(); ExtensionHeader ext = peekReader.ReadExtensionFormatHeader(); if (ext.TypeCode == ThisLibraryExtensionTypeCodes.TypelessFormatter) { reader = peekReader; // commit the experimental read made earlier. // it has type name serialized ReadOnlySequence <byte> typeName = reader.ReadStringSequence().Value; ArraySegment <byte> typeNameArraySegment; byte[] rented = null; if (!typeName.IsSingleSegment || !MemoryMarshal.TryGetArray(typeName.First, out typeNameArraySegment)) { rented = ArrayPool <byte> .Shared.Rent((int)typeName.Length); typeName.CopyTo(rented); typeNameArraySegment = new ArraySegment <byte>(rented, 0, (int)typeName.Length); } var result = this.DeserializeByTypeName(typeNameArraySegment, ref reader, options); if (rented != null) { ArrayPool <byte> .Shared.Return(rented); } return(result); } } // fallback return(DynamicObjectTypeFallbackFormatter.Instance.Deserialize(ref reader, options)); }
public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return(null); } if (reader.NextMessagePackType == MessagePackType.Extension) { ExtensionHeader ext = reader.ReadExtensionFormatHeader(); if (ext.TypeCode == TypelessFormatter.ExtensionTypeCode) { // it has type name serialized ReadOnlySequence <byte> typeName = reader.ReadStringSegment(); ArraySegment <byte> typeNameArraySegment; byte[] rented = null; if (!typeName.IsSingleSegment || !MemoryMarshal.TryGetArray(typeName.First, out typeNameArraySegment)) { rented = ArrayPool <byte> .Shared.Rent((int)typeName.Length); typeName.CopyTo(rented); typeNameArraySegment = new ArraySegment <byte>(rented, 0, (int)typeName.Length); } var result = this.DeserializeByTypeName(typeNameArraySegment, ref reader, options); if (rented != null) { ArrayPool <byte> .Shared.Return(rented); } return(result); } } // fallback return(Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter <object>().Deserialize(ref reader, options)); }
static T DeserializeWithLengthPrefixExt <T>(Stream stream, IFormatterResolver resolver) { const int ExtTypeCode = 111; // sample ext code using (var output = new ArrayBufferWriter(16)) { MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, output); var reader = new MessagePackReader(output.WrittenSpan); var header = reader.ReadExtensionFormatHeader(); if (header.TypeCode == ExtTypeCode) { // memo, read fully var valueSpan = reader.Peek((int)header.Length); var reader0 = new MessagePackReader(valueSpan); return(resolver.GetFormatter <T>().Deserialize(ref reader0, resolver)); } else { throw new Exception(); } } }
public T[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return(null); } ExtensionHeader header = reader.ReadExtensionFormatHeader(); if (header.TypeCode != this.TypeCode) { throw new InvalidOperationException("Invalid typeCode."); } var byteLength = reader.ReadInt32(); var isLittleEndian = reader.ReadBoolean(); // Allocate a T[] that we will return. We'll then cast the T[] as byte[] so we can copy the byte sequence directly into it. var result = new T[byteLength / Marshal.SizeOf <T>()]; Span <byte> resultAsBytes = MemoryMarshal.Cast <T, byte>(result); reader.ReadRaw(byteLength).CopyTo(resultAsBytes); // Reverse the byte order if necessary. if (isLittleEndian != BitConverter.IsLittleEndian) { for (int i = 0, j = resultAsBytes.Length - 1; i < j; i++, j--) { byte tmp = resultAsBytes[i]; resultAsBytes[i] = resultAsBytes[j]; resultAsBytes[j] = tmp; } } return(result); }
public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { MessagePackType type = reader.NextMessagePackType; IFormatterResolver resolver = options.Resolver; switch (type) { case MessagePackType.Integer: var code = reader.NextCode; if (code >= MessagePackCode.MinNegativeFixInt && code <= MessagePackCode.MaxNegativeFixInt) { return(reader.ReadSByte()); } else if (code >= MessagePackCode.MinFixInt && code <= MessagePackCode.MaxFixInt) { return(reader.ReadByte()); } else if (code == MessagePackCode.Int8) { return(reader.ReadSByte()); } else if (code == MessagePackCode.Int16) { return(reader.ReadInt16()); } else if (code == MessagePackCode.Int32) { return(reader.ReadInt32()); } else if (code == MessagePackCode.Int64) { return(reader.ReadInt64()); } else if (code == MessagePackCode.UInt8) { return(reader.ReadByte()); } else if (code == MessagePackCode.UInt16) { return(reader.ReadUInt16()); } else if (code == MessagePackCode.UInt32) { return(reader.ReadUInt32()); } else if (code == MessagePackCode.UInt64) { return(reader.ReadUInt64()); } throw new MessagePackSerializationException("Invalid primitive bytes."); case MessagePackType.Boolean: return(reader.ReadBoolean()); case MessagePackType.Float: if (reader.NextCode == MessagePackCode.Float32) { return(reader.ReadSingle()); } else { return(reader.ReadDouble()); } case MessagePackType.String: return(reader.ReadString()); case MessagePackType.Binary: // We must copy the sequence returned by ReadBytes since the reader's sequence is only valid during deserialization. return(reader.ReadBytes()?.ToArray()); case MessagePackType.Extension: ExtensionHeader ext = reader.ReadExtensionFormatHeader(); if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime) { return(reader.ReadDateTime(ext)); } throw new MessagePackSerializationException("Invalid primitive bytes."); case MessagePackType.Array: { var length = reader.ReadArrayHeader(); if (length == 0) { return(Array.Empty <object>()); } IMessagePackFormatter <object> objectFormatter = resolver.GetFormatter <object>(); var array = new object[length]; options.Security.DepthStep(ref reader); try { for (int i = 0; i < length; i++) { array[i] = objectFormatter.Deserialize(ref reader, options); } } finally { reader.Depth--; } return(array); } case MessagePackType.Map: { var length = reader.ReadMapHeader(); IMessagePackFormatter <object> objectFormatter = resolver.GetFormatter <object>(); var hash = new Dictionary <object, object>(length, options.Security.GetEqualityComparer <object>()); options.Security.DepthStep(ref reader); try { for (int i = 0; i < length; i++) { var key = objectFormatter.Deserialize(ref reader, options); var value = objectFormatter.Deserialize(ref reader, options); hash.Add(key, value); } } finally { reader.Depth--; } return(hash); } case MessagePackType.Nil: reader.ReadNil(); return(null); default: throw new MessagePackSerializationException("Invalid primitive bytes."); } }
public object Deserialize(ref MessagePackReader reader, IFormatterResolver resolver) { var type = reader.NextMessagePackType; switch (type) { case MessagePackType.Integer: var code = reader.NextCode; if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) { return(reader.ReadSByte()); } else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) { return(reader.ReadByte()); } else if (code == MessagePackCode.Int8) { return(reader.ReadSByte()); } else if (code == MessagePackCode.Int16) { return(reader.ReadInt16()); } else if (code == MessagePackCode.Int32) { return(reader.ReadInt32()); } else if (code == MessagePackCode.Int64) { return(reader.ReadInt64()); } else if (code == MessagePackCode.UInt8) { return(reader.ReadByte()); } else if (code == MessagePackCode.UInt16) { return(reader.ReadUInt16()); } else if (code == MessagePackCode.UInt32) { return(reader.ReadUInt32()); } else if (code == MessagePackCode.UInt64) { return(reader.ReadUInt64()); } throw new InvalidOperationException("Invalid primitive bytes."); case MessagePackType.Boolean: return(reader.ReadBoolean()); case MessagePackType.Float: if (MessagePackCode.Float32 == reader.NextCode) { return(reader.ReadSingle()); } else { return(reader.ReadDouble()); } case MessagePackType.String: return(reader.ReadString()); case MessagePackType.Binary: return(reader.ReadBytes()); case MessagePackType.Extension: var ext = reader.ReadExtensionFormatHeader(); if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime) { return(reader.ReadDateTime(ext)); } throw new InvalidOperationException("Invalid primitive bytes."); case MessagePackType.Array: { var length = reader.ReadArrayHeader(); var objectFormatter = resolver.GetFormatter <object>(); var array = new object[length]; for (int i = 0; i < length; i++) { array[i] = objectFormatter.Deserialize(ref reader, resolver); } return(array); } case MessagePackType.Map: { var length = reader.ReadMapHeader(); var objectFormatter = resolver.GetFormatter <object>(); var hash = new Dictionary <object, object>(length); for (int i = 0; i < length; i++) { var key = objectFormatter.Deserialize(ref reader, resolver); var value = objectFormatter.Deserialize(ref reader, resolver); hash.Add(key, value); } return(hash); } case MessagePackType.Nil: reader.ReadNil(); return(null); default: throw new InvalidOperationException("Invalid primitive bytes."); } }