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.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(Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter <object>().Deserialize(ref reader, options)); }
public void StringSegmentTest(string target) { (MemoryStream stream, MsgPack.Packer packer) = this.CreateReferencePacker(); var sequence = new Sequence <byte>(); var writer = new MessagePackWriter(sequence); writer.Write(target); writer.Flush(); var returnLength = sequence.Length; MsgPack.Packer referencePacked = packer.PackString(target); referencePacked.Position.Is(returnLength); stream.ToArray().SequenceEqual(sequence.AsReadOnlySequence.ToArray()).IsTrue(); var sequenceReader = new MessagePackReader(sequence.AsReadOnlySequence); var segment = sequenceReader.ReadStringSequence().Value.ToArray(); Encoding.UTF8.GetString(segment).Is(target); sequenceReader.End.IsTrue(); this.CreateUnpackedReference(sequence).AsStringUtf8().Is(target); }
public void TryReadStringSpan_Fragmented() { var contiguousSequence = new Sequence <byte>(); var writer = new MessagePackWriter(contiguousSequence); var expected = new byte[] { 0x1, 0x2, 0x3 }; writer.WriteString(expected); writer.Flush(); var fragmentedSequence = BuildSequence( contiguousSequence.AsReadOnlySequence.First.Slice(0, 2), contiguousSequence.AsReadOnlySequence.First.Slice(2)); var reader = new MessagePackReader(fragmentedSequence); Assert.False(reader.TryReadStringSpan(out ReadOnlySpan <byte> span)); Assert.Equal(0, span.Length); // After failing to read the span, a caller should still be able to read it as a sequence. var actualSequence = reader.ReadStringSequence(); Assert.True(actualSequence.HasValue); Assert.False(actualSequence.Value.IsSingleSegment); Assert.Equal(new byte[] { 1, 2, 3 }, actualSequence.Value.ToArray()); }
public Guid Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { ReadOnlySequence <byte> segment = reader.ReadStringSequence().Value; if (segment.Length != 36) { throw new MessagePackSerializationException("Unexpected length of string."); } GuidBits result; if (segment.IsSingleSegment) { result = new GuidBits(segment.First.Span); } else { Span <byte> bytes = stackalloc byte[36]; segment.CopyTo(bytes); result = new GuidBits(bytes); } return(result.Value); }
public decimal Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (!(reader.ReadStringSequence() is ReadOnlySequence <byte> sequence)) { throw new MessagePackSerializationException(string.Format("Unexpected msgpack code {0} ({1}) encountered.", MessagePackCode.Nil, MessagePackCode.ToFormatName(MessagePackCode.Nil))); } if (sequence.IsSingleSegment) { var span = sequence.First.Span; if (System.Buffers.Text.Utf8Parser.TryParse(span, out decimal result, out var bytesConsumed)) { if (span.Length != bytesConsumed) { throw new MessagePackSerializationException("Unexpected length of string."); } return(result); } } else { // sequence.Length is not free var seqLen = (int)sequence.Length; if (seqLen < 128) { Span <byte> span = stackalloc byte[seqLen]; sequence.CopyTo(span); if (System.Buffers.Text.Utf8Parser.TryParse(span, out decimal result, out var bytesConsumed)) { if (seqLen != bytesConsumed) { throw new MessagePackSerializationException("Unexpected length of string."); } return(result); } } else { var rentArray = ArrayPool <byte> .Shared.Rent(seqLen); try { sequence.CopyTo(rentArray); if (System.Buffers.Text.Utf8Parser.TryParse(rentArray.AsSpan(0, seqLen), out decimal result, out var bytesConsumed)) { if (seqLen != bytesConsumed) { throw new MessagePackSerializationException("Unexpected length of string."); } return(result); } } finally { ArrayPool <byte> .Shared.Return(rentArray); } } } throw new MessagePackSerializationException("Can't parse to decimal, input string was not in a correct format."); }
public global::PerfBenchmarkDotNet.StringKeySerializerTarget Deserialize(ref MessagePackReader reader, newmsgpack::MessagePack.MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return(null); } var length = reader.ReadMapHeader(); var __MyProperty1__ = default(int); var __MyProperty2__ = default(int); var __MyProperty3__ = default(int); var __MyProperty4__ = default(int); var __MyProperty5__ = default(int); var __MyProperty6__ = default(int); var __MyProperty7__ = default(int); var __MyProperty8__ = default(int); var __MyProperty9__ = default(int); for (int i = 0; i < length; i++) { ReadOnlySequence <byte> stringKey = reader.ReadStringSequence().Value; int key; if (!this.keyMapping.TryGetValue(stringKey, out key)) { reader.Skip(); continue; } switch (key) { case 0: __MyProperty1__ = reader.ReadInt32(); break; case 1: __MyProperty2__ = reader.ReadInt32(); break; case 2: __MyProperty3__ = reader.ReadInt32(); break; case 3: __MyProperty4__ = reader.ReadInt32(); break; case 4: __MyProperty5__ = reader.ReadInt32(); break; case 5: __MyProperty6__ = reader.ReadInt32(); break; case 6: __MyProperty7__ = reader.ReadInt32(); break; case 7: __MyProperty8__ = reader.ReadInt32(); break; case 8: __MyProperty9__ = reader.ReadInt32(); break; default: reader.Skip(); break; } } var ____result = new global::PerfBenchmarkDotNet.StringKeySerializerTarget(); ____result.MyProperty1 = __MyProperty1__; ____result.MyProperty2 = __MyProperty2__; ____result.MyProperty3 = __MyProperty3__; ____result.MyProperty4 = __MyProperty4__; ____result.MyProperty5 = __MyProperty5__; ____result.MyProperty6 = __MyProperty6__; ____result.MyProperty7 = __MyProperty7__; ____result.MyProperty8 = __MyProperty8__; ____result.MyProperty9 = __MyProperty9__; return(____result); }
public StringKeySerializerTarget Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { if (reader.TryReadNil()) { return(null); } int num3 = reader.ReadMapHeader(); int myProperty = 0; int myProperty2 = 0; int myProperty3 = 0; int myProperty4 = 0; int myProperty5 = 0; int myProperty6 = 0; int myProperty7 = 0; int myProperty8 = 0; int myProperty9 = 0; for (int i = 0; i < num3; i++) { int num4; ReadOnlySequence <byte> segment = reader.ReadStringSequence().Value; bool arg_47_0 = this.keyMapping.TryGetValue(segment, out num4); if (!arg_47_0) { reader.Skip(); } else { switch (num4) { case 0: myProperty = reader.ReadInt32(); break; case 1: myProperty2 = reader.ReadInt32(); break; case 2: myProperty3 = reader.ReadInt32(); break; case 3: myProperty4 = reader.ReadInt32(); break; case 4: myProperty5 = reader.ReadInt32(); break; case 5: myProperty6 = reader.ReadInt32(); break; case 6: myProperty7 = reader.ReadInt32(); break; case 7: myProperty8 = reader.ReadInt32(); break; case 8: myProperty9 = reader.ReadInt32(); break; default: reader.Skip(); break; } } } return(new StringKeySerializerTarget { MyProperty1 = myProperty, MyProperty2 = myProperty2, MyProperty3 = myProperty3, MyProperty4 = myProperty4, MyProperty5 = myProperty5, MyProperty6 = myProperty6, MyProperty7 = myProperty7, MyProperty8 = myProperty8, MyProperty9 = myProperty9, }); }