Add MessagePackReader
This replaces the (byte[]+offset+out bytesRead) reading methods.
This commit is contained in:
Родитель
79fbeb9b05
Коммит
91efb454b1
22
README.md
22
README.md
|
@ -4,7 +4,7 @@ MessagePack for C# (.NET, .NET Core, Unity, Xamarin)
|
|||
[![Build Status](https://dev.azure.com/ils0086/MessagePack-CSharp/_apis/build/status/MessagePack-CSharp-CI)](https://dev.azure.com/ils0086/MessagePack-CSharp/_build/latest?definitionId=2)
|
||||
[![NuGet](https://img.shields.io/nuget/v/MessagePack.svg)](https://www.nuget.org/packages/messagepack)
|
||||
[![Releases](https://img.shields.io/github/release/neuecc/MessagePack-CSharp.svg)](https://github.com/neuecc/MessagePack-CSharp/releases)
|
||||
|
||||
|
||||
[![Join the chat at https://gitter.im/MessagePack-CSharp/Lobby](https://badges.gitter.im/MessagePack-CSharp/Lobby.svg)](https://gitter.im/MessagePack-CSharp/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
The extremely fast [MessagePack](http://msgpack.org/) serializer for C#. It is 10x faster than [MsgPack-Cli](https://github.com/msgpack/msgpack-cli) and outperforms other C# serializers. MessagePack for C# also ships with built-in support for LZ4 compression - an extremely fast compression algorithm. Performance is important, particularly in applications like game development, distributed computing, microservice architecture, and caching.
|
||||
|
@ -781,7 +781,7 @@ IMessagePackFormatter is serializer by each type. For example `Int32Formatter :
|
|||
public interface IMessagePackFormatter<T>
|
||||
{
|
||||
int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver);
|
||||
T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
T Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver);
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -801,15 +801,14 @@ public class FileInfoFormatter<T> : IMessagePackFormatter<FileInfo>
|
|||
return MessagePackBinary.WriteString(ref bytes, offset, value.FullName);
|
||||
}
|
||||
|
||||
public FileInfo Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public FileInfo Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var path = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
var path = reader.ReadString();
|
||||
return new FileInfo(path);
|
||||
}
|
||||
}
|
||||
|
@ -850,7 +849,8 @@ Primitive API(MessagePackBinary)
|
|||
| FastResize | Buffer.BlockCopy version of Array.Resize. |
|
||||
| FastCloneWithResize | Same as FastResize but return copied byte[]. |
|
||||
|
||||
Read API returns deserialized primitive and read size. Write API returns write size and guranteed auto ensure ref byte[]. Write/Read API has `byte[]` overload and `Stream` overload, basically the byte[] API is faster.
|
||||
The Read API returns deserialized primitive and advances a `ReadOnlySequence<byte>` to consume the deserialized bytes. The Write API uses an `IBufferWriter<byte>` for efficient writing and buffer management.
|
||||
Serialization methods include overloads for other common types such as `byte[]` and `Stream`, and have some small overhead over using the primitive types directly.
|
||||
|
||||
DateTime is serialized to [MessagePack Timestamp format](https://github.com/msgpack/msgpack/blob/master/spec.md#formats-timestamp), it serialize/deserialize UTC and loses `Kind` info. If you use`NativeDateTimeResolver` serialized native DateTime binary format and it can keep `Kind` info but cannot communicate other platforms.
|
||||
|
||||
|
@ -1066,9 +1066,9 @@ public class CustomObject
|
|||
return formatterResolver.GetFormatterWithVerify<string>().Serialize(ref bytes, offset, value.internalId, formatterResolver);
|
||||
}
|
||||
|
||||
public CustomObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public CustomObject Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var id = formatterResolver.GetFormatterWithVerify<string>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var id = formatterResolver.GetFormatterWithVerify<string>().Deserialize(ref reader, formatterResolver);
|
||||
return new CustomObject { internalId = id };
|
||||
}
|
||||
}
|
||||
|
@ -1078,9 +1078,9 @@ public class CustomObject
|
|||
|
||||
public class Int_x10Formatter : IMessagePackFormatter<int>
|
||||
{
|
||||
public int Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public int Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize) * 10;
|
||||
return reader.ReadInt32() * 10;
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, int value, IFormatterResolver formatterResolver)
|
||||
|
|
|
@ -58,9 +58,27 @@
|
|||
<Compile Include="..\..\src\MessagePack\MessagePackBinary.cs">
|
||||
<Link>Code\MessagePackBinary.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\ExtensionHeader.cs">
|
||||
<Link>Code\ExtensionHeader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\ExtensionResult.cs">
|
||||
<Link>Code\ExtensionResult.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\MessagePackReader.cs">
|
||||
<Link>Code\MessagePackReader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\SequenceReader.cs">
|
||||
<Link>Code\SequenceReader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\SequenceReaderExtensions.cs">
|
||||
<Link>Code\SequenceReaderExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\MessagePackCode.cs">
|
||||
<Link>Code\MessagePackCode.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\Internal\DateTimeConstants.cs">
|
||||
<Link>Code\Internal\DateTimeConstants.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\MessagePack\Nil.cs">
|
||||
<Link>Code\Nil.cs</Link>
|
||||
</Compile>
|
||||
|
@ -81,6 +99,8 @@
|
|||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Memory" Version="4.5.2" />
|
||||
<PackageReference Include="Nerdbank.Streams" Version="2.0.214" />
|
||||
<PackageReference Include="System.Memory" Version="4.5.1" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
@ -4,6 +4,7 @@ using MessagePack.Internal;
|
|||
using MessagePack.Resolvers;
|
||||
using SharedData;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
@ -125,9 +126,9 @@ namespace DynamicCodeDumper
|
|||
}
|
||||
public class Int_x10Formatter : IMessagePackFormatter<int>
|
||||
{
|
||||
public int Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public int Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize) * 10;
|
||||
return reader.ReadInt32() * 10;
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, int value, IFormatterResolver formatterResolver)
|
||||
|
@ -138,9 +139,9 @@ namespace DynamicCodeDumper
|
|||
|
||||
public class String_x2Formatter : IMessagePackFormatter<string>
|
||||
{
|
||||
public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public string Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var s = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
var s = reader.ReadString();
|
||||
return s + s;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
extern alias newmsgpack;
|
||||
extern alias oldmsgpack;
|
||||
|
||||
using System;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using Nerdbank.Streams;
|
||||
|
||||
namespace PerfBenchmarkDotNet
|
||||
{
|
||||
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
|
||||
[CategoriesColumn]
|
||||
public class MessagePackReaderBenchmark
|
||||
{
|
||||
private const int BufferLength = 10000;
|
||||
private byte[] buffer = new byte[BufferLength];
|
||||
|
||||
[GlobalSetup]
|
||||
public void GlobalSetup()
|
||||
{
|
||||
for (int i = 0; i < this.buffer.Length; i++)
|
||||
{
|
||||
this.buffer[i] = newmsgpack::MessagePack.MessagePackCode.MaxFixInt;
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = BufferLength)]
|
||||
[BenchmarkCategory("2.0")]
|
||||
public void ReadByte20()
|
||||
{
|
||||
var reader = new newmsgpack::MessagePack.MessagePackReader(this.buffer);
|
||||
for (int i = 0; i < this.buffer.Length; i++)
|
||||
{
|
||||
reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = BufferLength)]
|
||||
[BenchmarkCategory("1.0")]
|
||||
public void ReadByte10()
|
||||
{
|
||||
int offset = 0;
|
||||
for (int i = 0; i < this.buffer.Length; i++)
|
||||
{
|
||||
oldmsgpack::MessagePack.MessagePackBinary.ReadInt32(this.buffer, offset, out int readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>PerfBenchmarkDotNet</RootNamespace>
|
||||
<AssemblyName>PerfBenchmarkDotNet</AssemblyName>
|
||||
<TargetFramework>net461</TargetFramework>
|
||||
<TargetFrameworks>net47;netcoreapp2.0</TargetFrameworks>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
|
@ -14,26 +12,17 @@
|
|||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" version="0.10.8" />
|
||||
<PackageReference Include="BenchmarkDotNet.Toolchains.Roslyn" version="0.10.8" />
|
||||
<PackageReference Include="BenchmarkDotNet" version="0.11.4" />
|
||||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.11.4" />
|
||||
<PackageReference Include="Hyperion" version="0.9.6" />
|
||||
<PackageReference Include="Jil" version="2.15.4" />
|
||||
<PackageReference Include="MessagePack" version="1.4.3" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.Win32.Registry" version="4.3.0" />
|
||||
<PackageReference Include="MsgPack.Cli" version="0.9.0-rc1" />
|
||||
<PackageReference Include="Newtonsoft.Json" version="10.0.3" />
|
||||
<PackageReference Include="protobuf-net" version="2.3.2" />
|
||||
<PackageReference Include="Sigil" version="4.7.0" />
|
||||
<PackageReference Include="ZeroFormatter" version="1.6.4" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="MessagePack_1_4_4.dll" />
|
||||
<Content Include="MessagePack_1_6_0_3.dll" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\MessagePack.Annotations\MessagePack.Annotations.csproj">
|
||||
<Aliases>newmsgpack</Aliases>
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace PerfBenchmarkDotNet
|
|||
Add(MarkdownExporter.GitHub);
|
||||
Add(MemoryDiagnoser.Default);
|
||||
|
||||
Add(Job.ShortRun.With(BenchmarkDotNet.Environments.Platform.X64).WithWarmupCount(1).WithTargetCount(1));
|
||||
Add(Job.ShortRun.With(BenchmarkDotNet.Environments.Platform.X64).WithWarmupCount(1).WithIterationCount(1));
|
||||
|
||||
//Add(Job.ShortRun.With(BenchmarkDotNet.Environments.Platform.X64).WithWarmupCount(1).WithTargetCount(1),
|
||||
//Job.ShortRun.With(BenchmarkDotNet.Environments.Platform.X86).WithWarmupCount(1).WithTargetCount(1));
|
||||
|
@ -50,7 +50,8 @@ namespace PerfBenchmarkDotNet
|
|||
typeof(StringKeyDeserializeCompare),
|
||||
typeof(NewVsOld),
|
||||
typeof(GuidImprov),
|
||||
typeof(ImproveStringKeySerializeBenchmark)
|
||||
typeof(ImproveStringKeySerializeBenchmark),
|
||||
typeof(MessagePackReaderBenchmark),
|
||||
});
|
||||
|
||||
// args = new[] { "0" };
|
||||
|
@ -736,6 +737,7 @@ namespace GeneratedFormatter
|
|||
using newmsgpack::MessagePack.Formatters;
|
||||
using PerfBenchmarkDotNet;
|
||||
using newmsgpack::MessagePack;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -829,16 +831,14 @@ namespace GeneratedFormatter
|
|||
return num - num2;
|
||||
}
|
||||
|
||||
public StringKeySerializerTarget Deserialize(byte[] bytes, int num, IFormatterResolver formatterResolver, out int ptr)
|
||||
public StringKeySerializerTarget Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, num))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
ptr = 1;
|
||||
return null;
|
||||
}
|
||||
int num2 = num;
|
||||
int num3 = MessagePackBinary.ReadMapHeader(bytes, num, out ptr);
|
||||
num += ptr;
|
||||
|
||||
int num3 = reader.ReadMapHeader();
|
||||
int myProperty = 0;
|
||||
int myProperty2 = 0;
|
||||
int myProperty3 = 0;
|
||||
|
@ -851,51 +851,49 @@ namespace GeneratedFormatter
|
|||
for (int i = 0; i < num3; i++)
|
||||
{
|
||||
int num4;
|
||||
bool arg_47_0 = this.keyMapping.TryGetValue(MessagePackBinary.ReadStringSegment(bytes, num, out ptr), out num4);
|
||||
num += ptr;
|
||||
bool arg_47_0 = this.keyMapping.TryGetValue(reader.ReadStringSegment(), out num4);
|
||||
if (!arg_47_0)
|
||||
{
|
||||
ptr = MessagePackBinary.ReadNextBlock(bytes, num);
|
||||
reader.Skip();
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (num4)
|
||||
{
|
||||
case 0:
|
||||
myProperty = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty = reader.ReadInt32();
|
||||
break;
|
||||
case 1:
|
||||
myProperty2 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty2 = reader.ReadInt32();
|
||||
break;
|
||||
case 2:
|
||||
myProperty3 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty3 = reader.ReadInt32();
|
||||
break;
|
||||
case 3:
|
||||
myProperty4 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty4 = reader.ReadInt32();
|
||||
break;
|
||||
case 4:
|
||||
myProperty5 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty5 = reader.ReadInt32();
|
||||
break;
|
||||
case 5:
|
||||
myProperty6 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty6 = reader.ReadInt32();
|
||||
break;
|
||||
case 6:
|
||||
myProperty7 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty7 = reader.ReadInt32();
|
||||
break;
|
||||
case 7:
|
||||
myProperty8 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty8 = reader.ReadInt32();
|
||||
break;
|
||||
case 8:
|
||||
myProperty9 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty9 = reader.ReadInt32();
|
||||
break;
|
||||
default:
|
||||
ptr = MessagePackBinary.ReadNextBlock(bytes, num);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
num += ptr;
|
||||
}
|
||||
ptr = num - num2;
|
||||
|
||||
return new StringKeySerializerTarget
|
||||
{
|
||||
MyProperty1 = myProperty,
|
||||
|
@ -1002,16 +1000,13 @@ namespace GeneratedFormatter
|
|||
return num - num2;
|
||||
}
|
||||
|
||||
public StringKeySerializerTarget Deserialize(byte[] bytes, int num, IFormatterResolver formatterResolver, out int ptr)
|
||||
public StringKeySerializerTarget Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, num))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
ptr = 1;
|
||||
return null;
|
||||
}
|
||||
int num2 = num;
|
||||
int num3 = MessagePackBinary.ReadMapHeader(bytes, num, out ptr);
|
||||
num += ptr;
|
||||
int num3 = reader.ReadMapHeader();
|
||||
int myProperty = 0;
|
||||
int myProperty2 = 0;
|
||||
int myProperty3 = 0;
|
||||
|
@ -1024,52 +1019,50 @@ namespace GeneratedFormatter
|
|||
for (int i = 0; i < num3; i++)
|
||||
{
|
||||
int num4;
|
||||
var segment = MessagePackBinary.ReadStringSegment(bytes, num, out ptr);
|
||||
var segment = reader.ReadStringSegment();
|
||||
bool arg_47_0 = this.keyMapping.TryGetValue(segment, out num4);
|
||||
num += ptr;
|
||||
if (!arg_47_0)
|
||||
{
|
||||
ptr = MessagePackBinary.ReadNextBlock(bytes, num);
|
||||
reader.Skip();
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (num4)
|
||||
{
|
||||
case 0:
|
||||
myProperty = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty = reader.ReadInt32();
|
||||
break;
|
||||
case 1:
|
||||
myProperty2 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty2 = reader.ReadInt32();
|
||||
break;
|
||||
case 2:
|
||||
myProperty3 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty3 = reader.ReadInt32();
|
||||
break;
|
||||
case 3:
|
||||
myProperty4 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty4 = reader.ReadInt32();
|
||||
break;
|
||||
case 4:
|
||||
myProperty5 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty5 = reader.ReadInt32();
|
||||
break;
|
||||
case 5:
|
||||
myProperty6 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty6 = reader.ReadInt32();
|
||||
break;
|
||||
case 6:
|
||||
myProperty7 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty7 = reader.ReadInt32();
|
||||
break;
|
||||
case 7:
|
||||
myProperty8 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty8 = reader.ReadInt32();
|
||||
break;
|
||||
case 8:
|
||||
myProperty9 = MessagePackBinary.ReadInt32(bytes, num, out ptr);
|
||||
myProperty9 = reader.ReadInt32();
|
||||
break;
|
||||
default:
|
||||
ptr = MessagePackBinary.ReadNextBlock(bytes, num);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
num += ptr;
|
||||
}
|
||||
ptr = num - num2;
|
||||
|
||||
return new StringKeySerializerTarget
|
||||
{
|
||||
MyProperty1 = myProperty,
|
||||
|
@ -1128,17 +1121,14 @@ namespace GeneratedFormatter
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public global::PerfBenchmarkDotNet.StringKeySerializerTarget Deserialize(byte[] bytes, int offset, newmsgpack::MessagePack.IFormatterResolver formatterResolver, out int readSize)
|
||||
public global::PerfBenchmarkDotNet.StringKeySerializerTarget Deserialize(ref MessagePackReader reader, newmsgpack::MessagePack.IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (newmsgpack::MessagePack.MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var startOffset = offset;
|
||||
var length = newmsgpack::MessagePack.MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var length = reader.ReadMapHeader();
|
||||
|
||||
var __MyProperty1__ = default(int);
|
||||
var __MyProperty2__ = default(int);
|
||||
|
@ -1152,55 +1142,49 @@ namespace GeneratedFormatter
|
|||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var stringKey = newmsgpack::MessagePack.MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var stringKey = reader.ReadStringSegment();
|
||||
int key;
|
||||
if (!____keyMapping.TryGetValue(stringKey, out key))
|
||||
{
|
||||
readSize = newmsgpack::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
goto NEXT_LOOP;
|
||||
reader.Skip();
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case 0:
|
||||
__MyProperty1__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty1__ = reader.ReadInt32();
|
||||
break;
|
||||
case 1:
|
||||
__MyProperty2__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty2__ = reader.ReadInt32();
|
||||
break;
|
||||
case 2:
|
||||
__MyProperty3__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty3__ = reader.ReadInt32();
|
||||
break;
|
||||
case 3:
|
||||
__MyProperty4__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty4__ = reader.ReadInt32();
|
||||
break;
|
||||
case 4:
|
||||
__MyProperty5__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty5__ = reader.ReadInt32();
|
||||
break;
|
||||
case 5:
|
||||
__MyProperty6__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty6__ = reader.ReadInt32();
|
||||
break;
|
||||
case 6:
|
||||
__MyProperty7__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty7__ = reader.ReadInt32();
|
||||
break;
|
||||
case 7:
|
||||
__MyProperty8__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty8__ = reader.ReadInt32();
|
||||
break;
|
||||
case 8:
|
||||
__MyProperty9__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty9__ = reader.ReadInt32();
|
||||
break;
|
||||
default:
|
||||
readSize = newmsgpack::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
|
||||
NEXT_LOOP:
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
var ____result = new global::PerfBenchmarkDotNet.StringKeySerializerTarget();
|
||||
____result.MyProperty1 = __MyProperty1__;
|
||||
____result.MyProperty2 = __MyProperty2__;
|
||||
|
@ -1257,17 +1241,14 @@ namespace GeneratedFormatter
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public global::PerfBenchmarkDotNet.StringKeySerializerTarget Deserialize(byte[] bytes, int offset, newmsgpack::MessagePack.IFormatterResolver formatterResolver, out int readSize)
|
||||
public global::PerfBenchmarkDotNet.StringKeySerializerTarget Deserialize(ref MessagePackReader reader, newmsgpack::MessagePack.IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (newmsgpack::MessagePack.MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var startOffset = offset;
|
||||
var length = newmsgpack::MessagePack.MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var length = reader.ReadMapHeader();
|
||||
|
||||
var __MyProperty1__ = default(int);
|
||||
var __MyProperty2__ = default(int);
|
||||
|
@ -1281,55 +1262,49 @@ namespace GeneratedFormatter
|
|||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var stringKey = newmsgpack::MessagePack.MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var stringKey = reader.ReadStringSegment();
|
||||
int key;
|
||||
if (!____keyMapping.TryGetValue(stringKey, out key))
|
||||
{
|
||||
readSize = newmsgpack::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
goto NEXT_LOOP;
|
||||
reader.Skip();
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case 0:
|
||||
__MyProperty1__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty1__ = reader.ReadInt32();
|
||||
break;
|
||||
case 1:
|
||||
__MyProperty2__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty2__ = reader.ReadInt32();
|
||||
break;
|
||||
case 2:
|
||||
__MyProperty3__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty3__ = reader.ReadInt32();
|
||||
break;
|
||||
case 3:
|
||||
__MyProperty4__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty4__ = reader.ReadInt32();
|
||||
break;
|
||||
case 4:
|
||||
__MyProperty5__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty5__ = reader.ReadInt32();
|
||||
break;
|
||||
case 5:
|
||||
__MyProperty6__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty6__ = reader.ReadInt32();
|
||||
break;
|
||||
case 6:
|
||||
__MyProperty7__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty7__ = reader.ReadInt32();
|
||||
break;
|
||||
case 7:
|
||||
__MyProperty8__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty8__ = reader.ReadInt32();
|
||||
break;
|
||||
case 8:
|
||||
__MyProperty9__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
__MyProperty9__ = reader.ReadInt32();
|
||||
break;
|
||||
default:
|
||||
readSize = newmsgpack::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
|
||||
NEXT_LOOP:
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
var ____result = new global::PerfBenchmarkDotNet.StringKeySerializerTarget();
|
||||
____result.MyProperty1 = __MyProperty1__;
|
||||
____result.MyProperty2 = __MyProperty2__;
|
||||
|
@ -1344,5 +1319,4 @@ namespace GeneratedFormatter
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -17,6 +17,7 @@ using System.Text;
|
|||
using System.IO.Compression;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Buffers;
|
||||
|
||||
namespace Sandbox
|
||||
{
|
||||
|
@ -248,9 +249,9 @@ namespace Sandbox
|
|||
return formatterResolver.GetFormatterWithVerify<string>().Serialize(ref bytes, offset, value.internalId, formatterResolver);
|
||||
}
|
||||
|
||||
public CustomObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public CustomObject Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var id = formatterResolver.GetFormatterWithVerify<string>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var id = formatterResolver.GetFormatterWithVerify<string>().Deserialize(ref reader, formatterResolver);
|
||||
return new CustomObject { internalId = id };
|
||||
}
|
||||
}
|
||||
|
@ -794,26 +795,22 @@ namespace Sandbox
|
|||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public IHogeMoge Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public IHogeMoge Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
// TODO:array header...
|
||||
|
||||
int keySize;
|
||||
int valueSize;
|
||||
var key = MessagePackBinary.ReadInt32(bytes, offset, out keySize);
|
||||
var key = reader.ReadInt32();
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
var result = formatterResolver.GetFormatterWithVerify<HogeMoge1>().Deserialize(bytes, offset + keySize, formatterResolver, out valueSize);
|
||||
readSize = keySize + valueSize;
|
||||
var result = formatterResolver.GetFormatterWithVerify<HogeMoge1>().Deserialize(ref reader, formatterResolver);
|
||||
return (IHogeMoge)result;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
var result = formatterResolver.GetFormatterWithVerify<HogeMoge2>().Deserialize(bytes, offset + keySize, formatterResolver, out valueSize);
|
||||
readSize = keySize + valueSize;
|
||||
var result = formatterResolver.GetFormatterWithVerify<HogeMoge2>().Deserialize(ref reader, formatterResolver);
|
||||
return (IHogeMoge)result;
|
||||
}
|
||||
default:
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharedData
|
||||
|
@ -51,7 +52,7 @@ namespace SharedData
|
|||
|
||||
public class OreOreFormatter : IMessagePackFormatter<int>
|
||||
{
|
||||
public int Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public int Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -69,7 +70,7 @@ namespace SharedData
|
|||
|
||||
}
|
||||
|
||||
public int Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public int Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -689,12 +690,9 @@ namespace SharedData
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public DynamicArgumentTuple<T1, T2, T3, T4, T5, T6, T7, T8, T9> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DynamicArgumentTuple<T1, T2, T3, T4, T5, T6, T7, T8, T9> Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var length = reader.ReadArrayHeader();
|
||||
|
||||
var item1 = default1;
|
||||
var item2 = default2;
|
||||
|
@ -711,41 +709,38 @@ namespace SharedData
|
|||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 1:
|
||||
item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 2:
|
||||
item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 3:
|
||||
item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 4:
|
||||
item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 5:
|
||||
item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 6:
|
||||
item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 7:
|
||||
item8 = formatterResolver.GetFormatterWithVerify<T8>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item8 = formatterResolver.GetFormatterWithVerify<T8>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
case 8:
|
||||
item9 = formatterResolver.GetFormatterWithVerify<T9>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
item9 = formatterResolver.GetFormatterWithVerify<T9>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
default:
|
||||
readSize = MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new DynamicArgumentTuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>(item1, item2, item3, item4, item5, item6, item7, item8, item9);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Immutable;
|
||||
using System;
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace MessagePack.ImmutableCollection
|
||||
{
|
||||
|
@ -29,28 +30,23 @@ namespace MessagePack.ImmutableCollection
|
|||
}
|
||||
}
|
||||
|
||||
public ImmutableArray<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ImmutableArray<T> Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return ImmutableArray<T>.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
|
||||
var builder = ImmutableArray.CreateBuilder<T>(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
builder.Add(formatter.Deserialize(bytes, offset, formatterResolver, out readSize));
|
||||
offset += readSize;
|
||||
builder.Add(formatter.Deserialize(ref reader, formatterResolver));
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return builder.ToImmutable();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard1.6;netstandard2.0;net45;net47</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard1.6;netstandard2.0;net46;net47</TargetFrameworks>
|
||||
|
||||
<IsPackable>true</IsPackable>
|
||||
<Title>MessagePack for C# Extension Support for ImmutableCollection</Title>
|
||||
|
|
|
@ -123,33 +123,24 @@ namespace MessagePack.ReactivePropertyExtension
|
|||
}
|
||||
}
|
||||
|
||||
public ReactiveProperty<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ReactiveProperty<T> Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var length = reader.ReadArrayHeader();
|
||||
if (length != 3) throw new InvalidOperationException("Invalid ReactiveProperty data.");
|
||||
|
||||
var mode = (ReactivePropertyMode)MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var mode = (ReactivePropertyMode)reader.ReadInt32();
|
||||
|
||||
var schedulerId = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var schedulerId = reader.ReadInt32();
|
||||
|
||||
var scheduler = ReactivePropertySchedulerMapper.GetScheduler(schedulerId);
|
||||
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(ref reader, formatterResolver);
|
||||
|
||||
return new ReactiveProperty<T>(scheduler, v, mode);
|
||||
}
|
||||
|
@ -183,16 +174,16 @@ namespace MessagePack.ReactivePropertyExtension
|
|||
}
|
||||
}
|
||||
|
||||
public IReactiveProperty<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public IReactiveProperty<T> Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var length = reader.ReadArrayHeader();
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case 2:
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactivePropertySlim<T>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactivePropertySlim<T>>().Deserialize(ref reader, formatterResolver);
|
||||
case 3:
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactiveProperty<T>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactiveProperty<T>>().Deserialize(ref reader, formatterResolver);
|
||||
default:
|
||||
throw new InvalidOperationException("Invalid ReactiveProperty or ReactivePropertySlim data.");
|
||||
}
|
||||
|
@ -225,16 +216,16 @@ namespace MessagePack.ReactivePropertyExtension
|
|||
}
|
||||
}
|
||||
|
||||
public IReadOnlyReactiveProperty<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public IReadOnlyReactiveProperty<T> Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var length = reader.ReadArrayHeader();
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case 2:
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactivePropertySlim<T>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactivePropertySlim<T>>().Deserialize(ref reader, formatterResolver);
|
||||
case 3:
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactiveProperty<T>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return ReactivePropertyResolver.Instance.GetFormatterWithVerify<ReactiveProperty<T>>().Deserialize(ref reader, formatterResolver);
|
||||
default:
|
||||
throw new InvalidOperationException("Invalid ReactiveProperty or ReactivePropertySlim data.");
|
||||
}
|
||||
|
@ -268,16 +259,15 @@ namespace MessagePack.ReactivePropertyExtension
|
|||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public Unit Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Unit Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return Unit.Default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Data type. Code:" + MessagePackCode.ToFormatName(bytes[offset]));
|
||||
throw new InvalidOperationException("Invalid Data type. Code: " + MessagePackCode.ToFormatName(reader.NextCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -296,16 +286,15 @@ namespace MessagePack.ReactivePropertyExtension
|
|||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public Unit? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Unit? Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return Unit.Default;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Data type. Code:" + MessagePackCode.ToFormatName(bytes[offset]));
|
||||
throw new InvalidOperationException("Invalid Data type. Code: " + MessagePackCode.ToFormatName(reader.NextCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -332,28 +321,20 @@ namespace MessagePack.ReactivePropertyExtension
|
|||
}
|
||||
}
|
||||
|
||||
public ReactivePropertySlim<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ReactivePropertySlim<T> Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var length = reader.ReadArrayHeader();
|
||||
if (length != 2) throw new InvalidOperationException("Invalid ReactivePropertySlim data.");
|
||||
|
||||
var mode = (ReactivePropertyMode)MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var mode = (ReactivePropertyMode)reader.ReadInt32();
|
||||
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(ref reader, formatterResolver);
|
||||
|
||||
return new ReactivePropertySlim<T>(v, mode);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
|
@ -38,8 +40,8 @@ namespace MessagePack.Unity.Extension
|
|||
public const sbyte Double = 39;
|
||||
}
|
||||
|
||||
// use ext instead of ArrayFormatter for extremely boostup performance.
|
||||
// Layout: [extHeader, byteSize(integer), isLittlEendian(bool), bytes()]
|
||||
// use ext instead of ArrayFormatter to extremely boost up performance.
|
||||
// Layout: [extHeader, byteSize(integer), isLittleEndian(bool), bytes()]
|
||||
|
||||
// Used Ext:30~36
|
||||
|
||||
|
@ -49,7 +51,7 @@ namespace MessagePack.Unity.Extension
|
|||
protected abstract sbyte TypeCode { get; }
|
||||
protected abstract int StructLength { get; }
|
||||
protected abstract void CopySerializeUnsafe(ref T[] src, ref byte[] dest, int destOffset, int byteLength);
|
||||
protected abstract void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref T[] dest, int byteLength);
|
||||
protected void CopyDeserializeUnsafe(ReadOnlySpan<byte> src, Span<T> dest) => src.CopyTo(MemoryMarshal.Cast<T, byte>(dest));
|
||||
|
||||
public unsafe int Serialize(ref byte[] bytes, int offset, T[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
|
@ -70,36 +72,35 @@ namespace MessagePack.Unity.Extension
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public unsafe T[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T[] Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var startOffset = offset;
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var header = reader.ReadExtensionFormatHeader();
|
||||
if (header.TypeCode != TypeCode) throw new InvalidOperationException("Invalid typeCode.");
|
||||
|
||||
var byteLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var byteLength = reader.ReadInt32();
|
||||
var isLittleEndian = reader.ReadBoolean();
|
||||
|
||||
var isLittleEndian = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
// 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>()];
|
||||
var resultAsBytes = MemoryMarshal.Cast<T, byte>(result);
|
||||
reader.ReadRaw(byteLength).CopyTo(resultAsBytes);
|
||||
|
||||
// Reverse the byte order if necessary.
|
||||
if (isLittleEndian != BitConverter.IsLittleEndian)
|
||||
{
|
||||
Array.Reverse(bytes, offset, byteLength);
|
||||
for (int i = 0, j = resultAsBytes.Length - 1; i < j; i++, j--)
|
||||
{
|
||||
byte tmp = resultAsBytes[i];
|
||||
resultAsBytes[i] = resultAsBytes[j];
|
||||
resultAsBytes[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
var result = new T[byteLength / StructLength];
|
||||
CopyDeserializeUnsafe(ref bytes, offset, ref result, byteLength);
|
||||
offset += byteLength;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -130,15 +131,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Vector2[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector3ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector3>
|
||||
|
@ -167,15 +159,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Vector3[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector4ArrayBlitFormatter : UnsafeBlitFormatterBase<Vector4>
|
||||
|
@ -204,15 +187,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Vector4[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class QuaternionArrayBlitFormatter : UnsafeBlitFormatterBase<Quaternion>
|
||||
|
@ -241,15 +215,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Quaternion[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ColorArrayBlitFormatter : UnsafeBlitFormatterBase<Color>
|
||||
|
@ -278,15 +243,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Color[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BoundsArrayBlitFormatter : UnsafeBlitFormatterBase<Bounds>
|
||||
|
@ -315,15 +271,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Bounds[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class RectArrayBlitFormatter : UnsafeBlitFormatterBase<Rect>
|
||||
|
@ -352,15 +299,6 @@ namespace MessagePack.Unity.Extension
|
|||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
protected override unsafe void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref Rect[] dest, int byteLength)
|
||||
{
|
||||
fixed (void* pSrc = &src[srcOffset])
|
||||
fixed (void* pDest = dest)
|
||||
{
|
||||
MemoryUtil.SimpleMemoryCopy(pDest, pSrc, byteLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class IntArrayBlitFormatter : UnsafeBlitFormatterBase<int>
|
||||
|
@ -369,11 +307,6 @@ namespace MessagePack.Unity.Extension
|
|||
|
||||
protected override int StructLength { get { return 4; } }
|
||||
|
||||
protected override void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref int[] dest, int byteLength)
|
||||
{
|
||||
Buffer.BlockCopy(src, srcOffset, dest, 0, byteLength);
|
||||
}
|
||||
|
||||
protected override void CopySerializeUnsafe(ref int[] src, ref byte[] dest, int destOffset, int byteLength)
|
||||
{
|
||||
Buffer.BlockCopy(src, 0, dest, destOffset, byteLength);
|
||||
|
@ -386,11 +319,6 @@ namespace MessagePack.Unity.Extension
|
|||
|
||||
protected override int StructLength { get { return 4; } }
|
||||
|
||||
protected override void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref float[] dest, int byteLength)
|
||||
{
|
||||
Buffer.BlockCopy(src, srcOffset, dest, 0, byteLength);
|
||||
}
|
||||
|
||||
protected override void CopySerializeUnsafe(ref float[] src, ref byte[] dest, int destOffset, int byteLength)
|
||||
{
|
||||
Buffer.BlockCopy(src, 0, dest, destOffset, byteLength);
|
||||
|
@ -403,11 +331,6 @@ namespace MessagePack.Unity.Extension
|
|||
|
||||
protected override int StructLength { get { return 8; } }
|
||||
|
||||
protected override void CopyDeserializeUnsafe(ref byte[] src, int srcOffset, ref double[] dest, int byteLength)
|
||||
{
|
||||
Buffer.BlockCopy(src, srcOffset, dest, 0, byteLength);
|
||||
}
|
||||
|
||||
protected override void CopySerializeUnsafe(ref double[] src, ref byte[] dest, int destOffset, int byteLength)
|
||||
{
|
||||
Buffer.BlockCopy(src, 0, dest, destOffset, byteLength);
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -1,7 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard1.6;netstandard2.0;net45;net47</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard1.6;netstandard2.0;net46;net47</TargetFrameworks>
|
||||
<RootNamespace>MessagePack.Unity</RootNamespace>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<DefineConstants>$(DefineConstants);ENABLE_UNSAFE_MSGPACK</DefineConstants>
|
||||
|
|
|
@ -110,11 +110,11 @@ namespace MessagePack.CodeGenerator
|
|||
{
|
||||
if (primitiveTypes.Contains(Type))
|
||||
{
|
||||
return $"MessagePackBinary.Read{ShortTypeName.Replace("[]", "s")}(bytes, offset, out readSize)";
|
||||
return $"reader.Read{ShortTypeName.Replace("[]", "s")}()";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"formatterResolver.GetFormatterWithVerify<{Type}>().Deserialize(bytes, offset, formatterResolver, out readSize)";
|
||||
return $"formatterResolver.GetFormatterWithVerify<{Type}>().Deserialize(ref reader, formatterResolver)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,30 +36,30 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n{\r\n using System;\r\n using MessagePack;\r\n\r\n");
|
||||
this.Write("\r\n{\r\n using System;\r\n using System.Buffers;\r\n using MessagePack;\r\n\r\n");
|
||||
|
||||
#line 16 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 17 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
foreach(var info in enumSerializationInfos) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" public sealed class ");
|
||||
|
||||
#line 17 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("Formatter : global::MessagePack.Formatters.IMessagePackFormatter<");
|
||||
|
||||
#line 17 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(">\r\n {\r\n public int Serialize(ref byte[] bytes, int offset, ");
|
||||
|
||||
#line 19 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 20 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
|
@ -67,43 +67,43 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write(" value, global::MessagePack.IFormatterResolver formatterResolver)\r\n {\r\n " +
|
||||
" return MessagePackBinary.Write");
|
||||
|
||||
#line 21 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 22 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.UnderlyingType));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("(ref bytes, offset, (");
|
||||
|
||||
#line 21 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 22 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.UnderlyingType));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(")value);\r\n }\r\n \r\n public ");
|
||||
|
||||
#line 24 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 25 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver for" +
|
||||
"matterResolver, out int readSize)\r\n {\r\n return (");
|
||||
this.Write(" Deserialize(ref MessagePackReader reader, global::MessagePack.IFormatterResolver" +
|
||||
" formatterResolver)\r\n {\r\n return (");
|
||||
|
||||
#line 26 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 27 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(")MessagePackBinary.Read");
|
||||
this.Write(")reader.Read");
|
||||
|
||||
#line 26 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 27 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.UnderlyingType));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("(bytes, offset, out readSize);\r\n }\r\n }\r\n\r\n");
|
||||
this.Write("();\r\n }\r\n }\r\n\r\n");
|
||||
|
||||
#line 30 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
#line 31 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\EnumTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
namespace <#= Namespace #>
|
||||
{
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using MessagePack;
|
||||
|
||||
<# foreach(var info in enumSerializationInfos) { #>
|
||||
|
@ -21,9 +22,9 @@ namespace <#= Namespace #>
|
|||
return MessagePackBinary.Write<#= info.UnderlyingType #>(ref bytes, offset, (<#= info.UnderlyingType #>)value);
|
||||
}
|
||||
|
||||
public <#= info.FullName #> Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= info.FullName #> Deserialize(ref MessagePackReader reader, global::MessagePack.IFormatterResolver formatterResolver)
|
||||
{
|
||||
return (<#= info.FullName #>)MessagePackBinary.Read<#= info.UnderlyingType #>(bytes, offset, out readSize);
|
||||
return (<#= info.FullName #>)reader.Read<#= info.UnderlyingType #>();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,30 +36,30 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n{\r\n using System;\r\n using MessagePack;\r\n\r\n");
|
||||
this.Write("\r\n{\r\n using System;\r\n using System.Buffers;\r\n using MessagePack;\r\n\r\n");
|
||||
|
||||
#line 16 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 17 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
foreach(var objInfo in objectSerializationInfos) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n public sealed class ");
|
||||
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 19 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("Formatter : global::MessagePack.Formatters.IMessagePackFormatter<");
|
||||
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 19 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(">\r\n {\r\n");
|
||||
|
||||
#line 20 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 21 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if( objInfo.IsStringKey) {
|
||||
|
||||
#line default
|
||||
|
@ -67,7 +67,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write("\r\n readonly global::MessagePack.Internal.AutomataDictionary ____keyMapping" +
|
||||
";\r\n readonly byte[][] ____stringByteKeys;\r\n\r\n public ");
|
||||
|
||||
#line 25 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 26 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.Name));
|
||||
|
||||
#line default
|
||||
|
@ -75,28 +75,28 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write("Formatter()\r\n {\r\n this.____keyMapping = new global::MessagePack" +
|
||||
".Internal.AutomataDictionary()\r\n {\r\n");
|
||||
|
||||
#line 29 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 30 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
foreach(var x in objInfo.Members) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" { \"");
|
||||
|
||||
#line 30 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 31 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.StringKey));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\", ");
|
||||
|
||||
#line 30 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 31 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.IntKey));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("},\r\n");
|
||||
|
||||
#line 31 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 32 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
|
@ -104,42 +104,42 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write(" };\r\n\r\n this.____stringByteKeys = new byte[][]\r\n " +
|
||||
" {\r\n");
|
||||
|
||||
#line 36 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 37 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
foreach(var x in objInfo.Members.Where(x => x.IsReadable)) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" global::MessagePack.MessagePackBinary.GetEncodedStringBytes(\"");
|
||||
|
||||
#line 37 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 38 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.StringKey));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\"),\r\n");
|
||||
|
||||
#line 38 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 39 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" \r\n };\r\n }\r\n\r\n");
|
||||
|
||||
#line 42 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 43 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n public int Serialize(ref byte[] bytes, int offset, ");
|
||||
|
||||
#line 44 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 45 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" value, global::MessagePack.IFormatterResolver formatterResolver)\r\n {\r\n");
|
||||
|
||||
#line 46 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 47 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if( objInfo.IsClass) {
|
||||
|
||||
#line default
|
||||
|
@ -147,14 +147,13 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write(" if (value == null)\r\n {\r\n return global::Mes" +
|
||||
"sagePack.MessagePackBinary.WriteNil(ref bytes, offset);\r\n }\r\n");
|
||||
|
||||
#line 51 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 52 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" \r\n");
|
||||
|
||||
#line 52 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 53 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBefore) {
|
||||
|
||||
#line default
|
||||
|
@ -162,21 +161,21 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" ((IMessagePackSerializationCallbackReceiver)value).OnBeforeSerialize(" +
|
||||
");\r\n");
|
||||
|
||||
#line 54 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 55 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else if(objInfo.HasIMessagePackSerializationCallbackReceiver) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" value.OnBeforeSerialize();\r\n");
|
||||
|
||||
#line 56 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 57 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" var startOffset = offset;\r\n");
|
||||
|
||||
#line 58 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 59 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if( objInfo.IsIntKey) { if( (objInfo.MaxKey + 1) <= 15) {
|
||||
|
||||
#line default
|
||||
|
@ -184,14 +183,14 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" offset += global::MessagePack.MessagePackBinary.WriteFixedArrayHeader" +
|
||||
"Unsafe(ref bytes, offset, ");
|
||||
|
||||
#line 59 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 60 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.MaxKey + 1));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(");\r\n");
|
||||
|
||||
#line 60 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 61 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else {
|
||||
|
||||
#line default
|
||||
|
@ -199,14 +198,14 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" offset += global::MessagePack.MessagePackBinary.WriteArrayHeader(ref " +
|
||||
"bytes, offset, ");
|
||||
|
||||
#line 61 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 62 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.MaxKey + 1));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(");\r\n");
|
||||
|
||||
#line 62 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 63 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} } else if( objInfo.WriteCount <= 15) {
|
||||
|
||||
#line default
|
||||
|
@ -214,14 +213,14 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" offset += global::MessagePack.MessagePackBinary.WriteFixedMapHeaderUn" +
|
||||
"safe(ref bytes, offset, ");
|
||||
|
||||
#line 63 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 64 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.WriteCount));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(");\r\n");
|
||||
|
||||
#line 64 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 65 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else {
|
||||
|
||||
#line default
|
||||
|
@ -229,32 +228,32 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" offset += global::MessagePack.MessagePackBinary.WriteMapHeader(ref by" +
|
||||
"tes, offset, ");
|
||||
|
||||
#line 65 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 66 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.WriteCount));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(");\r\n");
|
||||
|
||||
#line 66 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 67 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
|
||||
#line 67 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 68 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if(objInfo.IsIntKey) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
|
||||
#line 68 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 69 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
for(var i =0; i<= objInfo.MaxKey; i++) { var member = objInfo.GetMember(i);
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
|
||||
#line 69 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 70 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if( member == null) {
|
||||
|
||||
#line default
|
||||
|
@ -262,27 +261,27 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" offset += global::MessagePack.MessagePackBinary.WriteNil(ref bytes, o" +
|
||||
"ffset);\r\n");
|
||||
|
||||
#line 71 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 72 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" offset += ");
|
||||
|
||||
#line 72 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 73 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(member.GetSerializeMethodString()));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(";\r\n");
|
||||
|
||||
#line 73 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 74 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} } } else {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
|
||||
#line 74 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 75 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
var index = 0; foreach(var x in objInfo.Members) {
|
||||
|
||||
#line default
|
||||
|
@ -290,42 +289,42 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
this.Write(" offset += global::MessagePack.MessagePackBinary.WriteRaw(ref bytes, o" +
|
||||
"ffset, this.____stringByteKeys[");
|
||||
|
||||
#line 75 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 76 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(index++));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("]);\r\n offset += ");
|
||||
|
||||
#line 76 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 77 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.GetSerializeMethodString()));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(";\r\n");
|
||||
|
||||
#line 77 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 78 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} }
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" return offset - startOffset;\r\n }\r\n\r\n public ");
|
||||
|
||||
#line 81 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 82 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver for" +
|
||||
"matterResolver, out int readSize)\r\n {\r\n if (global::MessagePac" +
|
||||
"k.MessagePackBinary.IsNil(bytes, offset))\r\n {\r\n");
|
||||
this.Write(" Deserialize(ref MessagePackReader reader, global::MessagePack.IFormatterResolver" +
|
||||
" formatterResolver)\r\n {\r\n if (reader.TryReadNil())\r\n " +
|
||||
" {\r\n");
|
||||
|
||||
#line 85 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 86 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if( objInfo.IsClass) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" readSize = 1;\r\n return null;\r\n");
|
||||
this.Write(" return null;\r\n");
|
||||
|
||||
#line 88 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else {
|
||||
|
@ -340,175 +339,157 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBe
|
|||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" }\r\n\r\n var startOffset = offset;\r\n");
|
||||
this.Write(" }\r\n\r\n");
|
||||
|
||||
#line 94 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 93 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if(objInfo.IsStringKey) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" var length = global::MessagePack.MessagePackBinary.ReadMapHeader(byte" +
|
||||
"s, offset, out readSize);\r\n");
|
||||
this.Write(" var length = reader.ReadMapHeader();\r\n");
|
||||
|
||||
#line 96 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 95 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" var length = global::MessagePack.MessagePackBinary.ReadArrayHeader(by" +
|
||||
"tes, offset, out readSize);\r\n");
|
||||
this.Write(" var length = reader.ReadArrayHeader();\r\n");
|
||||
|
||||
#line 98 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 97 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" offset += readSize;\r\n\r\n");
|
||||
this.Write("\r\n");
|
||||
|
||||
#line 101 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 99 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
foreach(var x in objInfo.Members) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" var __");
|
||||
|
||||
#line 102 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 100 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("__ = default(");
|
||||
|
||||
#line 102 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 100 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.Type));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(");\r\n");
|
||||
|
||||
#line 103 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 101 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n for (int i = 0; i < length; i++)\r\n {\r\n");
|
||||
|
||||
#line 107 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 105 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if(objInfo.IsStringKey) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(@" var stringKey = global::MessagePack.MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
this.Write(@" var stringKey = reader.ReadStringSegment();
|
||||
int key;
|
||||
if (!____keyMapping.TryGetValue(stringKey, out key))
|
||||
{
|
||||
readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
goto NEXT_LOOP;
|
||||
reader.Skip();
|
||||
continue;
|
||||
}
|
||||
");
|
||||
|
||||
#line 116 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 113 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" var key = i;\r\n");
|
||||
|
||||
#line 118 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 115 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n switch (key)\r\n {\r\n");
|
||||
|
||||
#line 122 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 119 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
foreach(var x in objInfo.Members) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" case ");
|
||||
|
||||
#line 123 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 120 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.IntKey));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(":\r\n __");
|
||||
|
||||
#line 124 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 121 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("__ = ");
|
||||
|
||||
#line 124 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 121 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.GetDeserializeMethodString()));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(";\r\n break;\r\n");
|
||||
|
||||
#line 126 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 123 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" default:\r\n readSize = global::MessageP" +
|
||||
"ack.MessagePackBinary.ReadNextBlock(bytes, offset);\r\n bre" +
|
||||
"ak;\r\n }\r\n");
|
||||
this.Write(" default:\r\n reader.Skip();\r\n " +
|
||||
" break;\r\n }\r\n }\r\n\r\n var ____res" +
|
||||
"ult = new ");
|
||||
|
||||
#line 131 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if(objInfo.IsStringKey) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" \r\n NEXT_LOOP:\r\n");
|
||||
|
||||
#line 133 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" offset += readSize;\r\n }\r\n\r\n readSize = offs" +
|
||||
"et - startOffset;\r\n\r\n var ____result = new ");
|
||||
|
||||
#line 139 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 130 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(objInfo.GetConstructorString()));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(";\r\n");
|
||||
|
||||
#line 140 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 131 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
foreach(var x in objInfo.Members.Where(x => x.IsWritable)) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" ____result.");
|
||||
|
||||
#line 141 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 132 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" = __");
|
||||
|
||||
#line 141 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 132 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("__;\r\n");
|
||||
|
||||
#line 142 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 133 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
|
||||
#line 143 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 134 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnAfter) {
|
||||
|
||||
#line default
|
||||
|
@ -516,27 +497,27 @@ if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnAf
|
|||
this.Write(" ((IMessagePackSerializationCallbackReceiver)____result).OnAfterDeseri" +
|
||||
"alize();\r\n");
|
||||
|
||||
#line 145 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 136 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
} else if(objInfo.HasIMessagePackSerializationCallbackReceiver) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" ____result.OnAfterDeserialize();\r\n");
|
||||
|
||||
#line 147 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 138 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" return ____result;\r\n }\r\n }\r\n\r\n");
|
||||
|
||||
#line 152 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
#line 143 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\FormatterTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("}\r\n\r\n#pragma warning restore 168\r\n#pragma warning restore 414\r\n#pragma warning re" +
|
||||
"store 618\r\n#pragma warning restore 612");
|
||||
"store 618\r\n#pragma warning restore 612\r\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
namespace <#= Namespace #>
|
||||
{
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using MessagePack;
|
||||
|
||||
<# foreach(var objInfo in objectSerializationInfos) { #>
|
||||
|
@ -48,7 +49,7 @@ namespace <#= Namespace #>
|
|||
{
|
||||
return global::MessagePack.MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
<# } #>
|
||||
<# } #>
|
||||
<#if(objInfo.HasIMessagePackSerializationCallbackReceiver && objInfo.NeedsCastOnBefore) { #>
|
||||
((IMessagePackSerializationCallbackReceiver)value).OnBeforeSerialize();
|
||||
<# } else if(objInfo.HasIMessagePackSerializationCallbackReceiver) { #>
|
||||
|
@ -78,25 +79,22 @@ namespace <#= Namespace #>
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public <#= objInfo.FullName #> Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= objInfo.FullName #> Deserialize(ref MessagePackReader reader, global::MessagePack.IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (global::MessagePack.MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
<# if( objInfo.IsClass) { #>
|
||||
readSize = 1;
|
||||
return null;
|
||||
<# } else { #>
|
||||
throw new InvalidOperationException("typecode is null, struct not supported");
|
||||
<# } #>
|
||||
}
|
||||
|
||||
var startOffset = offset;
|
||||
<# if(objInfo.IsStringKey) { #>
|
||||
var length = global::MessagePack.MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
var length = reader.ReadMapHeader();
|
||||
<# } else { #>
|
||||
var length = global::MessagePack.MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var length = reader.ReadArrayHeader();
|
||||
<# } #>
|
||||
offset += readSize;
|
||||
|
||||
<# foreach(var x in objInfo.Members) { #>
|
||||
var __<#= x.Name #>__ = default(<#= x.Type #>);
|
||||
|
@ -105,13 +103,12 @@ namespace <#= Namespace #>
|
|||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
<# if(objInfo.IsStringKey) { #>
|
||||
var stringKey = global::MessagePack.MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var stringKey = reader.ReadStringSegment();
|
||||
int key;
|
||||
if (!____keyMapping.TryGetValue(stringKey, out key))
|
||||
{
|
||||
readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
goto NEXT_LOOP;
|
||||
reader.Skip();
|
||||
continue;
|
||||
}
|
||||
<# } else { #>
|
||||
var key = i;
|
||||
|
@ -125,17 +122,11 @@ namespace <#= Namespace #>
|
|||
break;
|
||||
<# } #>
|
||||
default:
|
||||
readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
<# if(objInfo.IsStringKey) { #>
|
||||
NEXT_LOOP:
|
||||
<# } #>
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
var ____result = new <#= objInfo.GetConstructorString() #>;
|
||||
<# foreach(var x in objInfo.Members.Where(x => x.IsWritable)) { #>
|
||||
____result.<#= x.Name #> = __<#= x.Name #>__;
|
||||
|
@ -155,4 +146,4 @@ namespace <#= Namespace #>
|
|||
#pragma warning restore 168
|
||||
#pragma warning restore 414
|
||||
#pragma warning restore 618
|
||||
#pragma warning restore 612
|
||||
#pragma warning restore 612
|
||||
|
|
|
@ -36,9 +36,10 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n{\r\n using System;\r\n using MessagePack;\r\n\r\n public class ");
|
||||
this.Write("\r\n{\r\n using System;\r\n using System.Buffers;\r\n using MessagePack;\r\n\r\n " +
|
||||
"public class ");
|
||||
|
||||
#line 16 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 17 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ResolverName));
|
||||
|
||||
#line default
|
||||
|
@ -46,14 +47,14 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write(" : global::MessagePack.IFormatterResolver\r\n {\r\n public static readonly " +
|
||||
"global::MessagePack.IFormatterResolver Instance = new ");
|
||||
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 19 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ResolverName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("();\r\n\r\n ");
|
||||
|
||||
#line 20 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 21 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ResolverName));
|
||||
|
||||
#line default
|
||||
|
@ -76,7 +77,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
{
|
||||
var f = ");
|
||||
|
||||
#line 36 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 37 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ResolverName));
|
||||
|
||||
#line default
|
||||
|
@ -92,7 +93,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
|
||||
internal static class ");
|
||||
|
||||
#line 45 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 46 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ResolverName));
|
||||
|
||||
#line default
|
||||
|
@ -100,7 +101,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write("GetFormatterHelper\r\n {\r\n static readonly global::System.Collections.Gen" +
|
||||
"eric.Dictionary<Type, int> lookup;\r\n\r\n static ");
|
||||
|
||||
#line 49 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 50 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(ResolverName));
|
||||
|
||||
#line default
|
||||
|
@ -108,35 +109,35 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write("GetFormatterHelper()\r\n {\r\n lookup = new global::System.Collecti" +
|
||||
"ons.Generic.Dictionary<Type, int>(");
|
||||
|
||||
#line 51 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 52 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(registerInfos.Length));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(")\r\n {\r\n");
|
||||
|
||||
#line 53 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 54 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
for(var i = 0; i < registerInfos.Length; i++) { var x = registerInfos[i];
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" {typeof(");
|
||||
|
||||
#line 54 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 55 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("), ");
|
||||
|
||||
#line 54 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 55 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(i));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" },\r\n");
|
||||
|
||||
#line 55 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 56 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
|
@ -145,28 +146,28 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
"\n {\r\n int key;\r\n if (!lookup.TryGetValue(t, out key" +
|
||||
")) return null;\r\n\r\n switch (key)\r\n {\r\n");
|
||||
|
||||
#line 66 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 67 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
for(var i = 0; i < registerInfos.Length; i++) { var x = registerInfos[i];
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" case ");
|
||||
|
||||
#line 67 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 68 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(i));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(": return new ");
|
||||
|
||||
#line 67 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 68 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(x.FormatterName.StartsWith("global::") ? x.FormatterName: (!string.IsNullOrEmpty(FormatterNamespace) ? FormatterNamespace + "." : FormatterNamespace) + x.FormatterName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("();\r\n");
|
||||
|
||||
#line 68 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
#line 69 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\ResolverTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
namespace <#= Namespace #>
|
||||
{
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using MessagePack;
|
||||
|
||||
public class <#= ResolverName #> : global::MessagePack.IFormatterResolver
|
||||
|
|
|
@ -36,24 +36,24 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n{\r\n using System;\r\n using System.Collections.Generic;\r\n using MessageP" +
|
||||
"ack;\r\n\r\n");
|
||||
this.Write("\r\n{\r\n using System;\r\n using System.Buffers;\r\n using System.Collections.G" +
|
||||
"eneric;\r\n using MessagePack;\r\n\r\n");
|
||||
|
||||
#line 17 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
foreach(var info in unionSerializationInfos) {
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" public sealed class ");
|
||||
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 19 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.Name));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("Formatter : global::MessagePack.Formatters.IMessagePackFormatter<");
|
||||
|
||||
#line 18 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 19 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
|
@ -62,7 +62,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
"typeToKeyAndJumpMap;\r\n readonly Dictionary<int, int> keyToJumpMap;\r\n\r\n " +
|
||||
" public ");
|
||||
|
||||
#line 23 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 24 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.Name));
|
||||
|
||||
#line default
|
||||
|
@ -70,7 +70,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write("Formatter()\r\n {\r\n this.typeToKeyAndJumpMap = new Dictionary<Run" +
|
||||
"timeTypeHandle, KeyValuePair<int, int>>(");
|
||||
|
||||
#line 25 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 26 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.SubTypes.Length));
|
||||
|
||||
#line default
|
||||
|
@ -78,70 +78,70 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write(", global::MessagePack.Internal.RuntimeTypeHandleEqualityComparer.Default)\r\n " +
|
||||
" {\r\n");
|
||||
|
||||
#line 27 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 28 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
for(var i = 0; i < info.SubTypes.Length; i++) { var item = info.SubTypes[i];
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" { typeof(");
|
||||
|
||||
#line 28 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 29 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(item.Type));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(").TypeHandle, new KeyValuePair<int, int>(");
|
||||
|
||||
#line 28 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 29 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(item.Key));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(", ");
|
||||
|
||||
#line 28 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 29 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(i));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(") },\r\n");
|
||||
|
||||
#line 29 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 30 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" };\r\n this.keyToJumpMap = new Dictionary<int, int>(");
|
||||
|
||||
#line 31 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 32 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.SubTypes.Length));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(")\r\n {\r\n");
|
||||
|
||||
#line 33 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 34 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
for(var i = 0; i < info.SubTypes.Length; i++) { var item = info.SubTypes[i];
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" { ");
|
||||
|
||||
#line 34 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 35 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(item.Key));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(", ");
|
||||
|
||||
#line 34 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 35 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(i));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" },\r\n");
|
||||
|
||||
#line 35 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 36 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
|
@ -149,7 +149,7 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
this.Write(" };\r\n }\r\n\r\n public int Serialize(ref byte[] bytes, int o" +
|
||||
"ffset, ");
|
||||
|
||||
#line 39 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 40 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
|
@ -166,143 +166,119 @@ namespace MessagePack.CodeGenerator.Generator
|
|||
{
|
||||
");
|
||||
|
||||
#line 49 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 50 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
for(var i = 0; i < info.SubTypes.Length; i++) { var item = info.SubTypes[i];
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" case ");
|
||||
|
||||
#line 50 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 51 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(i));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(":\r\n offset += formatterResolver.GetFormatterWithVerify<");
|
||||
|
||||
#line 51 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 52 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(item.Type));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(">().Serialize(ref bytes, offset, (");
|
||||
|
||||
#line 51 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 52 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(item.Type));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(")value, formatterResolver);\r\n break;\r\n");
|
||||
|
||||
#line 53 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 54 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" default:\r\n break;\r\n }\r\n" +
|
||||
"\r\n return offset - startOffset;\r\n }\r\n\r\n ret" +
|
||||
"urn MessagePackBinary.WriteNil(ref bytes, offset);\r\n }\r\n \r\n " +
|
||||
" public ");
|
||||
"urn MessagePackBinary.WriteNil(ref bytes, offset);\r\n }\r\n\r\n public " +
|
||||
"");
|
||||
|
||||
#line 64 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 65 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(@" Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
|
||||
this.Write(@" Deserialize(ref MessagePackReader reader, global::MessagePack.IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var startOffset = offset;
|
||||
|
||||
if (MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize) != 2)
|
||||
if (reader.ReadArrayHeader() != 2)
|
||||
{
|
||||
throw new InvalidOperationException(""Invalid Union data was detected. Type:");
|
||||
|
||||
#line 76 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 74 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(@""");
|
||||
}
|
||||
offset += readSize;
|
||||
|
||||
var key = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
if (!this.keyToJumpMap.TryGetValue(key, out key))
|
||||
{
|
||||
key = -1;
|
||||
}
|
||||
|
||||
");
|
||||
this.Write("\");\r\n }\r\n\r\n var key = reader.ReadInt32();\r\n\r\n if" +
|
||||
" (!this.keyToJumpMap.TryGetValue(key, out key))\r\n {\r\n " +
|
||||
"key = -1;\r\n }\r\n\r\n ");
|
||||
|
||||
#line 88 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 84 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" result = null;\r\n switch (key)\r\n {\r\n");
|
||||
|
||||
#line 91 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 87 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
for(var i = 0; i < info.SubTypes.Length; i++) { var item = info.SubTypes[i];
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(" case ");
|
||||
|
||||
#line 92 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 88 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(i));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(":\r\n result = (");
|
||||
|
||||
#line 93 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 89 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(info.FullName));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(")formatterResolver.GetFormatterWithVerify<");
|
||||
|
||||
#line 93 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 89 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
this.Write(this.ToStringHelper.ToStringWithCulture(item.Type));
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(">().Deserialize(bytes, offset, formatterResolver, out readSize);\r\n " +
|
||||
" offset += readSize;\r\n break;\r\n");
|
||||
this.Write(">().Deserialize(ref reader, formatterResolver);\r\n break;\r\n");
|
||||
|
||||
#line 96 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 91 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write(@" default:
|
||||
offset += MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
break;
|
||||
}
|
||||
this.Write(" default:\r\n reader.Skip();\r\n " +
|
||||
" break;\r\n }\r\n\r\n return result;\r\n }\r\n }\r\n\r\n");
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
");
|
||||
|
||||
#line 108 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
#line 101 "D:\git\MessagePack-CSharp\src\MessagePack.UniversalCodeGenerator\Generator\UnionTemplate.tt"
|
||||
}
|
||||
|
||||
#line default
|
||||
#line hidden
|
||||
this.Write("\r\n}\r\n\r\n#pragma warning restore 168\r\n#pragma warning restore 414\r\n#pragma warning " +
|
||||
"restore 618\r\n#pragma warning restore 612");
|
||||
"restore 618\r\n#pragma warning restore 612\r\n");
|
||||
return this.GenerationEnvironment.ToString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
namespace <#= Namespace #>
|
||||
{
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
|
||||
|
@ -60,25 +61,20 @@ namespace <#= Namespace #>
|
|||
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public <#= info.FullName #> Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
|
||||
|
||||
public <#= info.FullName #> Deserialize(ref MessagePackReader reader, global::MessagePack.IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var startOffset = offset;
|
||||
|
||||
if (MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize) != 2)
|
||||
if (reader.ReadArrayHeader() != 2)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Union data was detected. Type:<#= info.FullName #>");
|
||||
}
|
||||
offset += readSize;
|
||||
|
||||
var key = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var key = reader.ReadInt32();
|
||||
|
||||
if (!this.keyToJumpMap.TryGetValue(key, out key))
|
||||
{
|
||||
|
@ -90,17 +86,14 @@ namespace <#= Namespace #>
|
|||
{
|
||||
<# for(var i = 0; i < info.SubTypes.Length; i++) { var item = info.SubTypes[i]; #>
|
||||
case <#= i #>:
|
||||
result = (<#= info.FullName #>)formatterResolver.GetFormatterWithVerify<<#= item.Type #>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
result = (<#= info.FullName #>)formatterResolver.GetFormatterWithVerify<<#= item.Type #>>().Deserialize(ref reader, formatterResolver);
|
||||
break;
|
||||
<# } #>
|
||||
default:
|
||||
offset += MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -112,4 +105,4 @@ namespace <#= Namespace #>
|
|||
#pragma warning restore 168
|
||||
#pragma warning restore 414
|
||||
#pragma warning restore 618
|
||||
#pragma warning restore 612
|
||||
#pragma warning restore 612
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
namespace MessagePack
|
||||
{
|
||||
public struct ExtensionHeader
|
||||
{
|
||||
public sbyte TypeCode { get; private set; }
|
||||
public uint Length { get; private set; }
|
||||
|
||||
public ExtensionHeader(sbyte typeCode, uint length)
|
||||
{
|
||||
TypeCode = typeCode;
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public ExtensionHeader(sbyte typeCode, int length)
|
||||
{
|
||||
TypeCode = typeCode;
|
||||
Length = (uint)length;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
public struct ExtensionResult
|
||||
{
|
||||
public sbyte TypeCode { get; private set; }
|
||||
|
||||
public ReadOnlySequence<byte> Data { get; private set; }
|
||||
|
||||
public ExtensionResult(sbyte typeCode, Memory<byte> data)
|
||||
{
|
||||
TypeCode = typeCode;
|
||||
Data = new ReadOnlySequence<byte>(data);
|
||||
}
|
||||
|
||||
public ExtensionResult(sbyte typeCode, ReadOnlySequence<byte> data)
|
||||
{
|
||||
TypeCode = typeCode;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public ExtensionHeader Header => new ExtensionHeader(this.TypeCode, (uint)this.Data.Length);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Buffers;
|
||||
|
||||
#if !UNITY
|
||||
using System.Collections.Concurrent;
|
||||
|
@ -34,27 +35,22 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public T[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new T[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = formatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -81,18 +77,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public ArraySegment<byte> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ArraySegment<byte> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(ArraySegment<byte>);
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
// use ReadBytesSegment? But currently straem api uses memory pool so can't save arraysegment...
|
||||
var binary = MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
return new ArraySegment<byte>(binary, 0, binary.Length);
|
||||
return new ArraySegment<byte>(reader.ReadBytes().ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,17 +116,16 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public ArraySegment<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ArraySegment<T> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(ArraySegment<T>);
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var array = formatterResolver.GetFormatterWithVerify<T[]>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return new ArraySegment<T>(array, 0, array.Length);
|
||||
var array = resolver.GetFormatterWithVerify<T[]>().Deserialize(ref reader, resolver);
|
||||
return new ArraySegment<T>(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -164,27 +156,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public List<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public List<T> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var list = new List<T>(len);
|
||||
var len = reader.ReadArrayHeader();
|
||||
var list = new List<T>((int)len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
list.Add(formatter.Deserialize(bytes, offset, formatterResolver, out readSize));
|
||||
offset += readSize;
|
||||
list.Add(formatter.Deserialize(ref reader, resolver));
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -297,28 +285,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public TCollection Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public TCollection Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(TCollection);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<TElement>();
|
||||
var formatter = resolver.GetFormatterWithVerify<TElement>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
|
||||
var list = Create(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Add(list, i, formatter.Deserialize(bytes, offset, formatterResolver, out readSize));
|
||||
offset += readSize;
|
||||
Add(list, i, formatter.Deserialize(ref reader, resolver));
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return Complete(list);
|
||||
}
|
||||
|
@ -587,28 +570,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public IGrouping<TKey, TElement> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid Grouping format.");
|
||||
if (count != 2)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Grouping format.");
|
||||
}
|
||||
|
||||
var key = formatterResolver.GetFormatterWithVerify<TKey>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = formatterResolver.GetFormatterWithVerify<IEnumerable<TElement>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var key = resolver.GetFormatterWithVerify<TKey>().Deserialize(ref reader, resolver);
|
||||
var value = resolver.GetFormatterWithVerify<IEnumerable<TElement>>().Deserialize(ref reader, resolver);
|
||||
return new Grouping<TKey, TElement>(key, value);
|
||||
}
|
||||
}
|
||||
|
@ -728,28 +706,23 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(T);
|
||||
}
|
||||
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<object>();
|
||||
var startOffset = offset;
|
||||
var formatter = resolver.GetFormatterWithVerify<object>();
|
||||
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
||||
var list = new T();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
list.Add(formatter.Deserialize(bytes, offset, formatterResolver, out readSize));
|
||||
offset += readSize;
|
||||
list.Add(formatter.Deserialize(ref reader, resolver));
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -783,28 +756,23 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public IList Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public IList Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(IList);
|
||||
}
|
||||
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<object>();
|
||||
var startOffset = offset;
|
||||
var formatter = resolver.GetFormatterWithVerify<object>();
|
||||
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
||||
var list = new object[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
list[i] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
list[i] = formatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -833,31 +801,25 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<object>();
|
||||
var startOffset = offset;
|
||||
var formatter = resolver.GetFormatterWithVerify<object>();
|
||||
|
||||
var count = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadMapHeader();
|
||||
|
||||
var dict = new T();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var key = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var value = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var key = formatter.Deserialize(ref reader, resolver);
|
||||
var value = formatter.Deserialize(ref reader, resolver);
|
||||
dict.Add(key, value);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
|
@ -892,31 +854,25 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public IDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public IDictionary Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<object>();
|
||||
var startOffset = offset;
|
||||
var formatter = resolver.GetFormatterWithVerify<object>();
|
||||
|
||||
var count = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadMapHeader();
|
||||
|
||||
var dict = new Dictionary<object, object>(count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var key = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var value = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var key = formatter.Deserialize(ref reader, resolver);
|
||||
var value = formatter.Deserialize(ref reader, resolver);
|
||||
dict.Add(key, value);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Buffers;
|
||||
|
||||
#if !UNITY
|
||||
using System.Collections.Concurrent;
|
||||
|
@ -70,34 +71,28 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public TDictionary Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(TDictionary);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
|
||||
var keyFormatter = resolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = resolver.GetFormatterWithVerify<TValue>();
|
||||
|
||||
var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadMapHeader();
|
||||
|
||||
var dict = Create(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var key = keyFormatter.Deserialize(ref reader, resolver);
|
||||
|
||||
var value = valueFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var value = valueFormatter.Deserialize(ref reader, resolver);
|
||||
|
||||
Add(dict, i, key, value);
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return Complete(dict);
|
||||
}
|
||||
|
@ -314,18 +309,17 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public TDictionary Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return default(TDictionary);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
|
||||
var keyFormatter = resolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = resolver.GetFormatterWithVerify<TValue>();
|
||||
|
||||
var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
@ -333,10 +327,10 @@ namespace MessagePack.Formatters
|
|||
var dict = Create(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var key = keyFormatter.Deserialize(bytes, offset, resolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = valueFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var value = valueFormatter.Deserialize(bytes, offset, resolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
Add(dict, i, key, value);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
using MessagePack.Resolvers;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
@ -88,9 +89,9 @@ namespace MessagePack.Formatters
|
|||
return formatterAndDelegate.Value(formatterAndDelegate.Key, ref bytes, offset, value, formatterResolver);
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public object Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return PrimitiveObjectFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return PrimitiveObjectFormatter.Instance.Deserialize(ref reader, resolver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
|
@ -35,9 +36,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteString(ref bytes, offset, name);
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var name = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
var name = reader.ReadString();
|
||||
|
||||
T value;
|
||||
if (!nameValueMapping.TryGetValue(name, out value))
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -15,9 +16,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int16 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
return reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,16 +42,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int16? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
return reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,26 +83,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int16[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Int16[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadInt16();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -121,9 +115,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int32 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
return reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,16 +141,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int32? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
return reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -189,26 +182,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int32[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Int32[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadInt32();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -227,9 +214,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int64 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
return reader.ReadInt64();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,16 +240,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int64? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
return reader.ReadInt64();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -295,26 +281,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int64[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Int64[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadInt64();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -333,9 +313,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt16 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
return reader.ReadUInt16();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -359,16 +339,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt16? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
return reader.ReadUInt16();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -401,26 +380,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt16[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new UInt16[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadUInt16();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -439,9 +412,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt32 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
return reader.ReadUInt32();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -465,16 +438,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt32? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
return reader.ReadUInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -507,26 +479,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt32[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new UInt32[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadUInt32();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -545,9 +511,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt64 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
return reader.ReadUInt64();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -571,16 +537,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt64? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
return reader.ReadUInt64();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -613,26 +578,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt64[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new UInt64[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadUInt64();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -651,9 +610,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Byte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Byte Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
return reader.ReadByte();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -677,16 +636,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Byte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Byte? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
return reader.ReadByte();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -705,9 +663,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public SByte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public SByte Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
return reader.ReadSByte();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -731,16 +689,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public SByte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public SByte? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
return reader.ReadSByte();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -773,29 +730,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public SByte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public SByte[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new SByte[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadSByte();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
};
|
||||
#>
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -35,9 +36,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public <#= t.Name #> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t.Name #> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize);
|
||||
return reader.Read<#= t.Name #>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,16 +62,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public <#= t.Name #>? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t.Name #>? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize);
|
||||
return reader.Read<#= t.Name #>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -104,26 +104,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public <#= t.Name #>[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t.Name #>[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new <#= t.Name #>[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.Read<#= t.Name #>();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -131,4 +125,4 @@ namespace MessagePack.Formatters
|
|||
<# } #>
|
||||
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,21 @@
|
|||
namespace MessagePack.Formatters
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// The contract for serialization of some specific type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to be serialized or deserialized.</typeparam>
|
||||
public interface IMessagePackFormatter<T>
|
||||
{
|
||||
int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver);
|
||||
|
||||
T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
/// <summary>
|
||||
/// Deserializes a value.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader to deserialize from.</param>
|
||||
/// <param name="resolver">The resolver to use to obtain formatters for types that make up the composite type <typeparamref name="T"/>.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
namespace MessagePack.Formatters
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class IgnoreFormatter<T> : IMessagePackFormatter<T>
|
||||
{
|
||||
|
@ -7,9 +9,9 @@
|
|||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
readSize = MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
reader.Skip();
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
|
@ -38,30 +39,22 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public T[,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T[,] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,] format");
|
||||
|
||||
var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var iLength = reader.ReadInt32();
|
||||
var jLength = reader.ReadInt32();
|
||||
var maxLen = reader.ReadArrayHeader();
|
||||
|
||||
var array = new T[iLength, jLength];
|
||||
|
||||
|
@ -79,11 +72,9 @@ namespace MessagePack.Formatters
|
|||
i++;
|
||||
}
|
||||
|
||||
array[i, j] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
array[i, j] = formatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -123,33 +114,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public T[,,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T[,,] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,,] format");
|
||||
|
||||
var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var kLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var iLength = reader.ReadInt32();
|
||||
var jLength = reader.ReadInt32();
|
||||
var kLength = reader.ReadInt32();
|
||||
var maxLen = reader.ReadArrayHeader();
|
||||
|
||||
var array = new T[iLength, jLength, kLength];
|
||||
|
||||
|
@ -174,11 +155,9 @@ namespace MessagePack.Formatters
|
|||
i++;
|
||||
}
|
||||
|
||||
array[i, j, k] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
array[i, j, k] = formatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -220,37 +199,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public T[,,,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T[,,,] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,,,] format");
|
||||
|
||||
var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var kLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var lLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var iLength = reader.ReadInt32();
|
||||
var jLength = reader.ReadInt32();
|
||||
var kLength = reader.ReadInt32();
|
||||
var lLength = reader.ReadInt32();
|
||||
var maxLen = reader.ReadArrayHeader();
|
||||
var array = new T[iLength, jLength, kLength, lLength];
|
||||
|
||||
var i = 0;
|
||||
|
@ -282,13 +247,11 @@ namespace MessagePack.Formatters
|
|||
i++;
|
||||
}
|
||||
|
||||
array[i, j, k, l] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
array[i, j, k, l] = formatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -19,16 +17,16 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
readSize = 1;
|
||||
reader.ReadNil();
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return resolver.GetFormatterWithVerify<T>().Deserialize(ref reader, resolver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,16 +53,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return underlyingFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return underlyingFormatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -15,15 +16,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt64(ref bytes, offset, dateData);
|
||||
}
|
||||
|
||||
public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTime Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension)
|
||||
{
|
||||
return DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
}
|
||||
|
||||
var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
return DateTime.FromBinary(dateData);
|
||||
return reader.ReadDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,27 +45,21 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTime[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new DateTime[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
array[i] = DateTime.FromBinary(dateData);
|
||||
offset += readSize;
|
||||
var dateData = reader.ReadDateTime();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -151,9 +140,9 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public string Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
return reader.ReadString();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -202,59 +191,14 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public byte[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (type == MessagePackType.Nil)
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else if (type == MessagePackType.Binary)
|
||||
{
|
||||
return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
}
|
||||
else if (type == MessagePackType.String)
|
||||
{
|
||||
var code = bytes[offset];
|
||||
unchecked
|
||||
{
|
||||
if (MessagePackCode.MinFixStr <= code && code <= MessagePackCode.MaxFixStr)
|
||||
{
|
||||
var length = bytes[offset] & 0x1F;
|
||||
readSize = length + 1;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 1, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
else if (code == MessagePackCode.Str8)
|
||||
{
|
||||
var length = (int)bytes[offset + 1];
|
||||
readSize = length + 2;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 2, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
else if (code == MessagePackCode.Str16)
|
||||
{
|
||||
var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
|
||||
readSize = length + 3;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 3, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
else if (code == MessagePackCode.Str32)
|
||||
{
|
||||
var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]);
|
||||
readSize = length + 5;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 5, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
|
||||
return reader.ReadBytes().ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -15,9 +16,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt16(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int16 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
return reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,16 +42,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int16? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
return reader.ReadInt16();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,26 +83,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int16[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Int16[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadInt16();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -121,9 +115,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt32(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int32 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
return reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,16 +141,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int32? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
return reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -189,26 +182,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int32[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Int32[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadInt32();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -227,9 +214,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt64(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int64 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
return reader.ReadInt64();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,16 +240,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int64? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
return reader.ReadInt64();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -295,26 +281,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Int64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Int64[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Int64[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadInt64();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -333,9 +313,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteUInt16(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt16 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
return reader.ReadUInt16();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -359,16 +339,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt16? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
return reader.ReadUInt16();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -401,26 +380,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt16[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new UInt16[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadUInt16();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -439,9 +412,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteUInt32(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt32 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
return reader.ReadUInt32();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -465,16 +438,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt32? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
return reader.ReadUInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -507,26 +479,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt32[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new UInt32[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadUInt32();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -545,9 +511,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteUInt64(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt64 Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
return reader.ReadUInt64();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -571,16 +537,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt64? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
return reader.ReadUInt64();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -613,26 +578,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public UInt64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public UInt64[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new UInt64[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadUInt64();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -651,9 +610,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteSingle(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Single Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Single Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadSingle(bytes, offset, out readSize);
|
||||
return reader.ReadSingle();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -677,16 +636,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Single? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Single? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadSingle(bytes, offset, out readSize);
|
||||
return reader.ReadSingle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -719,26 +677,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Single[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Single[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Single[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadSingle(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadSingle();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -757,9 +709,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteDouble(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Double Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Double Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
return reader.ReadDouble();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -783,16 +735,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Double? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Double? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
return reader.ReadDouble();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -825,26 +776,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Double[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Double[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Double[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadDouble();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -863,9 +808,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteBoolean(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Boolean Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Boolean Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
return reader.ReadBoolean();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -889,16 +834,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Boolean? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Boolean? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
return reader.ReadBoolean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -931,26 +875,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Boolean[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Boolean[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Boolean[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadBoolean();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -969,9 +907,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteByte(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Byte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Byte Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
return reader.ReadByte();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -995,16 +933,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Byte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Byte? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
return reader.ReadByte();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1023,9 +960,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteSByte(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public SByte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public SByte Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
return reader.ReadSByte();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1049,16 +986,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public SByte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public SByte? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
return reader.ReadSByte();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1091,26 +1027,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public SByte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public SByte[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new SByte[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadSByte();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -1129,9 +1059,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteChar(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Char Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Char Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadChar(bytes, offset, out readSize);
|
||||
return reader.ReadChar();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1155,16 +1085,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Char? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Char? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadChar(bytes, offset, out readSize);
|
||||
return reader.ReadChar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1197,26 +1126,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Char[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Char[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new Char[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadChar(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadChar();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -1235,9 +1158,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteDateTime(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTime Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
return reader.ReadDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1261,16 +1184,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public DateTime? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTime? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
return reader.ReadDateTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1303,29 +1225,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTime[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new DateTime[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadDateTime();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
};
|
||||
#>
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -40,9 +41,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.Write<#= t.Name #>(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public <#= t.Name #> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t.Name #> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize);
|
||||
return reader.Read<#= t.Name #>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,16 +67,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public <#= t.Name #>? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t.Name #>? Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize);
|
||||
return reader.Read<#= t.Name #>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -109,26 +109,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public <#= t.Name #>[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t.Name #>[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new <#= t.Name #>[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.Read<#= t.Name #>();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -136,4 +130,4 @@ namespace MessagePack.Formatters
|
|||
<# } #>
|
||||
|
||||
<# } #>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
@ -156,87 +157,79 @@ namespace MessagePack.Formatters
|
|||
throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name);
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public object Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
var type = reader.NextMessagePackType;
|
||||
switch (type)
|
||||
{
|
||||
case MessagePackType.Integer:
|
||||
var code = bytes[offset];
|
||||
if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int8) return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int16) return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int32) return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int64) return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt8) return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt16) return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt32) return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt64) return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
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 MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
return reader.ReadBoolean();
|
||||
case MessagePackType.Float:
|
||||
if (MessagePackCode.Float32 == bytes[offset])
|
||||
if (MessagePackCode.Float32 == reader.NextCode)
|
||||
{
|
||||
return MessagePackBinary.ReadSingle(bytes, offset, out readSize);
|
||||
return reader.ReadSingle();
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
return reader.ReadDouble();
|
||||
}
|
||||
case MessagePackType.String:
|
||||
return MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
return reader.ReadString();
|
||||
case MessagePackType.Binary:
|
||||
return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
return reader.ReadBytes();
|
||||
case MessagePackType.Extension:
|
||||
var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
var ext = reader.ReadExtensionFormatHeader();
|
||||
if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
|
||||
{
|
||||
return MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
return reader.ReadDateTime(ext);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Invalid primitive bytes.");
|
||||
case MessagePackType.Array:
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var startOffset = offset;
|
||||
offset += readSize;
|
||||
var length = reader.ReadArrayHeader();
|
||||
|
||||
var objectFormatter = formatterResolver.GetFormatter<object>();
|
||||
var objectFormatter = resolver.GetFormatter<object>();
|
||||
var array = new object[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
array[i] = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = objectFormatter.Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
case MessagePackType.Map:
|
||||
{
|
||||
var length = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
var startOffset = offset;
|
||||
offset += readSize;
|
||||
var length = reader.ReadMapHeader();
|
||||
|
||||
var objectFormatter = formatterResolver.GetFormatter<object>();
|
||||
var objectFormatter = resolver.GetFormatter<object>();
|
||||
var hash = new Dictionary<object, object>(length);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var key = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var key = objectFormatter.Deserialize(ref reader, resolver);
|
||||
|
||||
var value = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var value = objectFormatter.Deserialize(ref reader, resolver);
|
||||
|
||||
hash.Add(key, value);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return hash;
|
||||
}
|
||||
case MessagePackType.Nil:
|
||||
readSize = 1;
|
||||
reader.ReadNil();
|
||||
return null;
|
||||
default:
|
||||
throw new InvalidOperationException("Invalid primitive bytes.");
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
@ -20,7 +21,6 @@ namespace MessagePack.Formatters
|
|||
|
||||
ByteArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver)
|
||||
|
@ -28,9 +28,16 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteBytes(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public byte[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return reader.ReadBytes().ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,7 +47,6 @@ namespace MessagePack.Formatters
|
|||
|
||||
NullableStringFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, String value, IFormatterResolver typeResolver)
|
||||
|
@ -48,9 +54,16 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteString(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public String Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
|
||||
public String Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return reader.ReadString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,26 +95,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public String[] Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
|
||||
public String[] Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
var array = new String[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadString();
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -121,9 +128,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteString(ref bytes, offset, value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public decimal Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return decimal.Parse(MessagePackBinary.ReadString(bytes, offset, out readSize), CultureInfo.InvariantCulture);
|
||||
return decimal.Parse(reader.ReadString(), CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,9 +148,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteInt64(ref bytes, offset, value.Ticks);
|
||||
}
|
||||
|
||||
public TimeSpan Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public TimeSpan Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return new TimeSpan(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
|
||||
return new TimeSpan(reader.ReadInt64());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,21 +172,16 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public DateTimeOffset Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTimeOffset Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid DateTimeOffset format.");
|
||||
|
||||
var utc = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var utc = reader.ReadDateTime();
|
||||
|
||||
var dtOffsetMinutes = MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var dtOffsetMinutes = reader.ReadInt16();
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return new DateTimeOffset(utc.Ticks, TimeSpan.FromMinutes(dtOffsetMinutes));
|
||||
}
|
||||
|
@ -189,10 +191,8 @@ namespace MessagePack.Formatters
|
|||
{
|
||||
public static readonly IMessagePackFormatter<Guid> Instance = new GuidFormatter();
|
||||
|
||||
|
||||
GuidFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Guid value, IFormatterResolver formatterResolver)
|
||||
|
@ -205,10 +205,27 @@ namespace MessagePack.Formatters
|
|||
return 38;
|
||||
}
|
||||
|
||||
public Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Guid Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var segment = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
return new GuidBits(segment).Value;
|
||||
var segment = reader.ReadStringSegment();
|
||||
if (segment.Length != 36)
|
||||
{
|
||||
throw new InvalidOperationException("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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,16 +251,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Uri Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Uri Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Uri(MessagePackBinary.ReadString(bytes, offset, out readSize), UriKind.RelativeOrAbsolute);
|
||||
return new Uri(reader.ReadString(), UriKind.RelativeOrAbsolute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -269,16 +285,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Version Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Version Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Version(MessagePackBinary.ReadString(bytes, offset, out readSize));
|
||||
return new Version(reader.ReadString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -294,21 +309,14 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public KeyValuePair<TKey, TValue> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public KeyValuePair<TKey, TValue> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid KeyValuePair format.");
|
||||
|
||||
var key = formatterResolver.GetFormatterWithVerify<TKey>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = formatterResolver.GetFormatterWithVerify<TValue>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var key = resolver.GetFormatterWithVerify<TKey>().Deserialize(ref reader, resolver);
|
||||
var value = resolver.GetFormatterWithVerify<TValue>().Deserialize(ref reader, resolver);
|
||||
return new KeyValuePair<TKey, TValue>(key, value);
|
||||
}
|
||||
}
|
||||
|
@ -334,16 +342,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public StringBuilder Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public StringBuilder Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StringBuilder(MessagePackBinary.ReadString(bytes, offset, out readSize));
|
||||
return new StringBuilder(reader.ReadString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -377,28 +384,22 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public BitArray Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public BitArray Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var len = reader.ReadArrayHeader();
|
||||
|
||||
var array = new BitArray(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
array[i] = reader.ReadBoolean();
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -420,9 +421,30 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteBytes(ref bytes, offset, value.ToByteArray());
|
||||
}
|
||||
|
||||
public System.Numerics.BigInteger Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public System.Numerics.BigInteger Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
return new System.Numerics.BigInteger(MessagePackBinary.ReadBytes(bytes, offset, out readSize));
|
||||
var bytes = reader.ReadBytes();
|
||||
#if NETCOREAPP2_1
|
||||
if (bytes.IsSingleSegment)
|
||||
{
|
||||
return new System.Numerics.BigInteger(bytes.First.Span);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] bytesArray = ArrayPool<byte>.Shared.Rent((int)bytes.Length);
|
||||
try
|
||||
{
|
||||
bytes.CopyTo(bytesArray);
|
||||
return new System.Numerics.BigInteger(bytesArray.AsSpan(0, (int)bytes.Length));
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(bytesArray);
|
||||
}
|
||||
}
|
||||
#else
|
||||
return new System.Numerics.BigInteger(bytes.ToArray());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -444,21 +466,16 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public System.Numerics.Complex Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public System.Numerics.Complex Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid Complex format.");
|
||||
|
||||
var real = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var real = reader.ReadDouble();
|
||||
|
||||
var imaginary = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var imaginary = reader.ReadDouble();
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new System.Numerics.Complex(real, imaginary);
|
||||
}
|
||||
}
|
||||
|
@ -477,22 +494,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Lazy<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Lazy<T> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// deserialize immediately(no delay, because capture byte[] causes memory leak)
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var v = resolver.GetFormatterWithVerify<T>().Deserialize(ref reader, resolver);
|
||||
return new Lazy<T>(() => v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable VSTHRD002 // This section will be removed when https://github.com/AArnott/MessagePack-CSharp/issues/29 is fixed
|
||||
|
||||
public sealed class TaskUnitFormatter : IMessagePackFormatter<Task>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<Task> Instance = new TaskUnitFormatter();
|
||||
|
@ -516,16 +534,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Task Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Task Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (!MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
throw new InvalidOperationException("Invalid input");
|
||||
return CompletedTask;
|
||||
}
|
||||
else
|
||||
{
|
||||
readSize = 1;
|
||||
return CompletedTask;
|
||||
throw new InvalidOperationException("Invalid input");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -545,16 +562,15 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Task<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Task<T> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var v = resolver.GetFormatterWithVerify<T>().Deserialize(ref reader, resolver);
|
||||
return Task.FromResult(v);
|
||||
}
|
||||
}
|
||||
|
@ -567,12 +583,14 @@ namespace MessagePack.Formatters
|
|||
return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Result, formatterResolver);
|
||||
}
|
||||
|
||||
public ValueTask<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTask<T> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
var v = resolver.GetFormatterWithVerify<T>().Deserialize(ref reader, resolver);
|
||||
return new ValueTask<T>(v);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning restore VSTHRD002
|
||||
|
||||
#endif
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#if !UNITY
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -24,24 +25,19 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 1) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1>(item1);
|
||||
}
|
||||
}
|
||||
|
@ -68,26 +64,20 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 2) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2>(item1, item2);
|
||||
}
|
||||
}
|
||||
|
@ -115,28 +105,21 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2, T3> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 3) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2, T3>(item1, item2, item3);
|
||||
}
|
||||
}
|
||||
|
@ -165,30 +148,22 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2, T3, T4> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 4) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4);
|
||||
}
|
||||
}
|
||||
|
@ -218,32 +193,23 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2, T3, T4, T5> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 5) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
|
||||
}
|
||||
}
|
||||
|
@ -274,34 +240,24 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5, T6> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2, T3, T4, T5, T6> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 6) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
var item6 = resolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
|
||||
}
|
||||
}
|
||||
|
@ -333,36 +289,25 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 7) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
var item6 = resolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, resolver);
|
||||
var item7 = resolver.GetFormatterWithVerify<T7>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
|
||||
}
|
||||
}
|
||||
|
@ -395,38 +340,26 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 8) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item8 = formatterResolver.GetFormatterWithVerify<TRest>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
var item6 = resolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, resolver);
|
||||
var item7 = resolver.GetFormatterWithVerify<T7>().Deserialize(ref reader, resolver);
|
||||
var item8 = resolver.GetFormatterWithVerify<TRest>().Deserialize(ref reader, resolver);
|
||||
|
||||
return new Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(item1, item2, item3, item4, item5, item6, item7, item8);
|
||||
}
|
||||
}
|
||||
|
@ -434,4 +367,4 @@ namespace MessagePack.Formatters
|
|||
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -4,17 +4,18 @@
|
|||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ output extension=".cs" #>
|
||||
#if NETSTANDARD
|
||||
#if !UNITY
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
<# for(var i = 1; i <= 8; i++) {
|
||||
<# for(var i = 1; i <= 8; i++) {
|
||||
Func<int, string> toT = x => "T" + ((x == 8) ? "Rest" : x.ToString());
|
||||
Func<int, string> toItem = x => ((x == 8) ? "Rest" : "Item" + x);
|
||||
var ts = string.Join(", ", Enumerable.Range(1, i).Select(x => toT(x)));
|
||||
var t = "Tuple<" + ts + ">";
|
||||
var ts = string.Join(", ", Enumerable.Range(1, i).Select(x => toT(x)));
|
||||
var t = "Tuple<" + ts + ">";
|
||||
#>
|
||||
|
||||
public sealed class TupleFormatter<<#= ts #>> : IMessagePackFormatter<<#= t #>>
|
||||
|
@ -38,26 +39,21 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
public <#= t #> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t #> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != <#= i #>) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
<# for(var j = 1; j <= i; j++) { #>
|
||||
var item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item<#= j #> = resolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(ref reader, resolver);
|
||||
<# } #>
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return new Tuple<<#= ts #>>(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => "item" + x)) #>);
|
||||
}
|
||||
}
|
||||
|
@ -66,4 +62,4 @@ namespace MessagePack.Formatters
|
|||
<# } #>
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
#if !UNITY
|
||||
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using MessagePack.Internal;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -21,7 +23,7 @@ namespace MessagePack.Formatters
|
|||
static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+", RegexOptions.Compiled);
|
||||
|
||||
delegate int SerializeMethod(object dynamicContractlessFormatter, ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver);
|
||||
delegate object DeserializeMethod(object dynamicContractlessFormatter, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
delegate object DeserializeMethod(object dynamicContractlessFormatter, ref MessagePackReader reader, IFormatterResolver resolver);
|
||||
|
||||
readonly ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>> serializers = new ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>>();
|
||||
readonly ThreadsafeTypeKeyHashTable<KeyValuePair<object, DeserializeMethod>> deserializers = new ThreadsafeTypeKeyHashTable<KeyValuePair<object, DeserializeMethod>>();
|
||||
|
@ -93,11 +95,7 @@ namespace MessagePack.Formatters
|
|||
public TypelessFormatter()
|
||||
{
|
||||
serializers.TryAdd(typeof(object), _ => new KeyValuePair<object, SerializeMethod>(null, (object p1, ref byte[] p2, int p3, object p4, IFormatterResolver p5) => 0));
|
||||
deserializers.TryAdd(typeof(object), _ => new KeyValuePair<object, DeserializeMethod>(null, (object p1, byte[] p2, int p3, IFormatterResolver p4, out int p5) =>
|
||||
{
|
||||
p5 = 0;
|
||||
return new object();
|
||||
}));
|
||||
deserializers.TryAdd(typeof(object), _ => new KeyValuePair<object, DeserializeMethod>(null, (object p1, ref MessagePackReader p2, IFormatterResolver p3) => new object()));
|
||||
}
|
||||
|
||||
// see:http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
|
||||
|
@ -207,41 +205,49 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public object Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.TryReadNil())
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
int startOffset = offset;
|
||||
var packType = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (packType == MessagePackType.Extension)
|
||||
if (reader.NextMessagePackType == MessagePackType.Extension)
|
||||
{
|
||||
var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
var ext = reader.ReadExtensionFormatHeader();
|
||||
if (ext.TypeCode == TypelessFormatter.ExtensionTypeCode)
|
||||
{
|
||||
// it has type name serialized
|
||||
offset += readSize;
|
||||
var typeName = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var result = DeserializeByTypeName(typeName, bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
readSize = offset - startOffset;
|
||||
var 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 = DeserializeByTypeName(typeNameArraySegment, ref reader, resolver);
|
||||
|
||||
if (rented != null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(rented);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// fallback
|
||||
return Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter<object>().Deserialize(bytes, startOffset, formatterResolver, out readSize);
|
||||
return Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter<object>().Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does not support deserializing of anonymous types
|
||||
/// Type should be covered by preceeding resolvers in complex/standard resolver
|
||||
/// </summary>
|
||||
private object DeserializeByTypeName(ArraySegment<byte> typeName, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
private object DeserializeByTypeName(ArraySegment<byte> typeName, ref MessagePackReader byteSequence, IFormatterResolver resolver)
|
||||
{
|
||||
// try get type with assembly name, throw if not found
|
||||
Type type;
|
||||
|
@ -280,33 +286,32 @@ namespace MessagePack.Formatters
|
|||
{
|
||||
var ti = type.GetTypeInfo();
|
||||
|
||||
var formatter = formatterResolver.GetFormatterDynamic(type);
|
||||
var formatter = resolver.GetFormatterDynamic(type);
|
||||
if (formatter == null)
|
||||
{
|
||||
throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolver:" + formatterResolver.GetType().Name);
|
||||
throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolver:" + resolver.GetType().Name);
|
||||
}
|
||||
|
||||
var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(type);
|
||||
var param0 = Expression.Parameter(typeof(object), "formatter");
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(int), "offset");
|
||||
var param3 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");
|
||||
var param4 = Expression.Parameter(typeof(int).MakeByRefType(), "readSize");
|
||||
var param1 = Expression.Parameter(typeof(MessagePackReader).MakeByRefType(), "reader");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
|
||||
var deserializeMethodInfo = formatterType.GetRuntimeMethod("Deserialize", new[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
var deserializeMethodInfo = formatterType.GetRuntimeMethod("Deserialize", new[] { typeof(MessagePackReader).MakeByRefType(), typeof(IFormatterResolver) });
|
||||
|
||||
var deserialize = Expression.Call(
|
||||
Expression.Convert(param0, formatterType),
|
||||
deserializeMethodInfo,
|
||||
param1,
|
||||
param2,
|
||||
param3,
|
||||
param4);
|
||||
param2);
|
||||
|
||||
Expression body = deserialize;
|
||||
if (ti.IsValueType)
|
||||
{
|
||||
body = Expression.Convert(deserialize, typeof(object));
|
||||
var lambda = Expression.Lambda<DeserializeMethod>(body, param0, param1, param2, param3, param4).Compile();
|
||||
}
|
||||
|
||||
var lambda = Expression.Lambda<DeserializeMethod>(body, param0, param1, param2).Compile();
|
||||
|
||||
formatterAndDelegate = new KeyValuePair<object, DeserializeMethod>(formatter, lambda);
|
||||
deserializers.TryAdd(type, formatterAndDelegate);
|
||||
|
@ -314,7 +319,7 @@ namespace MessagePack.Formatters
|
|||
}
|
||||
}
|
||||
|
||||
return formatterAndDelegate.Value(formatterAndDelegate.Key, bytes, offset, formatterResolver, out readSize);
|
||||
return formatterAndDelegate.Value(formatterAndDelegate.Key, ref byteSequence, resolver);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#if !UNITY
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -36,30 +37,20 @@ namespace MessagePack.Formatters
|
|||
return 18;
|
||||
}
|
||||
|
||||
public unsafe Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public unsafe Guid Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian) throw new Exception("BinaryGuidFormatter only allows on little endian env.");
|
||||
|
||||
if (!(offset + 18 <= bytes.Length))
|
||||
var valueSequence = reader.ReadBytes();
|
||||
if (valueSequence.Length != sizeof(Guid))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
throw new InvalidOperationException("Invalid Guid Size.");
|
||||
}
|
||||
|
||||
fixed (byte* src = &bytes[offset])
|
||||
{
|
||||
if (src[0] != MessagePackCode.Bin8)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
|
||||
}
|
||||
if (src[1] != 16)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Guid Size.");
|
||||
}
|
||||
|
||||
var target = *(Guid*)(src + 2);
|
||||
readSize = 18;
|
||||
return target;
|
||||
}
|
||||
Guid result;
|
||||
var resultSpan = new Span<byte>(&result, sizeof(Guid));
|
||||
valueSequence.CopyTo(resultSpan);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,30 +86,20 @@ namespace MessagePack.Formatters
|
|||
return 18;
|
||||
}
|
||||
|
||||
public unsafe Decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public unsafe Decimal Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian) throw new Exception("BinaryDecimalFormatter only allows on little endian env.");
|
||||
|
||||
if (!(offset + 18 <= bytes.Length))
|
||||
var valueSequence = reader.ReadBytes();
|
||||
if (valueSequence.Length != sizeof(decimal))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
throw new InvalidOperationException("Invalid decimal Size.");
|
||||
}
|
||||
|
||||
fixed (byte* src = &bytes[offset])
|
||||
{
|
||||
if (src[0] != MessagePackCode.Bin8)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
|
||||
}
|
||||
if (src[1] != 16)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Guid Size.");
|
||||
}
|
||||
|
||||
var target = *(Decimal*)(src + 2);
|
||||
readSize = 18;
|
||||
return target;
|
||||
}
|
||||
decimal result;
|
||||
var resultSpan = new Span<byte>(&result, sizeof(decimal));
|
||||
valueSequence.CopyTo(resultSpan);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#if !UNITY
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -16,23 +17,19 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 1) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1>(item1);
|
||||
}
|
||||
}
|
||||
|
@ -52,25 +49,20 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 2) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2>(item1, item2);
|
||||
}
|
||||
}
|
||||
|
@ -91,27 +83,21 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2, T3> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 3) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3>(item1, item2, item3);
|
||||
}
|
||||
}
|
||||
|
@ -133,29 +119,22 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2, T3, T4> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 4) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4>(item1, item2, item3, item4);
|
||||
}
|
||||
}
|
||||
|
@ -178,31 +157,23 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2, T3, T4, T5> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 5) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
|
||||
}
|
||||
}
|
||||
|
@ -226,33 +197,24 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 6) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
var item6 = resolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
|
||||
}
|
||||
}
|
||||
|
@ -277,35 +239,25 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 7) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
var item6 = resolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, resolver);
|
||||
var item7 = resolver.GetFormatterWithVerify<T7>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
|
||||
}
|
||||
}
|
||||
|
@ -331,37 +283,26 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != 8) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item8 = formatterResolver.GetFormatterWithVerify<TRest>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item1 = resolver.GetFormatterWithVerify<T1>().Deserialize(ref reader, resolver);
|
||||
var item2 = resolver.GetFormatterWithVerify<T2>().Deserialize(ref reader, resolver);
|
||||
var item3 = resolver.GetFormatterWithVerify<T3>().Deserialize(ref reader, resolver);
|
||||
var item4 = resolver.GetFormatterWithVerify<T4>().Deserialize(ref reader, resolver);
|
||||
var item5 = resolver.GetFormatterWithVerify<T5>().Deserialize(ref reader, resolver);
|
||||
var item6 = resolver.GetFormatterWithVerify<T6>().Deserialize(ref reader, resolver);
|
||||
var item7 = resolver.GetFormatterWithVerify<T7>().Deserialize(ref reader, resolver);
|
||||
var item8 = resolver.GetFormatterWithVerify<TRest>().Deserialize(ref reader, resolver);
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(item1, item2, item3, item4, item5, item6, item7, item8);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
<#@ output extension=".cs" #>
|
||||
#if !UNITY
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
@ -30,25 +31,21 @@ namespace MessagePack.Formatters
|
|||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public <#= t #> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public <#= t #> Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var count = reader.ReadArrayHeader();
|
||||
if (count != <#= i #>) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
<# for(var j = 1; j <= i; j++) { #>
|
||||
var item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item<#= j #> = resolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(ref reader, resolver);
|
||||
<# } #>
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<<#= ts #>>(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => "item" + x)) #>);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal sealed class BufferPool : ArrayPool<byte>
|
||||
{
|
||||
internal static readonly BufferPool Default = new BufferPool(65535);
|
||||
|
||||
internal BufferPool(int bufferLength)
|
||||
: base(bufferLength)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class ArrayPool<T>
|
||||
{
|
||||
readonly int bufferLength;
|
||||
readonly object gate;
|
||||
int index;
|
||||
T[][] buffers;
|
||||
|
||||
public ArrayPool(int bufferLength)
|
||||
{
|
||||
this.bufferLength = bufferLength;
|
||||
this.buffers = new T[4][];
|
||||
this.gate = new object();
|
||||
}
|
||||
|
||||
public T[] Rent()
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (index >= buffers.Length)
|
||||
{
|
||||
Array.Resize(ref buffers, buffers.Length * 2);
|
||||
}
|
||||
|
||||
if (buffers[index] == null)
|
||||
{
|
||||
buffers[index] = new T[bufferLength];
|
||||
}
|
||||
|
||||
var buffer = buffers[index];
|
||||
buffers[index] = null;
|
||||
index++;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
public void Return(T[] array)
|
||||
{
|
||||
if (array.Length != bufferLength)
|
||||
{
|
||||
throw new InvalidOperationException("return buffer is not from pool");
|
||||
}
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
if (index != 0)
|
||||
{
|
||||
buffers[--index] = array;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -187,7 +187,7 @@ namespace MessagePack.Internal
|
|||
if (item == null) continue;
|
||||
foreach (var item2 in item)
|
||||
{
|
||||
yield return new KeyValuePair<string, int>(Encoding.UTF8.GetString(item2.Key), item2.Value);
|
||||
yield return new KeyValuePair<string, int>(Encoding.UTF8.GetString(item2.Key.Span), item2.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ namespace MessagePack.Internal
|
|||
// for debugging
|
||||
public override string ToString()
|
||||
{
|
||||
return "(" + Encoding.UTF8.GetString(Key) + ", " + Value + ")";
|
||||
return "(" + Encoding.UTF8.GetString(Key.Span) + ", " + Value + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class DateTimeConstants
|
||||
{
|
||||
internal static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
internal const long BclSecondsAtUnixEpoch = 62135596800;
|
||||
internal const int NanosecondsPerTick = 100;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,33 +4,7 @@ namespace MessagePack.LZ4
|
|||
{
|
||||
partial class LZ4Codec
|
||||
{
|
||||
#if !UNITY
|
||||
|
||||
public static int Encode(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Encode32Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Encode64Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Decode(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Decode32Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Decode64Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
#if UNITY
|
||||
|
||||
// use 'Safe' code for Unity because in IL2CPP gots strange behaviour.
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#if ENABLE_UNSAFE_MSGPACK
|
||||
#if !UNITY
|
||||
|
||||
#region license
|
||||
|
||||
|
@ -69,33 +69,28 @@ namespace MessagePack.LZ4
|
|||
|
||||
/// <summary>Encodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Encode32Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
public static unsafe int Encode(ReadOnlySpan<byte> input, Span<byte> output)
|
||||
{
|
||||
CheckArguments(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
if (output.Length == 0) throw new ArgumentException("Output is empty.");
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
fixed (byte* inputPtr = input)
|
||||
fixed (byte* outputPtr = output)
|
||||
{
|
||||
if (inputLength < LZ4_64KLIMIT)
|
||||
if (input.Length < LZ4_64KLIMIT)
|
||||
{
|
||||
var uHashTable = HashTablePool.GetUShortHashTablePool();
|
||||
fixed (ushort* hash1 = &uHashTable[0])
|
||||
{
|
||||
return LZ4_compress64kCtx_32(hash1, inputPtr, outputPtr, inputLength, outputLength);
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4_compress64kCtx_32(hash1, inputPtr, outputPtr, input.Length, output.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4_compress64kCtx_64(hash1, inputPtr, outputPtr, input.Length, output.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -103,7 +98,14 @@ namespace MessagePack.LZ4
|
|||
var bHashTable = HashTablePool.GetUIntHashTablePool();
|
||||
fixed (uint* hash2 = &bHashTable[0])
|
||||
{
|
||||
return LZ4_compressCtx_32(hash2, inputPtr, outputPtr, inputLength, outputLength);
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4_compressCtx_32(hash2, inputPtr, outputPtr, input.Length, output.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4_compressCtx_64(hash2, inputPtr, outputPtr, input.Length, output.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,109 +113,28 @@ namespace MessagePack.LZ4
|
|||
|
||||
/// <summary>Decodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Decode32Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
public static unsafe int Decode(ReadOnlySpan<byte> input, Span<byte> output)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
if (output.Length == 0) throw new ArgumentException("Output is empty.");
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
fixed (byte* inputPtr = input)
|
||||
fixed (byte* outputPtr = output)
|
||||
{
|
||||
var length = LZ4_uncompress_32(inputPtr, outputPtr, outputLength);
|
||||
if (length != inputLength)
|
||||
throw new ArgumentException("LZ4 block is corrupted, or invalid length has been given.");
|
||||
return outputLength;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Encodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Encode64Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
{
|
||||
if (inputLength < LZ4_64KLIMIT)
|
||||
int length;
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
var uHashTable = HashTablePool.GetUShortHashTablePool();
|
||||
fixed (ushort* h1 = &uHashTable[0])
|
||||
{
|
||||
return LZ4_compress64kCtx_64(h1, inputPtr, outputPtr, inputLength, outputLength);
|
||||
}
|
||||
length = LZ4_uncompress_32(inputPtr, outputPtr, output.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
var uiHashTable = HashTablePool.GetUIntHashTablePool();
|
||||
fixed (uint* h2 = &uiHashTable[0])
|
||||
{
|
||||
return LZ4_compressCtx_64(h2, inputPtr, outputPtr, inputLength, outputLength);
|
||||
}
|
||||
length = LZ4_uncompress_64(inputPtr, outputPtr, output.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Decode64s the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Decode64Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
{
|
||||
var length = LZ4_uncompress_64(inputPtr, outputPtr, outputLength);
|
||||
if (length != inputLength)
|
||||
if (length != input.Length)
|
||||
throw new ArgumentException("LZ4 block is corrupted, or invalid length has been given.");
|
||||
return outputLength;
|
||||
return output.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using MessagePack.Internal;
|
||||
using MessagePack.LZ4;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
@ -14,37 +15,20 @@ namespace MessagePack
|
|||
/// <summary>
|
||||
/// Dump message-pack binary to JSON string.
|
||||
/// </summary>
|
||||
public override string ConvertToJson(byte[] bytes)
|
||||
public override void ConvertToJson(ref MessagePackReader reader, TextWriter jsonWriter)
|
||||
{
|
||||
if (bytes == null || bytes.Length == 0) return "";
|
||||
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes, 0) == MessagePackType.Extension)
|
||||
using (var scratch = new Nerdbank.Streams.Sequence<byte>())
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes, 0, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
if (TryDecompress(ref reader, scratch))
|
||||
{
|
||||
// decode lz4
|
||||
var offset = readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = LZ4MemoryPool.GetBuffer();
|
||||
if (buffer.Length < length)
|
||||
{
|
||||
buffer = new byte[length];
|
||||
}
|
||||
|
||||
// LZ4 Decode
|
||||
LZ4Codec.Decode(bytes, offset, bytes.Length - offset, buffer, 0, length);
|
||||
|
||||
bytes = buffer; // use LZ4 bytes
|
||||
var scratchReader = new MessagePackReader(scratch.AsReadOnlySequence);
|
||||
base.ConvertToJson(ref scratchReader, jsonWriter);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.ConvertToJson(ref reader, jsonWriter);
|
||||
}
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
ToJsonCore(bytes, 0, sb);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -55,214 +39,5 @@ namespace MessagePack
|
|||
var buffer = MessagePackSerializer.FromJsonUnsafe(reader); // offset is guranteed from 0
|
||||
return LZ4MessagePackSerializer.ToLZ4Binary(buffer);
|
||||
}
|
||||
|
||||
static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder)
|
||||
{
|
||||
var readSize = 0;
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
switch (type)
|
||||
{
|
||||
case MessagePackType.Integer:
|
||||
var code = bytes[offset];
|
||||
if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
|
||||
else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int8) builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int16) builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int32) builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int64) builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt8) builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt16) builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt32) builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt64) builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize));
|
||||
break;
|
||||
case MessagePackType.Boolean:
|
||||
builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false");
|
||||
break;
|
||||
case MessagePackType.Float:
|
||||
var floatCode = bytes[offset];
|
||||
if (floatCode == MessagePackCode.Float32)
|
||||
{
|
||||
builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
break;
|
||||
case MessagePackType.String:
|
||||
WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder);
|
||||
break;
|
||||
case MessagePackType.Binary:
|
||||
builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\"");
|
||||
break;
|
||||
case MessagePackType.Array:
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize);
|
||||
var totalReadSize = readSize;
|
||||
offset += readSize;
|
||||
builder.Append("[");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
|
||||
if (i != length - 1)
|
||||
{
|
||||
builder.Append(",");
|
||||
}
|
||||
}
|
||||
builder.Append("]");
|
||||
|
||||
return totalReadSize;
|
||||
}
|
||||
case MessagePackType.Map:
|
||||
{
|
||||
var length = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize);
|
||||
var totalReadSize = readSize;
|
||||
offset += readSize;
|
||||
builder.Append("{");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
// write key
|
||||
{
|
||||
var keyType = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (keyType == MessagePackType.String || keyType == MessagePackType.Binary)
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("\"");
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
builder.Append("\"");
|
||||
}
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
}
|
||||
|
||||
builder.Append(":");
|
||||
|
||||
// write body
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
}
|
||||
|
||||
if (i != length - 1)
|
||||
{
|
||||
builder.Append(",");
|
||||
}
|
||||
}
|
||||
builder.Append("}");
|
||||
|
||||
return totalReadSize;
|
||||
}
|
||||
case MessagePackType.Extension:
|
||||
var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
|
||||
{
|
||||
var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
builder.Append("\"");
|
||||
builder.Append(dt.ToString("o", CultureInfo.InvariantCulture));
|
||||
builder.Append("\"");
|
||||
}
|
||||
#if !UNITY
|
||||
else if (extHeader.TypeCode == TypelessFormatter.ExtensionTypeCode)
|
||||
{
|
||||
int startOffset = offset;
|
||||
// prepare type name token
|
||||
offset += 6;
|
||||
var typeNameToken = new StringBuilder();
|
||||
var typeNameReadSize = ToJsonCore(bytes, offset, typeNameToken);
|
||||
offset += typeNameReadSize;
|
||||
int startBuilderLength = builder.Length;
|
||||
if (extHeader.Length > typeNameReadSize)
|
||||
{
|
||||
// object map or array
|
||||
var typeInside = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
|
||||
builder.Append("{");
|
||||
offset += ToJsonCore(bytes, offset, builder);
|
||||
// insert type name token to start of object map or array
|
||||
if (typeInside != MessagePackType.Array)
|
||||
typeNameToken.Insert(0, "\"$type\":");
|
||||
if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
|
||||
builder.Append("}");
|
||||
if (builder.Length - startBuilderLength > 2)
|
||||
typeNameToken.Append(",");
|
||||
builder.Insert(startBuilderLength + 1, typeNameToken.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("{\"$type\":\"" + typeNameToken.ToString() + "}");
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize);
|
||||
builder.Append("[");
|
||||
builder.Append(ext.TypeCode);
|
||||
builder.Append(",");
|
||||
builder.Append("\"");
|
||||
builder.Append(Convert.ToBase64String(ext.Data));
|
||||
builder.Append("\"");
|
||||
builder.Append("]");
|
||||
}
|
||||
break;
|
||||
case MessagePackType.Unknown:
|
||||
case MessagePackType.Nil:
|
||||
default:
|
||||
readSize = 1;
|
||||
builder.Append("null");
|
||||
break;
|
||||
}
|
||||
|
||||
return readSize;
|
||||
}
|
||||
|
||||
// escape string
|
||||
static void WriteJsonString(string value, StringBuilder builder)
|
||||
{
|
||||
builder.Append('\"');
|
||||
|
||||
var len = value.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var c = value[i];
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
builder.Append("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
builder.Append("\\\\");
|
||||
break;
|
||||
case '\b':
|
||||
builder.Append("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
builder.Append("\\f");
|
||||
break;
|
||||
case '\n':
|
||||
builder.Append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
builder.Append("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
builder.Append("\\t");
|
||||
break;
|
||||
default:
|
||||
builder.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
builder.Append('\"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,24 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using MessagePack.LZ4;
|
||||
using Microsoft;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
/// <summary>
|
||||
/// LZ4 Compressed special serializer.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The spec for this is:
|
||||
/// Extension header: (typecode = 99)
|
||||
/// 32-bit integer with length of *uncompressed* data (as a MessagePack Int32 entity)
|
||||
/// compressed data (raw -- not as a raw/bytes MessagePack entity)
|
||||
/// </remarks>
|
||||
public partial class LZ4MessagePackSerializer : MessagePackSerializer
|
||||
{
|
||||
public const sbyte ExtensionTypeCode = 99;
|
||||
|
@ -79,7 +90,7 @@ namespace MessagePack
|
|||
offset += (6 + 5);
|
||||
|
||||
// write body
|
||||
var lz4Length = LZ4Codec.Encode(serializedData.Array, serializedData.Offset, serializedData.Count, bytes, offset, bytes.Length - offset);
|
||||
var lz4Length = LZ4Codec.Encode(serializedData, bytes.AsSpan(offset, bytes.Length - offset));
|
||||
|
||||
// write extension header(always 6 bytes)
|
||||
extHeaderOffset += MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref bytes, extHeaderOffset, ExtensionTypeCode, lz4Length + 5);
|
||||
|
@ -124,7 +135,7 @@ namespace MessagePack
|
|||
offset += (6 + 5);
|
||||
|
||||
// write body
|
||||
var lz4Length = LZ4Codec.Encode(serializedData.Array, serializedData.Offset, serializedData.Count, buffer, offset, buffer.Length - offset);
|
||||
var lz4Length = LZ4Codec.Encode(serializedData, buffer.AsSpan(offset, buffer.Length - offset));
|
||||
|
||||
// write extension header(always 6 bytes)
|
||||
extHeaderOffset += MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref buffer, extHeaderOffset, ExtensionTypeCode, lz4Length + 5);
|
||||
|
@ -136,188 +147,87 @@ namespace MessagePack
|
|||
}
|
||||
}
|
||||
|
||||
public override T Deserialize<T>(Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
public override T Deserialize<T>(ref MessagePackReader reader, IFormatterResolver resolver = null)
|
||||
{
|
||||
if (stream == null)
|
||||
using (var msgPackUncompressed = new Nerdbank.Streams.Sequence<byte>())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
if (!readStrict)
|
||||
{
|
||||
var buffer = MessagePack.Internal.InternalMemoryPool.GetBuffer(); // use MessagePackSerializer.Pool!
|
||||
var len = FillFromStream(stream, ref buffer);
|
||||
return DeserializeCore<T>(new ArraySegment<byte>(buffer, 0, len), resolver, out int readSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
int blockSize;
|
||||
var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out blockSize);
|
||||
return DeserializeCore<T>(new ArraySegment<byte>(bytes, 0, blockSize), resolver, out int readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public override T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver, out int readSize) => DeserializeCore<T>(bytes, resolver, out readSize);
|
||||
|
||||
public static byte[] Decode(Stream stream, bool readStrict = false)
|
||||
{
|
||||
if (!readStrict)
|
||||
{
|
||||
var buffer = MessagePack.Internal.InternalMemoryPool.GetBuffer(); // use MessagePackSerializer.Pool!
|
||||
var len = FillFromStream(stream, ref buffer);
|
||||
return Decode(new ArraySegment<byte>(buffer, 0, len));
|
||||
}
|
||||
else
|
||||
{
|
||||
int blockSize;
|
||||
var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out blockSize);
|
||||
return Decode(new ArraySegment<byte>(bytes, 0, blockSize));
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Decode(byte[] bytes)
|
||||
{
|
||||
return Decode(new ArraySegment<byte>(bytes, 0, bytes.Length));
|
||||
}
|
||||
|
||||
public static byte[] Decode(ArraySegment<byte> bytes)
|
||||
{
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes.Array, bytes.Offset) == MessagePackType.Extension)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes.Array, bytes.Offset, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
if (TryDecompress(ref reader, msgPackUncompressed))
|
||||
{
|
||||
// decode lz4
|
||||
var offset = bytes.Offset + readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes.Array, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = new byte[length]; // use new buffer.
|
||||
|
||||
// LZ4 Decode
|
||||
var len = bytes.Count + bytes.Offset - offset;
|
||||
LZ4Codec.Decode(bytes.Array, offset, len, buffer, 0, length);
|
||||
|
||||
return buffer;
|
||||
var uncompressedReader = reader.Clone(msgPackUncompressed.AsReadOnlySequence);
|
||||
return base.Deserialize<T>(ref uncompressedReader, resolver);
|
||||
}
|
||||
else
|
||||
{
|
||||
return base.Deserialize<T>(ref reader, resolver);
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes.Offset == 0 && bytes.Array.Length == bytes.Count)
|
||||
{
|
||||
// return same reference
|
||||
return bytes.Array;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = new byte[bytes.Count];
|
||||
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private delegate int LZ4Transform(ReadOnlySpan<byte> input, Span<byte> output);
|
||||
|
||||
/// <summary>
|
||||
/// Get the war memory pool byte[]. The result can not share across thread and can not hold and can not call LZ4Deserialize before use it.
|
||||
/// Performs LZ4 compression or decompression.
|
||||
/// </summary>
|
||||
public static byte[] DecodeUnsafe(byte[] bytes)
|
||||
/// <param name="input">The input for the operation.</param>
|
||||
/// <param name="output">The buffer to write the result of the operation.</param>
|
||||
/// <param name="lz4Operation">The LZ4 codec transformation.</param>
|
||||
/// <returns>The number of bytes written to the <paramref name="output"/>.</returns>
|
||||
private static int LZ4Operation(ReadOnlySequence<byte> input, Span<byte> output, LZ4Transform lz4Operation)
|
||||
{
|
||||
return DecodeUnsafe(new ArraySegment<byte>(bytes, 0, bytes.Length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the war memory pool byte[]. The result can not share across thread and can not hold and can not call LZ4Deserialize before use it.
|
||||
/// </summary>
|
||||
public static byte[] DecodeUnsafe(ArraySegment<byte> bytes)
|
||||
{
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes.Array, bytes.Offset) == MessagePackType.Extension)
|
||||
ReadOnlySpan<byte> inputSpan;
|
||||
byte[] rentedInputArray = null;
|
||||
if (input.IsSingleSegment)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes.Array, bytes.Offset, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
{
|
||||
// decode lz4
|
||||
var offset = bytes.Offset + readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes.Array, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = LZ4MemoryPool.GetBuffer(); // use LZ4 Pool(Unsafe)
|
||||
if (buffer.Length < length)
|
||||
{
|
||||
buffer = new byte[length];
|
||||
}
|
||||
|
||||
// LZ4 Decode
|
||||
var len = bytes.Count + bytes.Offset - offset;
|
||||
LZ4Codec.Decode(bytes.Array, offset, len, buffer, 0, length);
|
||||
|
||||
return buffer; // return pooled bytes.
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes.Offset == 0 && bytes.Array.Length == bytes.Count)
|
||||
{
|
||||
// return same reference
|
||||
return bytes.Array;
|
||||
inputSpan = input.First.Span;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = new byte[bytes.Count];
|
||||
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
|
||||
return result;
|
||||
rentedInputArray = ArrayPool<byte>.Shared.Rent((int)input.Length);
|
||||
input.CopyTo(rentedInputArray);
|
||||
inputSpan = rentedInputArray.AsSpan(0, (int)input.Length);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return lz4Operation(inputSpan, output);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (rentedInputArray != null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(rentedInputArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private T DeserializeCore<T>(ArraySegment<byte> bytes, IFormatterResolver resolver, out int readSize)
|
||||
private static bool TryDecompress(ref MessagePackReader reader, IBufferWriter<byte> writer)
|
||||
{
|
||||
if (resolver == null)
|
||||
if (!reader.End && reader.NextMessagePackType == MessagePackType.Extension)
|
||||
{
|
||||
resolver = DefaultResolver;
|
||||
}
|
||||
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
if (MessagePackBinary.GetMessagePackType(bytes.Array, bytes.Offset) == MessagePackType.Extension)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes.Array, bytes.Offset, out readSize);
|
||||
var peekReader = reader.CreatePeekReader();
|
||||
var header = peekReader.ReadExtensionFormatHeader();
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
{
|
||||
// decode lz4
|
||||
var offset = bytes.Offset + readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes.Array, offset, out readSize);
|
||||
offset += readSize;
|
||||
// Read the extension using the original reader, so we "consume" it.
|
||||
var extension = reader.ReadExtensionFormat();
|
||||
var extReader = new MessagePackReader(extension.Data);
|
||||
|
||||
var buffer = LZ4MemoryPool.GetBuffer(); // use LZ4 Pool
|
||||
if (buffer.Length < length)
|
||||
{
|
||||
buffer = new byte[length];
|
||||
}
|
||||
// The first part of the extension payload is a MessagePack-encoded Int32 that
|
||||
// tells us the length the data will be AFTER decompression.
|
||||
int uncompressedLength = extReader.ReadInt32();
|
||||
|
||||
// LZ4 Decode
|
||||
var len = bytes.Count + bytes.Offset - offset;
|
||||
LZ4Codec.Decode(bytes.Array, offset, len, buffer, 0, length);
|
||||
// The rest of the payload is the compressed data itself.
|
||||
var compressedData = extReader.Sequence.Slice(extReader.Position);
|
||||
|
||||
return formatter.Deserialize(buffer, 0, resolver, out readSize);
|
||||
var uncompressedSpan = writer.GetSpan(uncompressedLength).Slice(0, uncompressedLength);
|
||||
int actualUncompressedLength = LZ4Operation(compressedData, uncompressedSpan, LZ4Codec.Decode);
|
||||
Debug.Assert(actualUncompressedLength == uncompressedLength);
|
||||
writer.Advance(actualUncompressedLength);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return formatter.Deserialize(bytes.Array, bytes.Offset, resolver, out readSize);
|
||||
}
|
||||
|
||||
private static int FillFromStream(Stream input, ref byte[] buffer)
|
||||
{
|
||||
int length = 0;
|
||||
int read;
|
||||
while ((read = input.Read(buffer, length, buffer.Length - length)) > 0)
|
||||
{
|
||||
length += read;
|
||||
if (length == buffer.Length)
|
||||
{
|
||||
MessagePackBinary.FastResize(ref buffer, length * 2);
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -338,4 +248,4 @@ namespace MessagePack.Internal
|
|||
return lz4buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard1.6;netstandard2.0;net45;net47</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard1.6;netstandard2.0;netcoreapp2.1;net46;net47</TargetFrameworks>
|
||||
<NoWarn>$(NoWarn);CS0649</NoWarn>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
<DefineConstants>$(DefineConstants);ENABLE_UNSAFE_MSGPACK</DefineConstants>
|
||||
<DefineConstants Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">$(DefineConstants);SPAN_BUILTIN</DefineConstants>
|
||||
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
|
||||
|
||||
<IsPackable>true</IsPackable>
|
||||
|
@ -26,8 +27,10 @@
|
|||
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
|
||||
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
|
||||
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
|
||||
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
|
||||
<PackageReference Include="Nerdbank.Streams" Version="2.0.214" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -84,4 +87,8 @@
|
|||
<AutoGen>True</AutoGen>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -179,6 +179,25 @@ namespace MessagePack
|
|||
{
|
||||
return formatNameTable[code];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given messagepack code represents an integer that might include a sign (i.e. might be a negative number).
|
||||
/// </summary>
|
||||
/// <param name="code">The messagepack code.</param>
|
||||
/// <returns>A boolean value.</returns>
|
||||
public static bool IsSignedInteger(byte code)
|
||||
{
|
||||
switch (code)
|
||||
{
|
||||
case Int8:
|
||||
case Int16:
|
||||
case Int32:
|
||||
case Int64:
|
||||
return true;
|
||||
default:
|
||||
return code >= MinNegativeFixInt && code <= MaxNegativeFixInt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReservedMessagePackExtensionTypeCode
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -18,8 +18,6 @@ namespace MessagePack
|
|||
{
|
||||
private delegate int RawFormatterSerialize(MessagePackSerializer serializer, ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver);
|
||||
|
||||
private delegate object RawFormatterDeserialize(MessagePackSerializer serializer, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
|
||||
private static readonly Func<Type, CompiledMethods> CreateCompiledMethods;
|
||||
private static readonly MessagePack.Internal.ThreadsafeTypeKeyHashTable<CompiledMethods> serializes = new MessagePack.Internal.ThreadsafeTypeKeyHashTable<CompiledMethods>(capacity: 64);
|
||||
|
||||
|
@ -70,51 +68,21 @@ namespace MessagePack
|
|||
return GetOrAdd(type).serialize5.Invoke(this.serializer, ref bytes, offset, value, resolver);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, byte[] bytes)
|
||||
public object Deserialize(Type type, byte[] bytes, int offset = 0, IFormatterResolver resolver = null)
|
||||
{
|
||||
return GetOrAdd(type).deserialize1.Invoke(this.serializer, bytes);
|
||||
return GetOrAdd(type).deserialize2.Invoke(this.serializer, bytes, offset, resolver);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, byte[] bytes, IFormatterResolver resolver)
|
||||
{
|
||||
return GetOrAdd(type).deserialize2.Invoke(this.serializer, bytes, resolver);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, Stream stream)
|
||||
{
|
||||
return GetOrAdd(type).deserialize3.Invoke(this.serializer, stream);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, Stream stream, IFormatterResolver resolver)
|
||||
public object Deserialize(Type type, Stream stream, IFormatterResolver resolver = null)
|
||||
{
|
||||
return GetOrAdd(type).deserialize4.Invoke(this.serializer, stream, resolver);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, Stream stream, bool readStrict)
|
||||
{
|
||||
return GetOrAdd(type).deserialize5.Invoke(this.serializer, stream, readStrict);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
{
|
||||
return GetOrAdd(type).deserialize6.Invoke(this.serializer, stream, resolver, readStrict);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, ArraySegment<byte> bytes)
|
||||
{
|
||||
return GetOrAdd(type).deserialize7.Invoke(this.serializer, bytes);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, ArraySegment<byte> bytes, IFormatterResolver resolver)
|
||||
public object Deserialize(Type type, Memory<byte> bytes, IFormatterResolver resolver = null)
|
||||
{
|
||||
return GetOrAdd(type).deserialize8.Invoke(this.serializer, bytes, resolver);
|
||||
}
|
||||
|
||||
public object Deserialize(Type type, byte[] bytes, int offset, IFormatterResolver resolver, out int readSize)
|
||||
{
|
||||
return GetOrAdd(type).deserialize9.Invoke(this.serializer, bytes, offset, resolver, out readSize);
|
||||
}
|
||||
|
||||
private static CompiledMethods GetOrAdd(Type type)
|
||||
{
|
||||
return serializes.GetOrAdd(type, CreateCompiledMethods);
|
||||
|
@ -128,16 +96,10 @@ namespace MessagePack
|
|||
public readonly Action<MessagePackSerializer, Stream, object, IFormatterResolver> serialize4;
|
||||
public readonly RawFormatterSerialize serialize5;
|
||||
|
||||
public readonly Func<MessagePackSerializer, byte[], object> deserialize1;
|
||||
public readonly Func<MessagePackSerializer, byte[], IFormatterResolver, object> deserialize2;
|
||||
public readonly Func<MessagePackSerializer, Stream, object> deserialize3;
|
||||
public readonly Func<MessagePackSerializer, byte[], int, IFormatterResolver, object> deserialize2;
|
||||
public readonly Func<MessagePackSerializer, Stream, IFormatterResolver, object> deserialize4;
|
||||
public readonly Func<MessagePackSerializer, Stream, bool, object> deserialize5;
|
||||
public readonly Func<MessagePackSerializer, Stream, IFormatterResolver, bool, object> deserialize6;
|
||||
|
||||
public readonly Func<MessagePackSerializer, ArraySegment<byte>, object> deserialize7;
|
||||
public readonly Func<MessagePackSerializer, ArraySegment<byte>, IFormatterResolver, object> deserialize8;
|
||||
public readonly RawFormatterDeserialize deserialize9;
|
||||
public readonly Func<MessagePackSerializer, Memory<byte>, IFormatterResolver, object> deserialize8;
|
||||
|
||||
public CompiledMethods(Type type)
|
||||
{
|
||||
|
@ -228,36 +190,17 @@ namespace MessagePack
|
|||
this.serialize5 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(byte[] bytes)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(byte[]) });
|
||||
// public static T Deserialize<T>(byte[] bytes, int offset, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, byte[], object>>(body, param0, param1).Compile();
|
||||
|
||||
this.deserialize1 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(byte[] bytes, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(byte[]), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, byte[], IFormatterResolver, object>>(body, param0, param1, param2).Compile();
|
||||
var param2 = Expression.Parameter(typeof(int), "offset");
|
||||
var param3 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1, param2, param3), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, byte[], int, IFormatterResolver, object>>(body, param0, param1, param2, param3).Compile();
|
||||
|
||||
this.deserialize2 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, Stream, object>>(body, param0, param1).Compile();
|
||||
|
||||
this.deserialize3 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream), typeof(IFormatterResolver) });
|
||||
|
@ -270,63 +213,16 @@ namespace MessagePack
|
|||
this.deserialize4 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream, bool readStrict)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream), typeof(bool) });
|
||||
// public static T Deserialize<T>(Memory<byte> bytes, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Memory<byte>), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(bool), "readStrict");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, Stream, bool, object>>(body, param0, param1, param2).Compile();
|
||||
|
||||
this.deserialize5 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream), typeof(IFormatterResolver), typeof(bool) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var param3 = Expression.Parameter(typeof(bool), "readStrict");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1, param2, param3), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, Stream, IFormatterResolver, bool, object>>(body, param0, param1, param2, param3).Compile();
|
||||
|
||||
this.deserialize6 = lambda;
|
||||
}
|
||||
|
||||
{
|
||||
// public static T Deserialize<T>(ArraySegment<byte> bytes)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(ArraySegment<byte>) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(ArraySegment<byte>), "bytes");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, ArraySegment<byte>, object>>(body, param0, param1).Compile();
|
||||
|
||||
this.deserialize7 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(ArraySegment<byte>), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(ArraySegment<byte>), "bytes");
|
||||
var param1 = Expression.Parameter(typeof(Memory<byte>), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, ArraySegment<byte>, IFormatterResolver, object>>(body, param0, param1, param2).Compile();
|
||||
var lambda = Expression.Lambda<Func<MessagePackSerializer, Memory<byte>, IFormatterResolver, object>>(body, param0, param1, param2).Compile();
|
||||
|
||||
this.deserialize8 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(byte[] bytes, int offset, IFormatterResolver resolver, out int readSize)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(int), "offset");
|
||||
var param3 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var param4 = Expression.Parameter(typeof(int).MakeByRefType(), "readSize");
|
||||
var body = Expression.Convert(Expression.Call(param0, deserialize, param1, param2, param3, param4), typeof(object));
|
||||
var lambda = Expression.Lambda<RawFormatterDeserialize>(body, param0, param1, param2, param3, param4).Compile();
|
||||
|
||||
this.deserialize9 = lambda;
|
||||
}
|
||||
}
|
||||
|
||||
// null is generic type marker.
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
|
@ -34,11 +36,9 @@ namespace MessagePack
|
|||
|
||||
public object Deserialize(Stream stream) => serializer.Deserialize<object>(stream);
|
||||
|
||||
public object Deserialize(Stream stream, bool readStrict) => serializer.Deserialize<object>(stream, readStrict);
|
||||
|
||||
public object Deserialize(ArraySegment<byte> bytes) => serializer.Deserialize<object>(bytes);
|
||||
|
||||
public System.Threading.Tasks.Task<object> DeserializeAsync(Stream stream) => serializer.DeserializeAsync<object>(stream);
|
||||
public ValueTask<object> DeserializeAsync(Stream stream, CancellationToken cancellationToken = default) => serializer.DeserializeAsync<object>(stream, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
|
@ -27,15 +28,32 @@ namespace MessagePack
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dump message-pack binary to JSON string.
|
||||
/// Convert a message-pack binary to a JSON string.
|
||||
/// </summary>
|
||||
public virtual string ConvertToJson(byte[] bytes)
|
||||
{
|
||||
if (bytes == null || bytes.Length == 0) return "";
|
||||
public string ConvertToJson(ReadOnlyMemory<byte> bytes) => this.ConvertToJson(new ReadOnlySequence<byte>(bytes));
|
||||
|
||||
var sb = new StringBuilder();
|
||||
ToJsonCore(bytes, 0, sb);
|
||||
return sb.ToString();
|
||||
/// <summary>
|
||||
/// Convert a message-pack binary to a JSON string.
|
||||
/// </summary>
|
||||
public string ConvertToJson(ReadOnlySequence<byte> bytes)
|
||||
{
|
||||
var jsonWriter = new StringWriter();
|
||||
var reader = new MessagePackReader(bytes);
|
||||
this.ConvertToJson(ref reader, jsonWriter);
|
||||
return jsonWriter.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a message-pack binary to a JSON string.
|
||||
/// </summary>
|
||||
public virtual void ConvertToJson(ref MessagePackReader reader, TextWriter jsonWriter)
|
||||
{
|
||||
if (reader.End)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ToJsonCore(ref reader, jsonWriter);
|
||||
}
|
||||
|
||||
public byte[] ConvertFromJson(string str)
|
||||
|
@ -150,178 +168,174 @@ namespace MessagePack
|
|||
return count;
|
||||
}
|
||||
|
||||
static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder)
|
||||
private static void ToJsonCore(ref MessagePackReader reader, TextWriter writer)
|
||||
{
|
||||
var readSize = 0;
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
var type = reader.NextMessagePackType;
|
||||
switch (type)
|
||||
{
|
||||
case MessagePackType.Integer:
|
||||
var code = bytes[offset];
|
||||
if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.Int8) builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.Int16) builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.Int32) builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.Int64) builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.UInt8) builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.UInt16) builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.UInt32) builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
else if (code == MessagePackCode.UInt64) builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
break;
|
||||
case MessagePackType.Boolean:
|
||||
builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false");
|
||||
break;
|
||||
case MessagePackType.Float:
|
||||
var floatCode = bytes[offset];
|
||||
if (floatCode == MessagePackCode.Float32)
|
||||
if (MessagePackCode.IsSignedInteger(reader.NextCode))
|
||||
{
|
||||
builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
writer.Write(reader.ReadInt64().ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
writer.Write(reader.ReadUInt64().ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
break;
|
||||
case MessagePackType.Boolean:
|
||||
writer.Write(reader.ReadBoolean() ? "true" : "false");
|
||||
break;
|
||||
case MessagePackType.Float:
|
||||
if (reader.NextCode == MessagePackCode.Float32)
|
||||
{
|
||||
writer.Write(reader.ReadSingle().ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write(reader.ReadDouble().ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
break;
|
||||
case MessagePackType.String:
|
||||
WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder);
|
||||
WriteJsonString(reader.ReadString(), writer);
|
||||
break;
|
||||
case MessagePackType.Binary:
|
||||
builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\"");
|
||||
writer.Write("\"" + Convert.ToBase64String(reader.ReadBytes().ToArray()) + "\"");
|
||||
break;
|
||||
case MessagePackType.Array:
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize);
|
||||
var totalReadSize = readSize;
|
||||
offset += readSize;
|
||||
builder.Append("[");
|
||||
int length = reader.ReadArrayHeader();
|
||||
writer.Write("[");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
ToJsonCore(ref reader, writer);
|
||||
|
||||
if (i != length - 1)
|
||||
{
|
||||
builder.Append(",");
|
||||
writer.Write(",");
|
||||
}
|
||||
}
|
||||
builder.Append("]");
|
||||
|
||||
return totalReadSize;
|
||||
writer.Write("]");
|
||||
return;
|
||||
}
|
||||
case MessagePackType.Map:
|
||||
{
|
||||
var length = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize);
|
||||
var totalReadSize = readSize;
|
||||
offset += readSize;
|
||||
builder.Append("{");
|
||||
int length = reader.ReadMapHeader();
|
||||
writer.Write("{");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
// write key
|
||||
{
|
||||
var keyType = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
var keyType = reader.NextMessagePackType;
|
||||
if (keyType == MessagePackType.String || keyType == MessagePackType.Binary)
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
ToJsonCore(ref reader, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("\"");
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
builder.Append("\"");
|
||||
writer.Write("\"");
|
||||
ToJsonCore(ref reader, writer);
|
||||
writer.Write("\"");
|
||||
}
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
}
|
||||
|
||||
builder.Append(":");
|
||||
writer.Write(":");
|
||||
|
||||
// write body
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
ToJsonCore(ref reader, writer);
|
||||
}
|
||||
|
||||
if (i != length - 1)
|
||||
{
|
||||
builder.Append(",");
|
||||
writer.Write(",");
|
||||
}
|
||||
}
|
||||
builder.Append("}");
|
||||
writer.Write("}");
|
||||
|
||||
return totalReadSize;
|
||||
return;
|
||||
}
|
||||
case MessagePackType.Extension:
|
||||
var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
var extHeader = reader.ReadExtensionFormatHeader();
|
||||
if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
|
||||
{
|
||||
var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
builder.Append("\"");
|
||||
builder.Append(dt.ToString("o", CultureInfo.InvariantCulture));
|
||||
builder.Append("\"");
|
||||
var dt = reader.ReadDateTime(extHeader);
|
||||
writer.Write("\"");
|
||||
writer.Write(dt.ToString("o", CultureInfo.InvariantCulture));
|
||||
writer.Write("\"");
|
||||
}
|
||||
#if !UNITY
|
||||
else if (extHeader.TypeCode == TypelessFormatter.ExtensionTypeCode)
|
||||
{
|
||||
int startOffset = offset;
|
||||
// prepare type name token
|
||||
offset += 6;
|
||||
var typeNameToken = new StringBuilder();
|
||||
var typeNameReadSize = ToJsonCore(bytes, offset, typeNameToken);
|
||||
offset += typeNameReadSize;
|
||||
int startBuilderLength = builder.Length;
|
||||
var privateBuilder = new StringBuilder();
|
||||
var typeNameTokenBuilder = new StringBuilder();
|
||||
var positionBeforeTypeNameRead = reader.Position;
|
||||
ToJsonCore(ref reader, new StringWriter(typeNameTokenBuilder));
|
||||
int typeNameReadSize = (int)reader.Sequence.Slice(positionBeforeTypeNameRead, reader.Position).Length;
|
||||
if (extHeader.Length > typeNameReadSize)
|
||||
{
|
||||
// object map or array
|
||||
var typeInside = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
var typeInside = reader.NextMessagePackType;
|
||||
if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
|
||||
builder.Append("{");
|
||||
offset += ToJsonCore(bytes, offset, builder);
|
||||
{
|
||||
privateBuilder.Append("{");
|
||||
}
|
||||
|
||||
ToJsonCore(ref reader, new StringWriter(privateBuilder));
|
||||
// insert type name token to start of object map or array
|
||||
if (typeInside != MessagePackType.Array)
|
||||
typeNameToken.Insert(0, "\"$type\":");
|
||||
{
|
||||
typeNameTokenBuilder.Insert(0, "\"$type\":");
|
||||
}
|
||||
|
||||
if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
|
||||
builder.Append("}");
|
||||
if (builder.Length - startBuilderLength > 2)
|
||||
typeNameToken.Append(",");
|
||||
builder.Insert(startBuilderLength + 1, typeNameToken.ToString());
|
||||
{
|
||||
privateBuilder.Append("}");
|
||||
}
|
||||
|
||||
if (privateBuilder.Length > 2)
|
||||
{
|
||||
typeNameTokenBuilder.Append(",");
|
||||
}
|
||||
|
||||
privateBuilder.Insert(1, typeNameTokenBuilder.ToString());
|
||||
|
||||
writer.Write(privateBuilder.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("{\"$type\":\"" + typeNameToken.ToString() + "}");
|
||||
writer.Write("{\"$type\":\"" + typeNameTokenBuilder.ToString() + "}");
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize);
|
||||
builder.Append("[");
|
||||
builder.Append(ext.TypeCode);
|
||||
builder.Append(",");
|
||||
builder.Append("\"");
|
||||
builder.Append(Convert.ToBase64String(ext.Data));
|
||||
builder.Append("\"");
|
||||
builder.Append("]");
|
||||
var ext = reader.ReadExtensionFormat();
|
||||
writer.Write("[");
|
||||
writer.Write(ext.TypeCode);
|
||||
writer.Write(",");
|
||||
writer.Write("\"");
|
||||
writer.Write(Convert.ToBase64String(ext.Data.ToArray()));
|
||||
writer.Write("\"");
|
||||
writer.Write("]");
|
||||
}
|
||||
break;
|
||||
case MessagePackType.Unknown:
|
||||
case MessagePackType.Nil:
|
||||
default:
|
||||
readSize = 1;
|
||||
builder.Append("null");
|
||||
reader.Skip();
|
||||
writer.Write("null");
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException($"code is invalid. code: {reader.NextCode} format: {MessagePackCode.ToFormatName(reader.NextCode)}");
|
||||
}
|
||||
|
||||
return readSize;
|
||||
}
|
||||
|
||||
// escape string
|
||||
static void WriteJsonString(string value, StringBuilder builder)
|
||||
private static void WriteJsonString(string value, TextWriter builder)
|
||||
{
|
||||
builder.Append('\"');
|
||||
builder.Write('\"');
|
||||
|
||||
var len = value.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
|
@ -330,33 +344,33 @@ namespace MessagePack
|
|||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
builder.Append("\\\"");
|
||||
builder.Write("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
builder.Append("\\\\");
|
||||
builder.Write("\\\\");
|
||||
break;
|
||||
case '\b':
|
||||
builder.Append("\\b");
|
||||
builder.Write("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
builder.Append("\\f");
|
||||
builder.Write("\\f");
|
||||
break;
|
||||
case '\n':
|
||||
builder.Append("\\n");
|
||||
builder.Write("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
builder.Append("\\r");
|
||||
builder.Write("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
builder.Append("\\t");
|
||||
builder.Write("\\t");
|
||||
break;
|
||||
default:
|
||||
builder.Append(c);
|
||||
builder.Write(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
builder.Append('\"');
|
||||
builder.Write('\"');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,11 @@
|
|||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using Nerdbank.Streams;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
|
@ -128,7 +133,7 @@ namespace MessagePack
|
|||
if (resolver == null) resolver = DefaultResolver;
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var rentBuffer = BufferPool.Default.Rent();
|
||||
var rentBuffer = ArrayPool<byte>.Shared.Rent(65535);
|
||||
try
|
||||
{
|
||||
var buffer = rentBuffer;
|
||||
|
@ -139,126 +144,138 @@ namespace MessagePack
|
|||
}
|
||||
finally
|
||||
{
|
||||
BufferPool.Default.Return(rentBuffer);
|
||||
ArrayPool<byte>.Shared.Return(rentBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
public T Deserialize<T>(byte[] bytes) => Deserialize<T>(bytes, DefaultResolver);
|
||||
|
||||
public T Deserialize<T>(byte[] bytes, IFormatterResolver resolver) => Deserialize<T>(new ArraySegment<byte>(bytes), resolver);
|
||||
|
||||
public T Deserialize<T>(ArraySegment<byte> bytes) => Deserialize<T>(bytes, DefaultResolver);
|
||||
|
||||
public T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver) => Deserialize<T>(bytes, resolver, out int readSize);
|
||||
|
||||
public virtual T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver, out int readSize)
|
||||
/// <summary>
|
||||
/// Deserializes a value of a given type from a sequence of bytes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="byteSequence">The sequence to deserialize from.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public T Deserialize<T>(ReadOnlySequence<byte> byteSequence, IFormatterResolver resolver = null)
|
||||
{
|
||||
if (resolver == null) resolver = DefaultResolver;
|
||||
return resolver.GetFormatterWithVerify<T>().Deserialize(bytes.Array, bytes.Offset, resolver, out readSize);
|
||||
var reader = new MessagePackReader(byteSequence);
|
||||
return this.Deserialize<T>(ref reader, resolver);
|
||||
}
|
||||
|
||||
public T Deserialize<T>(Stream stream) => Deserialize<T>(stream, DefaultResolver, readStrict: false);
|
||||
|
||||
public T Deserialize<T>(Stream stream, IFormatterResolver resolver) => Deserialize<T>(stream, resolver, readStrict: false);
|
||||
|
||||
public T Deserialize<T>(Stream stream, bool readStrict) => Deserialize<T>(stream, DefaultResolver, readStrict);
|
||||
|
||||
public virtual T Deserialize<T>(Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
/// <summary>
|
||||
/// Deserializes a value of a given type from a sequence of bytes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="reader">The reader to deserialize from.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public virtual T Deserialize<T>(ref MessagePackReader reader, IFormatterResolver resolver = null)
|
||||
{
|
||||
if (resolver == null) resolver = DefaultResolver;
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
resolver = resolver ?? this.DefaultResolver;
|
||||
return resolver.GetFormatterWithVerify<T>().Deserialize(ref reader, resolver);
|
||||
}
|
||||
|
||||
if (!readStrict)
|
||||
/// <summary>
|
||||
/// Deserializes a value of a given type from a sequence of bytes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="buffer">The buffer to deserialize from.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public T Deserialize<T>(Memory<byte> buffer, IFormatterResolver resolver = null)
|
||||
{
|
||||
var reader = new MessagePackReader(new ReadOnlySequence<byte>(buffer));
|
||||
return this.Deserialize<T>(ref reader, resolver);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a value of a given type from a sequence of bytes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="buffer">The array to deserialize from.</param>
|
||||
/// <param name="offset">The position in the array to start deserialization.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public T Deserialize<T>(byte[] buffer, int offset = 0, IFormatterResolver resolver = null)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
#if NETSTANDARD && !NET45
|
||||
|
||||
var ms = stream as MemoryStream;
|
||||
if (ms != null)
|
||||
{
|
||||
// optimize for MemoryStream
|
||||
ArraySegment<byte> buffer;
|
||||
if (ms.TryGetBuffer(out buffer))
|
||||
{
|
||||
int readSize;
|
||||
return formatter.Deserialize(buffer.Array, buffer.Offset, resolver, out readSize);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// no else.
|
||||
{
|
||||
var buffer = InternalMemoryPool.GetBuffer();
|
||||
|
||||
FillFromStream(stream, ref buffer);
|
||||
|
||||
int readSize;
|
||||
return formatter.Deserialize(buffer, 0, resolver, out readSize);
|
||||
}
|
||||
throw new ArgumentNullException(nameof(buffer));
|
||||
}
|
||||
else
|
||||
|
||||
var reader = new MessagePackReader(new ReadOnlySequence<byte>(buffer.AsMemory(offset)));
|
||||
return Deserialize<T>(ref reader, resolver);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a value of a given type from a sequence of bytes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="buffer">The array to deserialize from.</param>
|
||||
/// <param name="offset">The position in the array to start deserialization.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <param name="bytesRead">The number of bytes read.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public T Deserialize<T>(byte[] buffer, int offset, IFormatterResolver resolver, out int bytesRead)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
int _;
|
||||
var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out _);
|
||||
int readSize;
|
||||
return formatter.Deserialize(bytes, 0, resolver, out readSize);
|
||||
throw new ArgumentNullException(nameof(buffer));
|
||||
}
|
||||
|
||||
var sequence = new ReadOnlySequence<byte>(buffer.AsMemory(offset));
|
||||
var reader = new MessagePackReader(sequence);
|
||||
T result = Deserialize<T>(ref reader, resolver);
|
||||
bytesRead = (int)sequence.Slice(0, reader.Position).Length;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the entire content of a <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="stream">The stream to deserialize from.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public T Deserialize<T>(Stream stream, IFormatterResolver resolver = null)
|
||||
{
|
||||
using (var sequence = new Sequence<byte>())
|
||||
{
|
||||
int bytesRead;
|
||||
do
|
||||
{
|
||||
var span = sequence.GetSpan(stream.CanSeek ? (int)(stream.Length - stream.Position) : 0);
|
||||
bytesRead = stream.Read(span);
|
||||
sequence.Advance(bytesRead);
|
||||
} while (bytesRead > 0);
|
||||
|
||||
return this.Deserialize<T>(sequence.AsReadOnlySequence, resolver);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reflect of resolver.GetFormatterWithVerify[T].Deserialize.
|
||||
/// Deserializes the entire content of a <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
public T Deserialize<T>(byte[] bytes, int offset, IFormatterResolver resolver, out int readSize) => Deserialize<T>(new ArraySegment<byte>(bytes, offset, bytes.Length - offset), resolver, out readSize);
|
||||
|
||||
#if !UNITY
|
||||
|
||||
public System.Threading.Tasks.Task<T> DeserializeAsync<T>(Stream stream) => DeserializeAsync<T>(stream, DefaultResolver);
|
||||
|
||||
// readStrict async read is too slow(many Task garbage) so I don't provide async option.
|
||||
|
||||
public async System.Threading.Tasks.Task<T> DeserializeAsync<T>(Stream stream, IFormatterResolver resolver)
|
||||
/// <typeparam name="T">The type of value to deserialize.</typeparam>
|
||||
/// <param name="stream">The stream to deserialize from.</param>
|
||||
/// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param>
|
||||
/// <param name="cancellationToken">A cancellation token.</param>
|
||||
/// <returns>The deserialized value.</returns>
|
||||
public async ValueTask<T> DeserializeAsync<T>(Stream stream, IFormatterResolver resolver = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (resolver == null) resolver = DefaultResolver;
|
||||
var rentBuffer = BufferPool.Default.Rent();
|
||||
var buf = rentBuffer;
|
||||
try
|
||||
using (var sequence = new Sequence<byte>())
|
||||
{
|
||||
int length = 0;
|
||||
int read;
|
||||
while ((read = await stream.ReadAsync(buf, length, buf.Length - length).ConfigureAwait(false)) > 0)
|
||||
int bytesRead;
|
||||
do
|
||||
{
|
||||
length += read;
|
||||
if (length == buf.Length)
|
||||
{
|
||||
MessagePackBinary.FastResize(ref buf, length * 2);
|
||||
}
|
||||
}
|
||||
var memory = sequence.GetMemory(stream.CanSeek ? (int)(stream.Length - stream.Position) : 0);
|
||||
bytesRead = await stream.ReadAsync(memory, cancellationToken).ConfigureAwait(false);
|
||||
sequence.Advance(bytesRead);
|
||||
} while (bytesRead > 0);
|
||||
|
||||
return Deserialize<T>(buf, resolver);
|
||||
return this.Deserialize<T>(sequence.AsReadOnlySequence, resolver);
|
||||
}
|
||||
finally
|
||||
{
|
||||
BufferPool.Default.Return(rentBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static int FillFromStream(Stream input, ref byte[] buffer)
|
||||
{
|
||||
int length = 0;
|
||||
int read;
|
||||
while ((read = input.Read(buffer, length, buffer.Length - length)) > 0)
|
||||
{
|
||||
length += read;
|
||||
if (length == buffer.Length)
|
||||
{
|
||||
MessagePackBinary.FastResize(ref buffer, length * 2);
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
|
@ -44,9 +45,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public Nil Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
|
||||
public Nil Deserialize(ref MessagePackReader reader, IFormatterResolver typeResolver)
|
||||
{
|
||||
return MessagePackBinary.ReadNil(bytes, offset, out readSize);
|
||||
return reader.ReadNil();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,9 +66,9 @@ namespace MessagePack.Formatters
|
|||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public Nil? Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
|
||||
public Nil? Deserialize(ref MessagePackReader reader, IFormatterResolver typeResolver)
|
||||
{
|
||||
return MessagePackBinary.ReadNil(bytes, offset, out readSize);
|
||||
return reader.ReadNil();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -162,7 +162,7 @@ namespace MessagePack.Resolvers
|
|||
// }
|
||||
// }
|
||||
|
||||
// public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
// public T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
// {
|
||||
// return (T)ReflectionObjectFormatterHelper.Deserialize(metaInfo, readMembers, constructorParameterIndexes, mapMemberDictionary, bytes, offset, formatterResolver, out readSize);
|
||||
// }
|
||||
|
|
|
@ -2,11 +2,12 @@
|
|||
#if !NET_STANDARD_2_0
|
||||
|
||||
using System;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System.Buffers;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Threading;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
|
@ -101,17 +102,15 @@ namespace MessagePack.Resolvers
|
|||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
// T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
// T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver);
|
||||
{
|
||||
var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
enumType,
|
||||
new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(IFormatterResolver) });
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Ldarg_2);
|
||||
il.Emit(OpCodes.Ldarg_S, (byte)4);
|
||||
il.Emit(OpCodes.Call, typeof(MessagePackBinary).GetRuntimeMethod("Read" + underlyingType.Name, new[] { typeof(byte[]), typeof(int), typeof(int).MakeByRefType() }));
|
||||
il.Emit(OpCodes.Call, typeof(MessagePackReader).GetRuntimeMethod("Read" + underlyingType.Name, Type.EmptyTypes));
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ using System.Text.RegularExpressions;
|
|||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
|
@ -373,7 +375,9 @@ namespace MessagePack.Internal
|
|||
{
|
||||
var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
type,
|
||||
new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
new Type[] { refMessagePackReader, typeof(IFormatterResolver) });
|
||||
method.DefineParameter(1, ParameterAttributes.None, "reader");
|
||||
method.DefineParameter(2, ParameterAttributes.None, "resolver");
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
BuildDeserialize(type, serializationInfo, il, (index, member) =>
|
||||
|
@ -398,8 +402,8 @@ namespace MessagePack.Internal
|
|||
if (serializationInfo == null) return null;
|
||||
|
||||
// internal delegate int AnonymousSerializeFunc<T>(byte[][] stringByteKeysField, object[] customFormatters, ref byte[] bytes, int offset, T value, IFormatterResolver resolver);
|
||||
// internal delegate T AnonymousDeserializeFunc<T>(object[] customFormatters, byte[] bytes, int offset, IFormatterResolver resolver, out int readSize);
|
||||
var serialize = new DynamicMethod("Serialize", typeof(int), new[] { typeof(byte[][]), typeof(object[]), typeof(byte[]).MakeByRefType(), typeof(int), type, typeof(IFormatterResolver) }, type, true);
|
||||
// internal delegate T AnonymousDeserializeFunc<T>(object[] customFormatters, ref MessagePackReader reader, IFormatterResolver resolver);
|
||||
DynamicMethod deserialize = null;
|
||||
|
||||
List<byte[]> stringByteKeysField = new List<byte[]>();
|
||||
|
@ -464,7 +468,7 @@ namespace MessagePack.Internal
|
|||
|
||||
if (serializationInfo.IsStruct || serializationInfo.BestmatchConstructor != null)
|
||||
{
|
||||
deserialize = new DynamicMethod("Deserialize", type, new[] { typeof(object[]), typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() }, type, true);
|
||||
deserialize = new DynamicMethod("Deserialize", type, new[] { typeof(object[]), refMessagePackReader, typeof(IFormatterResolver) }, type, true);
|
||||
|
||||
var il = deserialize.GetILGenerator();
|
||||
BuildDeserialize(type, serializationInfo, il, (index, member) =>
|
||||
|
@ -507,7 +511,7 @@ namespace MessagePack.Internal
|
|||
il.Emit(OpCodes.Dup);
|
||||
il.EmitLdc_I4(i);
|
||||
il.Emit(OpCodes.Ldstr, item.StringKey);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.GetEncodedStringBytes);
|
||||
il.EmitCall(CodeGenHelpersTypeInfo.GetEncodedStringBytes);
|
||||
il.Emit(OpCodes.Stelem_Ref);
|
||||
i++;
|
||||
}
|
||||
|
@ -775,25 +779,19 @@ namespace MessagePack.Internal
|
|||
}
|
||||
}
|
||||
|
||||
// T Deserialize([arg:1]byte[] bytes, [arg:2]int offset, [arg:3]IFormatterResolver formatterResolver, [arg:4]out int readSize);
|
||||
// T Deserialize([arg:1]ref MessagePackReader reader, [arg:2]IFormatterResolver resolver);
|
||||
static void BuildDeserialize(Type type, ObjectSerializationInfo info, ILGenerator il, Func<int, ObjectSerializationInfo.EmittableMember, Action> tryEmitLoadCustomFormatter, int firstArgIndex)
|
||||
{
|
||||
var argBytes = new ArgumentField(il, firstArgIndex);
|
||||
var argOffset = new ArgumentField(il, firstArgIndex + 1);
|
||||
var argResolver = new ArgumentField(il, firstArgIndex + 2);
|
||||
var argReadSize = new ArgumentField(il, firstArgIndex + 3);
|
||||
var reader = new ArgumentField(il, firstArgIndex, @ref: true);
|
||||
var argResolver = new ArgumentField(il, firstArgIndex + 1);
|
||||
|
||||
// if(MessagePackBinary.IsNil) readSize = 1, return null;
|
||||
// if(reader.TryReadNil()) { return null; }
|
||||
var falseLabel = il.DefineLabel();
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.IsNil);
|
||||
reader.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.TryReadNil);
|
||||
il.Emit(OpCodes.Brfalse_S, falseLabel);
|
||||
if (type.GetTypeInfo().IsClass)
|
||||
{
|
||||
argReadSize.EmitLoad();
|
||||
il.EmitLdc_I4(1);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
il.Emit(OpCodes.Ldnull);
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
@ -804,28 +802,21 @@ namespace MessagePack.Internal
|
|||
il.Emit(OpCodes.Throw);
|
||||
}
|
||||
|
||||
// var startOffset = offset;
|
||||
il.MarkLabel(falseLabel);
|
||||
var startOffsetLocal = il.DeclareLocal(typeof(int)); // [loc:0]
|
||||
argOffset.EmitLoad();
|
||||
il.EmitStloc(startOffsetLocal);
|
||||
|
||||
// var length = ReadMapHeader
|
||||
// var length = ReadMapHeader(ref byteSequence);
|
||||
var length = il.DeclareLocal(typeof(int)); // [loc:1]
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
argReadSize.EmitLoad();
|
||||
reader.EmitLdarg();
|
||||
|
||||
if (info.IsIntKey)
|
||||
{
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadArrayHeader);
|
||||
il.EmitCall(MessagePackReaderTypeInfo.ReadArrayHeader);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadMapHeader);
|
||||
il.EmitCall(MessagePackReaderTypeInfo.ReadMapHeader);
|
||||
}
|
||||
il.EmitStloc(length);
|
||||
EmitOffsetPlusReadSize(il, argOffset, argReadSize);
|
||||
|
||||
// make local fields
|
||||
Label? gotoDefault = null;
|
||||
|
@ -878,7 +869,7 @@ namespace MessagePack.Internal
|
|||
.ToArray();
|
||||
}
|
||||
|
||||
// Read Loop(for var i = 0; i< length; i++)
|
||||
// Read Loop(for var i = 0; i < length; i++)
|
||||
if (info.IsStringKey)
|
||||
{
|
||||
var automata = new AutomataDictionary();
|
||||
|
@ -887,8 +878,7 @@ namespace MessagePack.Internal
|
|||
automata.Add(info.Members[i].StringKey, i);
|
||||
}
|
||||
|
||||
var buffer = il.DeclareLocal(typeof(byte).MakeByRefType(), true);
|
||||
var keySpan = il.DeclareLocal(typeof(ReadOnlySpan<byte>));
|
||||
var buffer = il.DeclareLocal(typeof(ReadOnlySpan<byte>));
|
||||
var longKey = il.DeclareLocal(typeof(ulong));
|
||||
|
||||
// for (int i = 0; i < len; i++)
|
||||
|
@ -897,26 +887,18 @@ namespace MessagePack.Internal
|
|||
var readNext = il.DefineLabel();
|
||||
var loopEnd = il.DefineLabel();
|
||||
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
argReadSize.EmitLoad();
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadStringSegment);
|
||||
il.EmitCall(ReadOnlySpanFromArraySegment);
|
||||
il.EmitStloc(keySpan);
|
||||
EmitOffsetPlusReadSize(il, argOffset, argReadSize);
|
||||
|
||||
// if(rest == 0) goto End
|
||||
il.EmitLdloca(keySpan);
|
||||
il.EmitCall(ReadOnlySpanLength);
|
||||
il.Emit(OpCodes.Brfalse, readNext);
|
||||
reader.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.ReadStringSegment);
|
||||
il.EmitCall(ReadOnlySpanFromReadOnlySequence);
|
||||
il.EmitStloc(buffer);
|
||||
|
||||
// gen automata name lookup
|
||||
automata.EmitMatch(il, keySpan, longKey, x =>
|
||||
automata.EmitMatch(il, buffer, longKey, x =>
|
||||
{
|
||||
var i = x.Value;
|
||||
if (infoList[i].MemberInfo != null)
|
||||
{
|
||||
EmitDeserializeValue(il, infoList[i], i, tryEmitLoadCustomFormatter, argBytes, argOffset, argResolver, argReadSize);
|
||||
EmitDeserializeValue(il, infoList[i], i, tryEmitLoadCustomFormatter, reader, argResolver);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
}
|
||||
else
|
||||
|
@ -929,20 +911,11 @@ namespace MessagePack.Internal
|
|||
});
|
||||
|
||||
il.MarkLabel(readNext);
|
||||
argReadSize.EmitLoad();
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadNextBlock);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
reader.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.Skip);
|
||||
|
||||
il.MarkLabel(loopEnd);
|
||||
EmitOffsetPlusReadSize(il, argOffset, argReadSize);
|
||||
});
|
||||
|
||||
// end fixed
|
||||
il.Emit(OpCodes.Ldc_I4_0);
|
||||
il.Emit(OpCodes.Conv_U);
|
||||
il.EmitStloc(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -962,12 +935,9 @@ namespace MessagePack.Internal
|
|||
il.Emit(OpCodes.Switch, infoList.Select(x => x.SwitchLabel).ToArray());
|
||||
|
||||
il.MarkLabel(switchDefault);
|
||||
// default, only read. readSize = MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
argReadSize.EmitLoad();
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadNextBlock);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
// default, only read. reader.ReadNextBlock();
|
||||
reader.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.Skip);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
|
||||
if (gotoDefault != null)
|
||||
|
@ -982,24 +952,15 @@ namespace MessagePack.Internal
|
|||
if (item.MemberInfo != null)
|
||||
{
|
||||
il.MarkLabel(item.SwitchLabel);
|
||||
EmitDeserializeValue(il, item, i++, tryEmitLoadCustomFormatter, argBytes, argOffset, argResolver, argReadSize);
|
||||
EmitDeserializeValue(il, item, i++, tryEmitLoadCustomFormatter, reader, argResolver);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
}
|
||||
}
|
||||
|
||||
// offset += readSize
|
||||
il.MarkLabel(loopEnd);
|
||||
EmitOffsetPlusReadSize(il, argOffset, argReadSize);
|
||||
});
|
||||
}
|
||||
|
||||
// finish readSize: readSize = offset - startOffset;
|
||||
argReadSize.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
il.EmitLdloc(startOffsetLocal);
|
||||
il.Emit(OpCodes.Sub);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
|
||||
// create result object
|
||||
var structLocal = EmitNewObject(il, type, info, infoList);
|
||||
|
||||
|
@ -1045,16 +1006,7 @@ namespace MessagePack.Internal
|
|||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
static void EmitOffsetPlusReadSize(ILGenerator il, ArgumentField argOffset, ArgumentField argReadSize)
|
||||
{
|
||||
argOffset.EmitLoad();
|
||||
argReadSize.EmitLoad();
|
||||
il.Emit(OpCodes.Ldind_I4);
|
||||
il.Emit(OpCodes.Add);
|
||||
argOffset.EmitStore();
|
||||
}
|
||||
|
||||
static void EmitDeserializeValue(ILGenerator il, DeserializeInfo info, int index, Func<int, ObjectSerializationInfo.EmittableMember, Action> tryEmitLoadCustomFormatter, ArgumentField argBytes, ArgumentField argOffset, ArgumentField argResolver, ArgumentField argReadSize)
|
||||
static void EmitDeserializeValue(ILGenerator il, DeserializeInfo info, int index, Func<int, ObjectSerializationInfo.EmittableMember, Action> tryEmitLoadCustomFormatter, ArgumentField argByteSequence, ArgumentField argResolver)
|
||||
{
|
||||
var member = info.MemberInfo;
|
||||
var t = member.Type;
|
||||
|
@ -1062,34 +1014,28 @@ namespace MessagePack.Internal
|
|||
if (emitter != null)
|
||||
{
|
||||
emitter();
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
argByteSequence.EmitLdarg();
|
||||
argResolver.EmitLoad();
|
||||
argReadSize.EmitLoad();
|
||||
il.EmitCall(typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Deserialize", new[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), refInt }));
|
||||
il.EmitCall(getDeserialize(t));
|
||||
}
|
||||
else if (IsOptimizeTargetType(t))
|
||||
{
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
argByteSequence.EmitLdarg();
|
||||
if (t == typeof(byte[]))
|
||||
{
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadBytes);
|
||||
il.EmitCall(MessagePackReaderTypeInfo.ReadBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.TypeInfo.GetDeclaredMethods("Read" + t.Name).OrderByDescending(x => x.GetParameters().Length).First());
|
||||
il.EmitCall(MessagePackReaderTypeInfo.TypeInfo.GetDeclaredMethods("Read" + t.Name).OrderByDescending(x => x.GetParameters().Length).First());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
argResolver.EmitLoad();
|
||||
il.EmitCall(getFormatterWithVerify.MakeGenericMethod(t));
|
||||
argBytes.EmitLoad();
|
||||
argOffset.EmitLoad();
|
||||
argByteSequence.EmitLdarg();
|
||||
argResolver.EmitLoad();
|
||||
argReadSize.EmitLoad();
|
||||
il.EmitCall(getDeserialize(t));
|
||||
}
|
||||
|
||||
|
@ -1173,21 +1119,22 @@ namespace MessagePack.Internal
|
|||
|
||||
// EmitInfos...
|
||||
|
||||
static readonly Type refMessagePackReader = typeof(MessagePackReader).MakeByRefType();
|
||||
|
||||
static readonly MethodInfo ReadOnlySpanFromByteArray = typeof(ReadOnlySpan<byte>).GetRuntimeMethod("op_Implicit", new[] { typeof(byte[]) });
|
||||
static readonly MethodInfo ReadOnlySpanFromReadOnlySequence = typeof(CodeGenHelpers).GetRuntimeMethod(nameof(CodeGenHelpers.GetSpanFromSequence), new[] { typeof(ReadOnlySequence<byte>) });
|
||||
|
||||
static readonly Type refByte = typeof(byte[]).MakeByRefType();
|
||||
static readonly Type refInt = typeof(int).MakeByRefType();
|
||||
static readonly MethodInfo getFormatterWithVerify = typeof(FormatterResolverExtensions).GetRuntimeMethods().First(x => x.Name == "GetFormatterWithVerify");
|
||||
static readonly MethodInfo getFormatterWithVerify = typeof(FormatterResolverExtensions).GetRuntimeMethods().First(x => x.Name == nameof(FormatterResolverExtensions.GetFormatterWithVerify));
|
||||
static readonly Func<Type, MethodInfo> getSerialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Serialize", new[] { refByte, typeof(int), t, typeof(IFormatterResolver) });
|
||||
static readonly Func<Type, MethodInfo> getDeserialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Deserialize", new[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), refInt });
|
||||
static readonly Func<Type, MethodInfo> getDeserialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod(nameof(IMessagePackFormatter<int>.Deserialize), new[] { refMessagePackReader, typeof(IFormatterResolver) });
|
||||
// static readonly ConstructorInfo dictionaryConstructor = typeof(ByteArrayStringHashTable).GetTypeInfo().DeclaredConstructors.First(x => { var p = x.GetParameters(); return p.Length == 1 && p[0].ParameterType == typeof(int); });
|
||||
// static readonly MethodInfo dictionaryAdd = typeof(ByteArrayStringHashTable).GetRuntimeMethod("Add", new[] { typeof(string), typeof(int) });
|
||||
// static readonly MethodInfo dictionaryTryGetValue = typeof(ByteArrayStringHashTable).GetRuntimeMethod("TryGetValue", new[] { typeof(ArraySegment<byte>), refInt });
|
||||
static readonly ConstructorInfo invalidOperationExceptionConstructor = typeof(System.InvalidOperationException).GetTypeInfo().DeclaredConstructors.First(x => { var p = x.GetParameters(); return p.Length == 1 && p[0].ParameterType == typeof(string); });
|
||||
|
||||
static readonly MethodInfo ReadOnlySpanFromArraySegment = typeof(ReadOnlySpan<byte>).GetRuntimeMethod("op_Implicit", new[] { typeof(ArraySegment<byte>) });
|
||||
static readonly MethodInfo ReadOnlySpanLength = typeof(ReadOnlySpan<byte>).GetRuntimeProperty(nameof(ReadOnlySpan<byte>.Length)).GetMethod;
|
||||
|
||||
static readonly MethodInfo onBeforeSerialize = typeof(IMessagePackSerializationCallbackReceiver).GetRuntimeMethod("OnBeforeSerialize", Type.EmptyTypes);
|
||||
static readonly MethodInfo onAfterDeserialize = typeof(IMessagePackSerializationCallbackReceiver).GetRuntimeMethod("OnAfterDeserialize", Type.EmptyTypes);
|
||||
static readonly MethodInfo onAfterDeserialize = typeof(IMessagePackSerializationCallbackReceiver).GetRuntimeMethod(nameof(IMessagePackSerializationCallbackReceiver.OnAfterDeserialize), Type.EmptyTypes);
|
||||
|
||||
static readonly ConstructorInfo objectCtor = typeof(object).GetTypeInfo().DeclaredConstructors.First(x => x.GetParameters().Length == 0);
|
||||
|
||||
|
@ -1205,22 +1152,26 @@ typeof(int), typeof(int) });
|
|||
public static MethodInfo WriteInt32 = typeof(MessagePackBinary).GetRuntimeMethod("WriteInt32", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteBytes = typeof(MessagePackBinary).GetRuntimeMethod("WriteBytes", new[] { refByte, typeof(int), typeof(byte[]) });
|
||||
public static MethodInfo WriteNil = typeof(MessagePackBinary).GetRuntimeMethod("WriteNil", new[] { refByte, typeof(int) });
|
||||
public static MethodInfo ReadBytes = typeof(MessagePackBinary).GetRuntimeMethod("ReadBytes", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadInt32 = typeof(MessagePackBinary).GetRuntimeMethod("ReadInt32", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadString = typeof(MessagePackBinary).GetRuntimeMethod("ReadString", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadStringSegment = typeof(MessagePackBinary).GetRuntimeMethod("ReadStringSegment", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo IsNil = typeof(MessagePackBinary).GetRuntimeMethod("IsNil", new[] { typeof(byte[]), typeof(int) });
|
||||
public static MethodInfo ReadNextBlock = typeof(MessagePackBinary).GetRuntimeMethod("ReadNextBlock", new[] { typeof(byte[]), typeof(int) });
|
||||
public static MethodInfo WriteStringUnsafe = typeof(MessagePackBinary).GetRuntimeMethod("WriteStringUnsafe", new[] { refByte, typeof(int), typeof(string), typeof(int) });
|
||||
public static MethodInfo WriteStringBytes = typeof(MessagePackBinary).GetRuntimeMethod("WriteStringBytes", new[] { refByte, typeof(int), typeof(byte[]) });
|
||||
public static MethodInfo WriteRaw = typeof(MessagePackBinary).GetRuntimeMethod("WriteRaw", new[] { refByte, typeof(int), typeof(byte[]) });
|
||||
}
|
||||
|
||||
public static MethodInfo ReadArrayHeader = typeof(MessagePackBinary).GetRuntimeMethod("ReadArrayHeader", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadMapHeader = typeof(MessagePackBinary).GetRuntimeMethod("ReadMapHeader", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
internal static class MessagePackReaderTypeInfo
|
||||
{
|
||||
public static TypeInfo TypeInfo = typeof(MessagePackReader).GetTypeInfo();
|
||||
|
||||
static MessagePackBinaryTypeInfo()
|
||||
{
|
||||
}
|
||||
public static MethodInfo ReadArrayHeader = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadArrayHeader), Type.EmptyTypes);
|
||||
public static MethodInfo ReadMapHeader = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadMapHeader), Type.EmptyTypes);
|
||||
public static MethodInfo ReadBytes = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadBytes), Type.EmptyTypes);
|
||||
public static MethodInfo ReadStringSegment = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadStringSegment), Type.EmptyTypes);
|
||||
public static MethodInfo TryReadNil = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.TryReadNil), Type.EmptyTypes);
|
||||
public static MethodInfo Skip = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.Skip), Type.EmptyTypes);
|
||||
}
|
||||
|
||||
internal static class CodeGenHelpersTypeInfo
|
||||
{
|
||||
public static readonly MethodInfo GetEncodedStringBytes = typeof(CodeGenHelpers).GetRuntimeMethod(nameof(CodeGenHelpers.GetEncodedStringBytes), new[] { typeof(string) });
|
||||
}
|
||||
|
||||
internal static class EmitInfo
|
||||
|
@ -1247,7 +1198,7 @@ typeof(int), typeof(int) });
|
|||
}
|
||||
|
||||
internal delegate int AnonymousSerializeFunc<T>(byte[][] stringByteKeysField, object[] customFormatters, ref byte[] bytes, int offset, T value, IFormatterResolver resolver);
|
||||
internal delegate T AnonymousDeserializeFunc<T>(object[] customFormatters, byte[] bytes, int offset, IFormatterResolver resolver, out int readSize);
|
||||
internal delegate T AnonymousDeserializeFunc<T>(object[] customFormatters, ref MessagePackReader reader, IFormatterResolver resolver);
|
||||
|
||||
internal class AnonymousSerializableFormatter<T> : IMessagePackFormatter<T>
|
||||
{
|
||||
|
@ -1272,10 +1223,10 @@ typeof(int), typeof(int) });
|
|||
return serialize(stringByteKeysField, serializeCustomFormatters, ref bytes, offset, value, formatterResolver);
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public T Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
|
||||
{
|
||||
if (deserialize == null) throw new InvalidOperationException(this.GetType().Name + " does not support Deserialize.");
|
||||
return deserialize(deserializeCustomFormatters, bytes, offset, formatterResolver, out readSize);
|
||||
return deserialize(deserializeCustomFormatters, ref reader, resolver);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ using System.Reflection.Emit;
|
|||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Buffers;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
|
@ -125,9 +126,9 @@ namespace MessagePack.Resolvers
|
|||
BuildSerialize(type, unionAttrs, method, typeToKeyAndJumpMap, il);
|
||||
}
|
||||
{
|
||||
var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
|
||||
var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.NewSlot,
|
||||
type,
|
||||
new Type[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
new Type[] { refMessagePackReader, typeof(IFormatterResolver) });
|
||||
|
||||
var il = method.GetILGenerator();
|
||||
BuildDeserialize(type, unionAttrs, method, keyToJumpMap, il);
|
||||
|
@ -292,33 +293,25 @@ namespace MessagePack.Resolvers
|
|||
il.EmitStarg(2);
|
||||
}
|
||||
|
||||
// T Deserialize([arg:1]byte[] bytes, [arg:2]int offset, [arg:3]IFormatterResolver formatterResolver, [arg:4]out int readSize);
|
||||
// T Deserialize([arg:1]ref MessagePackReader reader, [arg:2]IFormatterResolver formatterResolver);
|
||||
static void BuildDeserialize(Type type, UnionAttribute[] infos, MethodBuilder method, FieldBuilder keyToJumpMap, ILGenerator il)
|
||||
{
|
||||
// if(MessagePackBinary.IsNil) readSize = 1, return null;
|
||||
// if(MessagePackBinary.TryReadNil()) { return null; }
|
||||
var falseLabel = il.DefineLabel();
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.IsNil);
|
||||
il.EmitCall(MessagePackReaderTypeInfo.TryReadNil);
|
||||
il.Emit(OpCodes.Brfalse_S, falseLabel);
|
||||
|
||||
il.EmitLdarg(4);
|
||||
il.EmitLdc_I4(1);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
il.Emit(OpCodes.Ldnull);
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
// read-array header and validate, ReadArrayHeader(bytes, offset, out readSize) != 2) throw;
|
||||
// read-array header and validate, reader.ReadArrayHeader() != 2) throw;
|
||||
il.MarkLabel(falseLabel);
|
||||
var startOffset = il.DeclareLocal(typeof(int));
|
||||
il.EmitLdarg(2);
|
||||
il.EmitStloc(startOffset);
|
||||
|
||||
var rightLabel = il.DefineLabel();
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadArrayHeader);
|
||||
var writer = new ArgumentField(il, 1);
|
||||
writer.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.ReadArrayHeader);
|
||||
il.EmitLdc_I4(2);
|
||||
il.Emit(OpCodes.Beq_S, rightLabel);
|
||||
il.Emit(OpCodes.Ldstr, "Invalid Union data was detected. Type:" + type.FullName);
|
||||
|
@ -326,16 +319,12 @@ namespace MessagePack.Resolvers
|
|||
il.Emit(OpCodes.Throw);
|
||||
|
||||
il.MarkLabel(rightLabel);
|
||||
EmitOffsetPlusReadSize(il);
|
||||
|
||||
// read key
|
||||
var key = il.DeclareLocal(typeof(int));
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadInt32);
|
||||
writer.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.ReadInt32);
|
||||
il.EmitStloc(key);
|
||||
EmitOffsetPlusReadSize(il);
|
||||
|
||||
// is-sequential don't need else convert key to jump-table value
|
||||
if (!IsZeroStartSequential(infos))
|
||||
|
@ -364,41 +353,28 @@ namespace MessagePack.Resolvers
|
|||
il.Emit(OpCodes.Switch, switchLabels.Select(x => x.Label).ToArray());
|
||||
|
||||
// default
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitCall(MessagePackBinaryTypeInfo.ReadNextBlock);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.EmitStarg(2);
|
||||
writer.EmitLdarg();
|
||||
il.EmitCall(MessagePackReaderTypeInfo.Skip);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
|
||||
foreach (var item in switchLabels)
|
||||
{
|
||||
il.MarkLabel(item.Label);
|
||||
il.EmitLdarg(3);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitCall(getFormatterWithVerify.MakeGenericMethod(item.Attr.SubType));
|
||||
il.EmitLdarg(1);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(3);
|
||||
il.EmitLdarg(4);
|
||||
il.EmitCall(getDeserialize(item.Attr.SubType));
|
||||
if (item.Attr.SubType.GetTypeInfo().IsValueType)
|
||||
{
|
||||
il.Emit(OpCodes.Box, item.Attr.SubType);
|
||||
}
|
||||
il.Emit(OpCodes.Stloc, result);
|
||||
EmitOffsetPlusReadSize(il);
|
||||
il.Emit(OpCodes.Br, loopEnd);
|
||||
}
|
||||
|
||||
il.MarkLabel(loopEnd);
|
||||
|
||||
//// finish readSize = offset - startOffset;
|
||||
il.EmitLdarg(4);
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdloc(startOffset);
|
||||
il.Emit(OpCodes.Sub);
|
||||
il.Emit(OpCodes.Stind_I4);
|
||||
il.Emit(OpCodes.Ldloc, result);
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
@ -412,24 +388,15 @@ namespace MessagePack.Resolvers
|
|||
return true;
|
||||
}
|
||||
|
||||
static void EmitOffsetPlusReadSize(ILGenerator il)
|
||||
{
|
||||
il.EmitLdarg(2);
|
||||
il.EmitLdarg(4);
|
||||
il.Emit(OpCodes.Ldind_I4);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.EmitStarg(2);
|
||||
}
|
||||
|
||||
// EmitInfos...
|
||||
|
||||
static readonly Type refByte = typeof(byte[]).MakeByRefType();
|
||||
static readonly Type refInt = typeof(int).MakeByRefType();
|
||||
static readonly Type refMessagePackReader = typeof(MessagePackReader).MakeByRefType();
|
||||
static readonly Type refKvp = typeof(KeyValuePair<int, int>).MakeByRefType();
|
||||
static readonly MethodInfo getFormatterWithVerify = typeof(FormatterResolverExtensions).GetRuntimeMethods().First(x => x.Name == "GetFormatterWithVerify");
|
||||
|
||||
static readonly Func<Type, MethodInfo> getSerialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Serialize", new[] { refByte, typeof(int), t, typeof(IFormatterResolver) });
|
||||
static readonly Func<Type, MethodInfo> getDeserialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Deserialize", new[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), refInt });
|
||||
static readonly Func<Type, MethodInfo> getDeserialize = t => typeof(IMessagePackFormatter<>).MakeGenericType(t).GetRuntimeMethod("Deserialize", new[] { typeof(MessagePackReader).MakeByRefType(), typeof(IFormatterResolver) });
|
||||
|
||||
static readonly FieldInfo runtimeTypeHandleEqualityComparer = typeof(RuntimeTypeHandleEqualityComparer).GetRuntimeField("Default");
|
||||
static readonly ConstructorInfo intIntKeyValuePairConstructor = typeof(KeyValuePair<int, int>).GetTypeInfo().DeclaredConstructors.First(x => x.GetParameters().Length == 2);
|
||||
|
@ -439,7 +406,7 @@ namespace MessagePack.Resolvers
|
|||
|
||||
static readonly ConstructorInfo keyMapDictionaryConstructor = typeof(Dictionary<int, int>).GetTypeInfo().DeclaredConstructors.First(x => { var p = x.GetParameters(); return p.Length == 1 && p[0].ParameterType == typeof(int); });
|
||||
static readonly MethodInfo keyMapDictionaryAdd = typeof(Dictionary<int, int>).GetRuntimeMethod("Add", new[] { typeof(int), typeof(int) });
|
||||
static readonly MethodInfo keyMapDictionaryTryGetValue = typeof(Dictionary<int, int>).GetRuntimeMethod("TryGetValue", new[] { typeof(int), refInt });
|
||||
static readonly MethodInfo keyMapDictionaryTryGetValue = typeof(Dictionary<int, int>).GetRuntimeMethod("TryGetValue", new[] { typeof(int), typeof(int).MakeByRefType() });
|
||||
|
||||
static readonly MethodInfo objectGetType = typeof(object).GetRuntimeMethod("GetType", Type.EmptyTypes);
|
||||
static readonly MethodInfo getTypeHandle = typeof(Type).GetRuntimeProperty("TypeHandle").GetGetMethod();
|
||||
|
@ -462,20 +429,25 @@ namespace MessagePack.Resolvers
|
|||
public static MethodInfo WriteInt32 = typeof(MessagePackBinary).GetRuntimeMethod("WriteInt32", new[] { refByte, typeof(int), typeof(int) });
|
||||
public static MethodInfo WriteBytes = typeof(MessagePackBinary).GetRuntimeMethod("WriteBytes", new[] { refByte, typeof(int), typeof(byte[]) });
|
||||
public static MethodInfo WriteNil = typeof(MessagePackBinary).GetRuntimeMethod("WriteNil", new[] { refByte, typeof(int) });
|
||||
public static MethodInfo ReadBytes = typeof(MessagePackBinary).GetRuntimeMethod("ReadBytes", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadInt32 = typeof(MessagePackBinary).GetRuntimeMethod("ReadInt32", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadString = typeof(MessagePackBinary).GetRuntimeMethod("ReadString", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo IsNil = typeof(MessagePackBinary).GetRuntimeMethod("IsNil", new[] { typeof(byte[]), typeof(int) });
|
||||
public static MethodInfo ReadNextBlock = typeof(MessagePackBinary).GetRuntimeMethod("ReadNextBlock", new[] { typeof(byte[]), typeof(int) });
|
||||
public static MethodInfo WriteStringUnsafe = typeof(MessagePackBinary).GetRuntimeMethod("WriteStringUnsafe", new[] { refByte, typeof(int), typeof(string), typeof(int) });
|
||||
|
||||
public static MethodInfo ReadArrayHeader = typeof(MessagePackBinary).GetRuntimeMethod("ReadArrayHeader", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
public static MethodInfo ReadMapHeader = typeof(MessagePackBinary).GetRuntimeMethod("ReadMapHeader", new[] { typeof(byte[]), typeof(int), refInt });
|
||||
|
||||
static MessagePackBinaryTypeInfo()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
static class MessagePackReaderTypeInfo
|
||||
{
|
||||
public static TypeInfo ReaderTypeInfo = typeof(MessagePackReader).GetTypeInfo();
|
||||
|
||||
public static MethodInfo ReadBytes = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadBytes), Type.EmptyTypes);
|
||||
public static MethodInfo ReadInt32 = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadInt32), Type.EmptyTypes);
|
||||
public static MethodInfo ReadString = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadString), Type.EmptyTypes);
|
||||
public static MethodInfo TryReadNil = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.TryReadNil), Type.EmptyTypes);
|
||||
public static MethodInfo Skip = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.Skip), Type.EmptyTypes);
|
||||
public static MethodInfo ReadArrayHeader = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadArrayHeader), Type.EmptyTypes);
|
||||
public static MethodInfo ReadMapHeader = typeof(MessagePackReader).GetRuntimeMethod(nameof(MessagePackReader.ReadMapHeader), Type.EmptyTypes);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -178,7 +178,7 @@ namespace MessagePack.Internal
|
|||
BuiltinResolver.Instance, // Try Builtin
|
||||
AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter]
|
||||
|
||||
#if !(NETSTANDARD || NETFRAMEWORK)
|
||||
#if UNITY
|
||||
MessagePack.Unity.UnityResolver.Instance,
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,375 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace System.Buffers
|
||||
{
|
||||
internal ref partial struct SequenceReader<T> where T : unmanaged, IEquatable<T>
|
||||
{
|
||||
private SequencePosition _currentPosition;
|
||||
private SequencePosition _nextPosition;
|
||||
private bool _moreData;
|
||||
private long _length;
|
||||
|
||||
/// <summary>
|
||||
/// Create a <see cref="SequenceReader{T}"/> over the given <see cref="ReadOnlySequence{T}"/>.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public SequenceReader(ReadOnlySequence<T> sequence)
|
||||
{
|
||||
CurrentSpanIndex = 0;
|
||||
Consumed = 0;
|
||||
Sequence = sequence;
|
||||
_currentPosition = sequence.Start;
|
||||
_length = -1;
|
||||
|
||||
var first = sequence.First.Span;
|
||||
_nextPosition = sequence.GetPosition(first.Length);
|
||||
CurrentSpan = first;
|
||||
_moreData = first.Length > 0;
|
||||
|
||||
if (!_moreData && !sequence.IsSingleSegment)
|
||||
{
|
||||
_moreData = true;
|
||||
GetNextSpan();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when there is no more data in the <see cref="Sequence"/>.
|
||||
/// </summary>
|
||||
public bool End => !_moreData;
|
||||
|
||||
/// <summary>
|
||||
/// The underlying <see cref="ReadOnlySequence{T}"/> for the reader.
|
||||
/// </summary>
|
||||
public ReadOnlySequence<T> Sequence { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The current position in the <see cref="Sequence"/>.
|
||||
/// </summary>
|
||||
public SequencePosition Position
|
||||
=> Sequence.GetPosition(CurrentSpanIndex, _currentPosition);
|
||||
|
||||
/// <summary>
|
||||
/// The current segment in the <see cref="Sequence"/> as a span.
|
||||
/// </summary>
|
||||
public ReadOnlySpan<T> CurrentSpan { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The index in the <see cref="CurrentSpan"/>.
|
||||
/// </summary>
|
||||
public int CurrentSpanIndex { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The unread portion of the <see cref="CurrentSpan"/>.
|
||||
/// </summary>
|
||||
public ReadOnlySpan<T> UnreadSpan
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => CurrentSpan.Slice(CurrentSpanIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The total number of <typeparamref name="T"/>'s processed by the reader.
|
||||
/// </summary>
|
||||
public long Consumed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Remaining <typeparamref name="T"/>'s in the reader's <see cref="Sequence"/>.
|
||||
/// </summary>
|
||||
public long Remaining => Length - Consumed;
|
||||
|
||||
/// <summary>
|
||||
/// Count of <typeparamref name="T"/> in the reader's <see cref="Sequence"/>.
|
||||
/// </summary>
|
||||
public long Length
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if (_length < 0)
|
||||
{
|
||||
// Cache the length
|
||||
_length = Sequence.Length;
|
||||
}
|
||||
return _length;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Peeks at the next value without advancing the reader.
|
||||
/// </summary>
|
||||
/// <param name="value">The next value or default if at the end.</param>
|
||||
/// <returns>False if at the end of the reader.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryPeek(out T value)
|
||||
{
|
||||
if (_moreData)
|
||||
{
|
||||
value = CurrentSpan[CurrentSpanIndex];
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the next value and advance the reader.
|
||||
/// </summary>
|
||||
/// <param name="value">The next value or default if at the end.</param>
|
||||
/// <returns>False if at the end of the reader.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryRead(out T value)
|
||||
{
|
||||
if (End)
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = CurrentSpan[CurrentSpanIndex];
|
||||
CurrentSpanIndex++;
|
||||
Consumed++;
|
||||
|
||||
if (CurrentSpanIndex >= CurrentSpan.Length)
|
||||
{
|
||||
GetNextSpan();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move the reader back the specified number of items.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Rewind(long count)
|
||||
{
|
||||
if (count < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
}
|
||||
|
||||
Consumed -= count;
|
||||
|
||||
if (CurrentSpanIndex >= count)
|
||||
{
|
||||
CurrentSpanIndex -= (int)count;
|
||||
_moreData = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Current segment doesn't have enough data, scan backward through segments
|
||||
RetreatToPreviousSpan(Consumed);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void RetreatToPreviousSpan(long consumed)
|
||||
{
|
||||
ResetReader();
|
||||
Advance(consumed);
|
||||
}
|
||||
|
||||
private void ResetReader()
|
||||
{
|
||||
CurrentSpanIndex = 0;
|
||||
Consumed = 0;
|
||||
_currentPosition = Sequence.Start;
|
||||
_nextPosition = _currentPosition;
|
||||
|
||||
if (Sequence.TryGet(ref _nextPosition, out ReadOnlyMemory<T> memory, advance: true))
|
||||
{
|
||||
_moreData = true;
|
||||
|
||||
if (memory.Length == 0)
|
||||
{
|
||||
CurrentSpan = default;
|
||||
// No data in the first span, move to one with data
|
||||
GetNextSpan();
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentSpan = memory.Span;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No data in any spans and at end of sequence
|
||||
_moreData = false;
|
||||
CurrentSpan = default;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next segment with available data, if any.
|
||||
/// </summary>
|
||||
private void GetNextSpan()
|
||||
{
|
||||
if (!Sequence.IsSingleSegment)
|
||||
{
|
||||
SequencePosition previousNextPosition = _nextPosition;
|
||||
while (Sequence.TryGet(ref _nextPosition, out ReadOnlyMemory<T> memory, advance: true))
|
||||
{
|
||||
_currentPosition = previousNextPosition;
|
||||
if (memory.Length > 0)
|
||||
{
|
||||
CurrentSpan = memory.Span;
|
||||
CurrentSpanIndex = 0;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentSpan = default;
|
||||
CurrentSpanIndex = 0;
|
||||
previousNextPosition = _nextPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
_moreData = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move the reader ahead the specified number of items.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Advance(long count)
|
||||
{
|
||||
const long TooBigOrNegative = unchecked((long)0xFFFFFFFF80000000);
|
||||
if ((count & TooBigOrNegative) == 0 && CurrentSpan.Length - CurrentSpanIndex > (int)count)
|
||||
{
|
||||
CurrentSpanIndex += (int)count;
|
||||
Consumed += count;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Can't satisfy from the current span
|
||||
AdvanceToNextSpan(count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unchecked helper to avoid unnecessary checks where you know count is valid.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void AdvanceCurrentSpan(long count)
|
||||
{
|
||||
Debug.Assert(count >= 0);
|
||||
|
||||
Consumed += count;
|
||||
CurrentSpanIndex += (int)count;
|
||||
if (CurrentSpanIndex >= CurrentSpan.Length)
|
||||
GetNextSpan();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Only call this helper if you know that you are advancing in the current span
|
||||
/// with valid count and there is no need to fetch the next one.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void AdvanceWithinSpan(long count)
|
||||
{
|
||||
Debug.Assert(count >= 0);
|
||||
|
||||
Consumed += count;
|
||||
CurrentSpanIndex += (int)count;
|
||||
|
||||
Debug.Assert(CurrentSpanIndex < CurrentSpan.Length);
|
||||
}
|
||||
|
||||
private void AdvanceToNextSpan(long count)
|
||||
{
|
||||
if (count < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
}
|
||||
|
||||
Consumed += count;
|
||||
while (_moreData)
|
||||
{
|
||||
int remaining = CurrentSpan.Length - CurrentSpanIndex;
|
||||
|
||||
if (remaining > count)
|
||||
{
|
||||
CurrentSpanIndex += (int)count;
|
||||
count = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// As there may not be any further segments we need to
|
||||
// push the current index to the end of the span.
|
||||
CurrentSpanIndex += remaining;
|
||||
count -= remaining;
|
||||
Debug.Assert(count >= 0);
|
||||
|
||||
GetNextSpan();
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (count != 0)
|
||||
{
|
||||
// Not enough data left- adjust for where we actually ended and throw
|
||||
Consumed -= count;
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies data from the current <see cref="Position"/> to the given <paramref name="destination"/> span.
|
||||
/// </summary>
|
||||
/// <param name="destination">Destination to copy to.</param>
|
||||
/// <returns>True if there is enough data to copy to the <paramref name="destination"/>.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryCopyTo(Span<T> destination)
|
||||
{
|
||||
ReadOnlySpan<T> firstSpan = UnreadSpan;
|
||||
if (firstSpan.Length >= destination.Length)
|
||||
{
|
||||
firstSpan.Slice(0, destination.Length).CopyTo(destination);
|
||||
return true;
|
||||
}
|
||||
|
||||
return TryCopyMultisegment(destination);
|
||||
}
|
||||
|
||||
internal bool TryCopyMultisegment(Span<T> destination)
|
||||
{
|
||||
if (Remaining < destination.Length)
|
||||
return false;
|
||||
|
||||
ReadOnlySpan<T> firstSpan = UnreadSpan;
|
||||
Debug.Assert(firstSpan.Length < destination.Length);
|
||||
firstSpan.CopyTo(destination);
|
||||
int copied = firstSpan.Length;
|
||||
|
||||
SequencePosition next = _nextPosition;
|
||||
while (Sequence.TryGet(ref next, out ReadOnlyMemory<T> nextSegment, true))
|
||||
{
|
||||
if (nextSegment.Length > 0)
|
||||
{
|
||||
ReadOnlySpan<T> nextSpan = nextSegment.Span;
|
||||
int toCopy = Math.Min(nextSpan.Length, destination.Length - copied);
|
||||
nextSpan.Slice(0, toCopy).CopyTo(destination.Slice(copied));
|
||||
copied += toCopy;
|
||||
if (copied >= destination.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Buffers.Binary;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace System.Buffers
|
||||
{
|
||||
internal static partial class SequenceReaderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Try to read the given type out of the buffer if possible. Warning: this is dangerous to use with arbitrary
|
||||
/// structs- see remarks for full details.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// IMPORTANT: The read is a straight copy of bits. If a struct depends on specific state of it's members to
|
||||
/// behave correctly this can lead to exceptions, etc. If reading endian specific integers, use the explicit
|
||||
/// overloads such as <see cref="TryReadLittleEndian(ref SequenceReader{byte}, out short)"/>
|
||||
/// </remarks>
|
||||
/// <returns>
|
||||
/// True if successful. <paramref name="value"/> will be default if failed (due to lack of space).
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe bool TryRead<T>(ref this SequenceReader<byte> reader, out T value) where T : unmanaged
|
||||
{
|
||||
ReadOnlySpan<byte> span = reader.UnreadSpan;
|
||||
if (span.Length < sizeof(T))
|
||||
return TryReadMultisegment(ref reader, out value);
|
||||
|
||||
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(span));
|
||||
reader.Advance(sizeof(T));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static unsafe bool TryReadMultisegment<T>(ref SequenceReader<byte> reader, out T value) where T : unmanaged
|
||||
{
|
||||
Debug.Assert(reader.UnreadSpan.Length < sizeof(T));
|
||||
|
||||
// Not enough data in the current segment, try to peek for the data we need.
|
||||
T buffer = default;
|
||||
Span<byte> tempSpan = new Span<byte>(&buffer, sizeof(T));
|
||||
|
||||
if (!reader.TryCopyTo(tempSpan))
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(tempSpan));
|
||||
reader.Advance(sizeof(T));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an <see cref="Int16"/> as little endian.
|
||||
/// </summary>
|
||||
/// <returns>False if there wasn't enough data for an <see cref="Int16"/>.</returns>
|
||||
public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out short value)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
return reader.TryRead(out value);
|
||||
}
|
||||
|
||||
return TryReadReverseEndianness(ref reader, out value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an <see cref="Int16"/> as big endian.
|
||||
/// </summary>
|
||||
/// <returns>False if there wasn't enough data for an <see cref="Int16"/>.</returns>
|
||||
public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out short value)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
{
|
||||
return reader.TryRead(out value);
|
||||
}
|
||||
|
||||
return TryReadReverseEndianness(ref reader, out value);
|
||||
}
|
||||
|
||||
private static bool TryReadReverseEndianness(ref SequenceReader<byte> reader, out short value)
|
||||
{
|
||||
if (reader.TryRead(out value))
|
||||
{
|
||||
value = BinaryPrimitives.ReverseEndianness(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an <see cref="Int32"/> as little endian.
|
||||
/// </summary>
|
||||
/// <returns>False if there wasn't enough data for an <see cref="Int32"/>.</returns>
|
||||
public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out int value)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
return reader.TryRead(out value);
|
||||
}
|
||||
|
||||
return TryReadReverseEndianness(ref reader, out value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an <see cref="Int32"/> as big endian.
|
||||
/// </summary>
|
||||
/// <returns>False if there wasn't enough data for an <see cref="Int32"/>.</returns>
|
||||
public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out int value)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
{
|
||||
return reader.TryRead(out value);
|
||||
}
|
||||
|
||||
return TryReadReverseEndianness(ref reader, out value);
|
||||
}
|
||||
|
||||
private static bool TryReadReverseEndianness(ref SequenceReader<byte> reader, out int value)
|
||||
{
|
||||
if (reader.TryRead(out value))
|
||||
{
|
||||
value = BinaryPrimitives.ReverseEndianness(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an <see cref="Int64"/> as little endian.
|
||||
/// </summary>
|
||||
/// <returns>False if there wasn't enough data for an <see cref="Int64"/>.</returns>
|
||||
public static bool TryReadLittleEndian(ref this SequenceReader<byte> reader, out long value)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
return reader.TryRead(out value);
|
||||
}
|
||||
|
||||
return TryReadReverseEndianness(ref reader, out value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an <see cref="Int64"/> as big endian.
|
||||
/// </summary>
|
||||
/// <returns>False if there wasn't enough data for an <see cref="Int64"/>.</returns>
|
||||
public static bool TryReadBigEndian(ref this SequenceReader<byte> reader, out long value)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
{
|
||||
return reader.TryRead(out value);
|
||||
}
|
||||
|
||||
return TryReadReverseEndianness(ref reader, out value);
|
||||
}
|
||||
|
||||
private static bool TryReadReverseEndianness(ref SequenceReader<byte> reader, out long value)
|
||||
{
|
||||
if (reader.TryRead(out value))
|
||||
{
|
||||
value = BinaryPrimitives.ReverseEndianness(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
#if !SPAN_BUILTIN
|
||||
internal static class StreamPolyfillExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads from the stream into a memory buffer.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to read from.</param>
|
||||
/// <param name="buffer">The buffer to read directly into.</param>
|
||||
/// <returns>The number of bytes actually read.</returns>
|
||||
internal static int Read(this Stream stream, Span<byte> buffer)
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length);
|
||||
try
|
||||
{
|
||||
int bytesRead = stream.Read(array, 0, buffer.Length);
|
||||
new Span<byte>(array, 0, bytesRead).CopyTo(buffer);
|
||||
return bytesRead;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads from the stream into a memory buffer.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to read from.</param>
|
||||
/// <param name="buffer">The buffer to read directly into.</param>
|
||||
/// <param name="cancellationToken">A cancellation token.</param>
|
||||
/// <returns>The number of bytes actually read.</returns>
|
||||
/// <devremarks>
|
||||
/// This method shamelessly copied from the .NET Core 2.1 Stream class: https://github.com/dotnet/coreclr/blob/a113b1c803783c9d64f1f0e946ff9a853e3bc140/src/System.Private.CoreLib/shared/System/IO/Stream.cs#L366-L391.
|
||||
/// </devremarks>
|
||||
internal static ValueTask<int> ReadAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array))
|
||||
{
|
||||
return new ValueTask<int>(stream.ReadAsync(array.Array, array.Offset, array.Count, cancellationToken));
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
|
||||
return FinishReadAsync(stream.ReadAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer, buffer);
|
||||
|
||||
async ValueTask<int> FinishReadAsync(Task<int> readTask, byte[] localBuffer, Memory<byte> localDestination)
|
||||
{
|
||||
try
|
||||
{
|
||||
int result = await readTask.ConfigureAwait(false);
|
||||
new Span<byte>(localBuffer, 0, result).CopyTo(localDestination.Span);
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(localBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -22,22 +22,16 @@ namespace MessagePack
|
|||
}
|
||||
}
|
||||
|
||||
internal static unsafe string GetString(this Encoding encoding, ReadOnlyMemory<byte> bytes)
|
||||
internal static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
if (bytes.Length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (MemoryMarshal.TryGetArray(bytes, out var segment))
|
||||
fixed (byte* pBytes = bytes)
|
||||
{
|
||||
return encoding.GetString(segment.Array, segment.Offset, segment.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
var tmp = new byte[bytes.Length];
|
||||
bytes.CopyTo(tmp);
|
||||
return encoding.GetString(tmp);
|
||||
return encoding.GetString(pBytes, bytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ namespace MessagePack.Tests.ExtensionTests
|
|||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
body.CopyTo(ms);
|
||||
await body.CopyToAsync(ms);
|
||||
Assert.Equal(messagePackBinary, ms.ToArray());
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,7 @@ namespace MessagePack.Tests.ExtensionTests
|
|||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
body.CopyTo(ms);
|
||||
await body.CopyToAsync(ms);
|
||||
var binary = ms.ToArray();
|
||||
|
||||
binary.IsNot(messagePackBinary);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<CodeAnalysisRuleSet>..\MessagePack.Tests\MessagePack.Tests.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
|
@ -42,18 +43,18 @@ namespace MessagePack.Tests
|
|||
{
|
||||
private MessagePackSerializer serializer = new MessagePackSerializer();
|
||||
|
||||
IEnumerable<string> IteratePropertyNames(byte[] bin)
|
||||
IEnumerable<string> IteratePropertyNames(ReadOnlyMemory<byte> bytes)
|
||||
{
|
||||
var offset = 0;
|
||||
int readSize = 0;
|
||||
var mapCount = MessagePackBinary.ReadMapHeader(bin, 0, out readSize);
|
||||
offset += readSize;
|
||||
var reader = new MessagePackReader(bytes);
|
||||
var mapCount = reader.ReadMapHeader();
|
||||
var result = new string[mapCount];
|
||||
for (int i = 0; i < mapCount; i++)
|
||||
{
|
||||
yield return MessagePackBinary.ReadString(bin, offset, out readSize);
|
||||
offset += readSize;
|
||||
offset += MessagePackBinary.ReadNext(bin, offset);
|
||||
result[i] = reader.ReadString();
|
||||
reader.Skip(); // skip the value
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using SharedData;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
@ -27,9 +28,9 @@ namespace MessagePack.Tests.ExtensionTests
|
|||
public void TestSmall()
|
||||
{
|
||||
// small size binary don't use LZ4 Encode
|
||||
MessagePackBinary.GetMessagePackType(lz4Serializer.Serialize(100), 0).Is(MessagePackType.Integer);
|
||||
MessagePackBinary.GetMessagePackType(lz4Serializer.Serialize("test"), 0).Is(MessagePackType.String);
|
||||
MessagePackBinary.GetMessagePackType(lz4Serializer.Serialize(false), 0).Is(MessagePackType.Boolean);
|
||||
PeekMessagePackType(lz4Serializer.Serialize(100)).Is(MessagePackType.Integer);
|
||||
PeekMessagePackType(lz4Serializer.Serialize("test")).Is(MessagePackType.String);
|
||||
PeekMessagePackType(lz4Serializer.Serialize(false)).Is(MessagePackType.Boolean);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -39,10 +40,10 @@ namespace MessagePack.Tests.ExtensionTests
|
|||
|
||||
var lz4Data = lz4Serializer.Serialize(originalData);
|
||||
|
||||
MessagePackBinary.GetMessagePackType(lz4Data, 0).Is(MessagePackType.Extension);
|
||||
int r;
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(lz4Data, 0, out r);
|
||||
header.TypeCode.Is((sbyte)LZ4MessagePackSerializer.ExtensionTypeCode);
|
||||
PeekMessagePackType(lz4Data).Is(MessagePackType.Extension);
|
||||
var lz4DataReader = new MessagePackReader(lz4Data);
|
||||
var header = lz4DataReader.ReadExtensionFormatHeader();
|
||||
header.TypeCode.Is(LZ4MessagePackSerializer.ExtensionTypeCode);
|
||||
|
||||
var decompress = lz4Serializer.Deserialize<int[]>(lz4Data);
|
||||
|
||||
|
@ -56,10 +57,10 @@ namespace MessagePack.Tests.ExtensionTests
|
|||
|
||||
var lz4Data = lz4NonGenericSerializer.Serialize(typeof(FirstSimpleData[]), originalData);
|
||||
|
||||
MessagePackBinary.GetMessagePackType(lz4Data, 0).Is(MessagePackType.Extension);
|
||||
int r;
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(lz4Data, 0, out r);
|
||||
header.TypeCode.Is((sbyte)LZ4MessagePackSerializer.ExtensionTypeCode);
|
||||
PeekMessagePackType(lz4Data).Is(MessagePackType.Extension);
|
||||
var lz4DataReader = new MessagePackReader(lz4Data);
|
||||
var header = lz4DataReader.ReadExtensionFormatHeader();
|
||||
header.TypeCode.Is(LZ4MessagePackSerializer.ExtensionTypeCode);
|
||||
|
||||
var decompress = lz4NonGenericSerializer.Deserialize(typeof(FirstSimpleData[]), lz4Data);
|
||||
|
||||
|
@ -137,18 +138,10 @@ namespace MessagePack.Tests.ExtensionTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Decode()
|
||||
private static MessagePackType PeekMessagePackType(byte[] msgpackBuffer)
|
||||
{
|
||||
var originalData = Enumerable.Range(1, 100).Select(x => new FirstSimpleData { Prop1 = x * x, Prop2 = "hoge", Prop3 = x }).ToArray();
|
||||
var simple = lz4Serializer.Serialize(100);
|
||||
var complex = lz4Serializer.Serialize(originalData);
|
||||
|
||||
var msgpack1 = LZ4MessagePackSerializer.Decode(simple);
|
||||
var msgpack2 = LZ4MessagePackSerializer.Decode(complex);
|
||||
|
||||
serializer.Deserialize<int>(msgpack1).Is(100);
|
||||
serializer.Deserialize<FirstSimpleData[]>(msgpack2).IsStructuralEqual(originalData);
|
||||
var reader = new MessagePackReader(new ReadOnlySequence<byte>(msgpackBuffer));
|
||||
return reader.NextMessagePackType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using SharedData;
|
||||
using MessagePack.Formatters;
|
||||
using SharedData;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
@ -222,11 +223,14 @@ namespace MessagePack.Tests
|
|||
byte[] bytesD = null;
|
||||
MessagePackBinary.WriteString(ref bytesD, 0, d).Is(Encoding.UTF8.GetByteCount(d) + 5);
|
||||
|
||||
int readSize = 0;
|
||||
MessagePackBinary.ReadString(bytesA, 0, out readSize).Is(a);
|
||||
MessagePackBinary.ReadString(bytesB, 0, out readSize).Is(b);
|
||||
MessagePackBinary.ReadString(bytesC, 0, out readSize).Is(c);
|
||||
MessagePackBinary.ReadString(bytesD, 0, out readSize).Is(d);
|
||||
var readerA = new MessagePackReader(bytesA);
|
||||
var readerB = new MessagePackReader(bytesB);
|
||||
var readerC = new MessagePackReader(bytesC);
|
||||
var readerD = new MessagePackReader(bytesD);
|
||||
readerA.ReadString().Is(a);
|
||||
readerB.ReadString().Is(b);
|
||||
readerC.ReadString().Is(c);
|
||||
readerD.ReadString().Is(d);
|
||||
}
|
||||
|
||||
// https://github.com/neuecc/MessagePack-CSharp/issues/22
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
@ -83,9 +84,7 @@ namespace MessagePack.Tests
|
|||
{
|
||||
serializer.Serialize(t).Is(serializer.Serialize(reference));
|
||||
Convert(t).Array.Is(reference);
|
||||
MessagePackBinary.IsNil(serializer.Serialize(t2), 0).IsTrue();
|
||||
new MessagePackReader(serializer.Serialize(t2)).IsNil.IsTrue();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
|
||||
<NoWarn>$(NoWarn);CS1701</NoWarn>
|
||||
<CodeAnalysisRuleSet>MessagePack.Tests.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\sandbox\SharedData\SharedData.csproj" />
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RuleSet Name="Microsoft Managed Recommended Rules" Description="These rules focus on the most critical problems in your code, including potential security holes, application crashes, and other important logic and design errors. It is recommended to include this rule set in any custom rule set you create for your projects." ToolsVersion="10.0">
|
||||
<Localization ResourceAssembly="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.dll" ResourceBaseName="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.Localized">
|
||||
<Name Resource="MinimumRecommendedRules_Name" />
|
||||
<Description Resource="MinimumRecommendedRules_Description" />
|
||||
</Localization>
|
||||
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
|
||||
<Rule Id="CA1001" Action="Warning" />
|
||||
<Rule Id="CA1009" Action="Warning" />
|
||||
<Rule Id="CA1016" Action="Warning" />
|
||||
<Rule Id="CA1033" Action="Warning" />
|
||||
<Rule Id="CA1049" Action="Warning" />
|
||||
<Rule Id="CA1060" Action="Warning" />
|
||||
<Rule Id="CA1061" Action="Warning" />
|
||||
<Rule Id="CA1063" Action="Warning" />
|
||||
<Rule Id="CA1065" Action="Warning" />
|
||||
<Rule Id="CA1301" Action="Warning" />
|
||||
<Rule Id="CA1400" Action="Warning" />
|
||||
<Rule Id="CA1401" Action="Warning" />
|
||||
<Rule Id="CA1403" Action="Warning" />
|
||||
<Rule Id="CA1404" Action="Warning" />
|
||||
<Rule Id="CA1405" Action="Warning" />
|
||||
<Rule Id="CA1410" Action="Warning" />
|
||||
<Rule Id="CA1415" Action="Warning" />
|
||||
<Rule Id="CA1821" Action="Warning" />
|
||||
<Rule Id="CA1900" Action="Warning" />
|
||||
<Rule Id="CA1901" Action="Warning" />
|
||||
<Rule Id="CA2002" Action="Warning" />
|
||||
<Rule Id="CA2100" Action="Warning" />
|
||||
<Rule Id="CA2101" Action="Warning" />
|
||||
<Rule Id="CA2108" Action="Warning" />
|
||||
<Rule Id="CA2111" Action="Warning" />
|
||||
<Rule Id="CA2112" Action="Warning" />
|
||||
<Rule Id="CA2114" Action="Warning" />
|
||||
<Rule Id="CA2116" Action="Warning" />
|
||||
<Rule Id="CA2117" Action="Warning" />
|
||||
<Rule Id="CA2122" Action="Warning" />
|
||||
<Rule Id="CA2123" Action="Warning" />
|
||||
<Rule Id="CA2124" Action="Warning" />
|
||||
<Rule Id="CA2126" Action="Warning" />
|
||||
<Rule Id="CA2131" Action="Warning" />
|
||||
<Rule Id="CA2132" Action="Warning" />
|
||||
<Rule Id="CA2133" Action="Warning" />
|
||||
<Rule Id="CA2134" Action="Warning" />
|
||||
<Rule Id="CA2137" Action="Warning" />
|
||||
<Rule Id="CA2138" Action="Warning" />
|
||||
<Rule Id="CA2140" Action="Warning" />
|
||||
<Rule Id="CA2141" Action="Warning" />
|
||||
<Rule Id="CA2146" Action="Warning" />
|
||||
<Rule Id="CA2147" Action="Warning" />
|
||||
<Rule Id="CA2149" Action="Warning" />
|
||||
<Rule Id="CA2200" Action="Warning" />
|
||||
<Rule Id="CA2202" Action="Warning" />
|
||||
<Rule Id="CA2207" Action="Warning" />
|
||||
<Rule Id="CA2212" Action="Warning" />
|
||||
<Rule Id="CA2213" Action="Warning" />
|
||||
<Rule Id="CA2214" Action="Warning" />
|
||||
<Rule Id="CA2216" Action="Warning" />
|
||||
<Rule Id="CA2220" Action="Warning" />
|
||||
<Rule Id="CA2229" Action="Warning" />
|
||||
<Rule Id="CA2231" Action="Warning" />
|
||||
<Rule Id="CA2232" Action="Warning" />
|
||||
<Rule Id="CA2235" Action="Warning" />
|
||||
<Rule Id="CA2236" Action="Warning" />
|
||||
<Rule Id="CA2237" Action="Warning" />
|
||||
<Rule Id="CA2238" Action="Warning" />
|
||||
<Rule Id="CA2240" Action="Warning" />
|
||||
<Rule Id="CA2241" Action="Warning" />
|
||||
<Rule Id="CA2242" Action="Warning" />
|
||||
</Rules>
|
||||
<Rules AnalyzerId="Microsoft.VisualStudio.Threading.Analyzers" RuleNamespace="Microsoft.VisualStudio.Threading.Analyzers">
|
||||
<Rule Id="VSTHRD200" Action="Hidden" />
|
||||
<Rule Id="VSTHRD002" Action="Hidden" />
|
||||
</Rules>
|
||||
</RuleSet>
|
|
@ -181,198 +181,5 @@ namespace MessagePack.Tests
|
|||
(x) => MessagePackBinary.WriteUInt64ForceUInt64Block(x, 11),
|
||||
(ref byte[] x, int y) => MessagePackBinary.WriteUInt64ForceUInt64Block(ref x, y, 11));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void Read()
|
||||
{
|
||||
void Check1<T, T2>(T data, T2 result, Func<Stream, T2> streamRead)
|
||||
{
|
||||
const int CheckOffset = 10;
|
||||
|
||||
byte[] bytes = null;
|
||||
var len = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter<T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance);
|
||||
MessagePackBinary.FastResize(ref bytes, CheckOffset + len);
|
||||
|
||||
|
||||
var ms = new MemoryStream(bytes);
|
||||
ms.Position = CheckOffset;
|
||||
|
||||
streamRead(ms).Is(result);
|
||||
}
|
||||
|
||||
void Check2<T>(T data, Func<Stream, T> streamRead)
|
||||
{
|
||||
const int CheckOffset = 10;
|
||||
|
||||
byte[] bytes = null;
|
||||
var len = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter<T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance);
|
||||
MessagePackBinary.FastResize(ref bytes, CheckOffset + len);
|
||||
|
||||
|
||||
var ms = new MemoryStream(bytes);
|
||||
ms.Position = CheckOffset;
|
||||
|
||||
streamRead(ms).Is(data);
|
||||
}
|
||||
|
||||
Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, 7, x => MessagePackBinary.ReadArrayHeader(x));
|
||||
Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, (uint)7, x => MessagePackBinary.ReadArrayHeaderRaw(x));
|
||||
Check1(Nil.Default, true, x => MessagePackBinary.IsNil((x)));
|
||||
Check2(true, x => MessagePackBinary.ReadBoolean(x));
|
||||
Check2((byte)100, x => MessagePackBinary.ReadByte(x));
|
||||
Check2(new byte[] { 1, 10, 100, 245 }, x => MessagePackBinary.ReadBytes(x));
|
||||
Check2('あ', x => MessagePackBinary.ReadChar(x));
|
||||
Check2(DateTime.UtcNow, x => MessagePackBinary.ReadDateTime(x));
|
||||
Check2(132, x => MessagePackBinary.ReadInt16(x));
|
||||
Check2(423, x => MessagePackBinary.ReadInt32(x));
|
||||
Check2(64332, x => MessagePackBinary.ReadInt64(x));
|
||||
Check2(Nil.Default, x => MessagePackBinary.ReadNil(x));
|
||||
Check2(11, x => MessagePackBinary.ReadSByte(x));
|
||||
Check2(10.31231f, x => MessagePackBinary.ReadSingle(x));
|
||||
Check2("foobar", x => MessagePackBinary.ReadString(x));
|
||||
Check2(124, x => MessagePackBinary.ReadUInt16(x));
|
||||
Check2((uint)432, x => MessagePackBinary.ReadUInt32(x));
|
||||
Check2((ulong)432, x => MessagePackBinary.ReadUInt64(x));
|
||||
|
||||
|
||||
Check1(new Dictionary<int, int>() { { 1, 2 } }, 1, x => MessagePackBinary.ReadMapHeader(x));
|
||||
Check1(new Dictionary<int, int>() { { 1, 2 } }, (uint)1, x => MessagePackBinary.ReadMapHeaderRaw(x));
|
||||
|
||||
{
|
||||
var block = new object[] { 1, new[] { 1, 10, 100 }, 100 };
|
||||
var bytes = serializer.Serialize(block);
|
||||
var stream = new MemoryStream(bytes);
|
||||
MessagePackBinary.ReadNext(stream); // array(first)
|
||||
MessagePackBinary.ReadNext(stream); // int
|
||||
MessagePackBinary.ReadNextBlock(stream); // skip array
|
||||
MessagePackBinary.ReadInt32(stream).Is(100);
|
||||
}
|
||||
{
|
||||
var block = new object[] { 1, new Dictionary<int, int> { { 1, 10 }, { 111, 200 } }, 100 };
|
||||
var bytes = serializer.Serialize(block);
|
||||
var stream = new MemoryStream(bytes);
|
||||
MessagePackBinary.ReadNext(stream);
|
||||
MessagePackBinary.ReadNext(stream);
|
||||
MessagePackBinary.ReadNextBlock(stream);
|
||||
MessagePackBinary.ReadInt32(stream).Is(100);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Standard()
|
||||
{
|
||||
var o = new SimpleIntKeyData()
|
||||
{
|
||||
Prop1 = 100,
|
||||
Prop2 = ByteEnum.C,
|
||||
Prop3 = "abcde",
|
||||
Prop4 = new SimpleStringKeyData
|
||||
{
|
||||
Prop1 = 99999,
|
||||
Prop2 = ByteEnum.E,
|
||||
Prop3 = 3
|
||||
},
|
||||
Prop5 = new SimpleStructIntKeyData
|
||||
{
|
||||
X = 100,
|
||||
Y = 300,
|
||||
BytesSpecial = new byte[] { 9, 99, 122 }
|
||||
},
|
||||
Prop6 = new SimpleStructStringKeyData
|
||||
{
|
||||
X = 9999,
|
||||
Y = new[] { 1, 10, 100 }
|
||||
},
|
||||
BytesSpecial = new byte[] { 1, 4, 6 }
|
||||
};
|
||||
|
||||
|
||||
var bytes = serializer.Serialize(o);
|
||||
var ms = new MemoryStream(bytes);
|
||||
|
||||
MessagePackBinary.ReadArrayHeader(ms).Is(7);
|
||||
MessagePackBinary.ReadInt32(ms).Is(100);
|
||||
MessagePackBinary.ReadByte(ms).Is((byte)ByteEnum.C);
|
||||
MessagePackBinary.ReadString(ms).Is("abcde");
|
||||
|
||||
MessagePackBinary.ReadMapHeader(ms).Is(3);
|
||||
MessagePackBinary.ReadString(ms).Is("Prop1");
|
||||
MessagePackBinary.ReadInt32(ms).Is(99999);
|
||||
MessagePackBinary.ReadString(ms).Is("Prop2");
|
||||
MessagePackBinary.ReadByte(ms).Is((byte)ByteEnum.E);
|
||||
MessagePackBinary.ReadString(ms).Is("Prop3");
|
||||
MessagePackBinary.ReadInt32(ms).Is(3);
|
||||
|
||||
MessagePackBinary.ReadArrayHeader(ms).Is(3);
|
||||
MessagePackBinary.ReadInt32(ms).Is(100);
|
||||
MessagePackBinary.ReadInt32(ms).Is(300);
|
||||
MessagePackBinary.ReadBytes(ms).Is(new byte[] { 9, 99, 122 });
|
||||
|
||||
MessagePackBinary.ReadMapHeader(ms).Is(2);
|
||||
MessagePackBinary.ReadString(ms).Is("key-X");
|
||||
MessagePackBinary.ReadInt32(ms).Is(9999);
|
||||
MessagePackBinary.ReadString(ms).Is("key-Y");
|
||||
MessagePackBinary.ReadArrayHeader(ms).Is(3);
|
||||
MessagePackBinary.ReadInt32(ms).Is(1);
|
||||
MessagePackBinary.ReadInt32(ms).Is(10);
|
||||
MessagePackBinary.ReadInt32(ms).Is(100);
|
||||
|
||||
MessagePackBinary.ReadBytes(ms).Is(new byte[] { 1, 4, 6 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadStrictDeserialize()
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
serializer.Serialize(ms, new SimpleStringKeyData
|
||||
{
|
||||
Prop1 = 99999,
|
||||
Prop2 = ByteEnum.E,
|
||||
Prop3 = 3
|
||||
});
|
||||
serializer.Serialize(ms, new SimpleStructStringKeyData
|
||||
{
|
||||
X = 9999,
|
||||
Y = new[] { 1, 10, 100 }
|
||||
});
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
var d = serializer.Deserialize<SimpleStringKeyData>(ms, readStrict: true);
|
||||
d.Prop1.Is(99999); d.Prop2.Is(ByteEnum.E); d.Prop3.Is(3);
|
||||
|
||||
var d2 = (SimpleStructStringKeyData)nonGenericSerializer.Deserialize(typeof(SimpleStructStringKeyData), ms, readStrict: true);
|
||||
d2.X.Is(9999); d2.Y.Is(new[] { 1, 10, 100 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadStrictDeserializeLZ4()
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
lz4Serializer.Serialize(ms, new SimpleStringKeyData
|
||||
{
|
||||
Prop1 = 99999,
|
||||
Prop2 = ByteEnum.E,
|
||||
Prop3 = 3
|
||||
});
|
||||
lz4Serializer.Serialize(ms, new string('a', 100000));
|
||||
lz4Serializer.Serialize(ms, new SimpleStructStringKeyData
|
||||
{
|
||||
X = 9999,
|
||||
Y = new[] { 1, 10, 100 }
|
||||
});
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
var d = lz4Serializer.Deserialize<SimpleStringKeyData>(ms, readStrict: true);
|
||||
d.Prop1.Is(99999); d.Prop2.Is(ByteEnum.E); d.Prop3.Is(3);
|
||||
|
||||
var ds = lz4Serializer.Deserialize<string>(ms, readStrict: true);
|
||||
ds.Is(new string('a', 100000));
|
||||
|
||||
var d2 = (SimpleStructStringKeyData)lz4nonGenericSerializer.Deserialize(typeof(SimpleStructStringKeyData), ms, readStrict: true);
|
||||
d2.X.Is(9999); d2.Y.Is(new[] { 1, 10, 100 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,9 @@ using System.Linq;
|
|||
using System.IO;
|
||||
using Xunit;
|
||||
using System.Text;
|
||||
using Nerdbank.Streams;
|
||||
using System.Buffers;
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace MessagePack.Tests
|
||||
{
|
||||
|
@ -23,6 +26,8 @@ namespace MessagePack.Tests
|
|||
return unpacker.LastReadData;
|
||||
}
|
||||
|
||||
MsgPack.MessagePackObject CreateUnpackedReference(ReadOnlySequence<byte> bytes) => CreateUnpackedReference(bytes);
|
||||
|
||||
[Fact]
|
||||
public void NilTest()
|
||||
{
|
||||
|
@ -34,9 +39,9 @@ namespace MessagePack.Tests
|
|||
packer.PackNull().Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadNil(bytes, 0, out readSize).Is(Nil.Default);
|
||||
readSize.Is(1);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadNil().Is(Nil.Default);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).IsNil.IsTrue();
|
||||
}
|
||||
|
@ -54,9 +59,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadBoolean(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadBoolean().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsBoolean().Is(target);
|
||||
}
|
||||
|
@ -76,16 +81,15 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadByte(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadByte().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsByte().Is(target);
|
||||
}
|
||||
|
||||
public static object[][] bytesTestData = new object[][]
|
||||
{
|
||||
new object[]{ (byte[])null, 1 },
|
||||
new object[]{ new byte[] { }, 2 },
|
||||
new object[]{ new byte[] { 1, 2, 3 }, 5 },
|
||||
new object[]{ Enumerable.Repeat((byte)100, byte.MaxValue).ToArray(), 255 + 2 },
|
||||
|
@ -105,9 +109,9 @@ namespace MessagePack.Tests
|
|||
packer.PackBinary(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadBytes(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadBytes().ToArray().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsBinary().Is(target);
|
||||
}
|
||||
|
@ -134,9 +138,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadSByte(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadSByte().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsSByte().Is(target);
|
||||
}
|
||||
|
@ -161,9 +165,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadSingle(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadSingle().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsSingle().Is(target);
|
||||
}
|
||||
|
@ -188,9 +192,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadDouble(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadDouble().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsDouble().Is(target);
|
||||
}
|
||||
|
@ -221,9 +225,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
// stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadInt16(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadInt16().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsInt16().Is(target);
|
||||
}
|
||||
|
@ -271,9 +275,9 @@ namespace MessagePack.Tests
|
|||
}
|
||||
// stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadInt32(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadInt32().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsInt32().Is(target);
|
||||
}
|
||||
|
@ -330,9 +334,9 @@ namespace MessagePack.Tests
|
|||
}
|
||||
// stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadInt64(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadInt64().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsInt64().Is(target);
|
||||
}
|
||||
|
@ -361,9 +365,9 @@ namespace MessagePack.Tests
|
|||
packer.PackMapHeader((int)target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadMapHeaderRaw(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadMapHeader().Is((int)target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
var ms = new MemoryStream(bytes);
|
||||
var unpacker = MsgPack.Unpacker.Create(ms);
|
||||
|
@ -396,9 +400,9 @@ namespace MessagePack.Tests
|
|||
packer.PackArrayHeader((int)target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadArrayHeaderRaw(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadArrayHeader().Is((int)target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
var ms = new MemoryStream(bytes);
|
||||
var unpacker = MsgPack.Unpacker.Create(ms);
|
||||
|
@ -428,9 +432,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadUInt16(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadUInt16().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsUInt16().Is(target);
|
||||
}
|
||||
|
@ -457,9 +461,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadUInt32(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadUInt32().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsUInt32().Is(target);
|
||||
}
|
||||
|
@ -490,9 +494,9 @@ namespace MessagePack.Tests
|
|||
packer.Pack(target).Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadUInt64(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(length);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadUInt64().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
CreateUnpackedReference(bytes).AsUInt64().Is(target);
|
||||
}
|
||||
|
@ -533,11 +537,12 @@ namespace MessagePack.Tests
|
|||
|
||||
var referencePacked = packer.PackString(target);
|
||||
referencePacked.Position.Is(returnLength);
|
||||
stream.ToArray().SequenceEqual(bytes.Take(returnLength).ToArray()).IsTrue();
|
||||
Assert.Equal<byte>(stream.ToArray(), bytes.AsSpan(0, returnLength).ToArray());
|
||||
|
||||
var sequenceReader = new MessagePackReader(bytes.AsMemory(0, returnLength));
|
||||
sequenceReader.ReadString().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadString(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(returnLength);
|
||||
CreateUnpackedReference(bytes).AsStringUtf8().Is(target);
|
||||
}
|
||||
|
||||
|
@ -552,12 +557,13 @@ namespace MessagePack.Tests
|
|||
|
||||
var referencePacked = packer.PackString(target);
|
||||
referencePacked.Position.Is(returnLength);
|
||||
stream.ToArray().SequenceEqual(bytes.Take(returnLength).ToArray()).IsTrue();
|
||||
Assert.Equal<byte>(stream.ToArray(), bytes.AsSpan(0, returnLength).ToArray());
|
||||
|
||||
var sequenceReader = new MessagePackReader(bytes.AsMemory(0, returnLength));
|
||||
var segment = sequenceReader.ReadStringSegment().ToArray();
|
||||
Encoding.UTF8.GetString(segment).Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
int readSize;
|
||||
var segment = MessagePackBinary.ReadStringSegment(bytes, 0, out readSize);
|
||||
Encoding.UTF8.GetString(segment.Array, segment.Offset, segment.Count).Is(target);
|
||||
readSize.Is(returnLength);
|
||||
CreateUnpackedReference(bytes).AsStringUtf8().Is(target);
|
||||
}
|
||||
|
||||
|
@ -579,9 +585,9 @@ namespace MessagePack.Tests
|
|||
referencePacked.Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
MessagePackBinary.ReadChar(bytes, 0, out readSize).Is(target);
|
||||
readSize.Is(returnLength);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
sequenceReader.ReadChar().Is(target);
|
||||
sequenceReader.End.IsTrue();
|
||||
((char)CreateUnpackedReference(bytes).AsUInt16()).Is(target);
|
||||
}
|
||||
|
||||
|
@ -634,15 +640,15 @@ namespace MessagePack.Tests
|
|||
referencePacked.Position.Is(bytes.Length);
|
||||
stream.ToArray().SequenceEqual(bytes).IsTrue();
|
||||
|
||||
int readSize;
|
||||
var ext = MessagePackBinary.ReadExtensionFormat(bytes, 0, out readSize);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
var ext = sequenceReader.ReadExtensionFormat();
|
||||
ext.TypeCode.Is(typeCode);
|
||||
ext.Data.SequenceEqual(target).IsTrue();
|
||||
readSize.Is(returnLength);
|
||||
ext.Data.ToArray().SequenceEqual(target).IsTrue();
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
var ext2 = CreateUnpackedReference(bytes).AsMessagePackExtendedTypeObject();
|
||||
ext2.TypeCode.Is((byte)ext.TypeCode);
|
||||
ext2.GetBody().SequenceEqual(ext.Data).IsTrue();
|
||||
ext2.GetBody().SequenceEqual(ext.Data.ToArray()).IsTrue();
|
||||
}
|
||||
|
||||
// FixExt4(-1) => seconds | [1970-01-01 00:00:00 UTC, 2106-02-07 06:28:16 UTC) range
|
||||
|
@ -675,9 +681,9 @@ namespace MessagePack.Tests
|
|||
var returnLength = MessagePackBinary.WriteDateTime(ref bytes, 0, target);
|
||||
returnLength.Is(expectedLength);
|
||||
|
||||
int readSize;
|
||||
var result = MessagePackBinary.ReadDateTime(bytes, 0, out readSize);
|
||||
readSize.Is(returnLength);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
var result = sequenceReader.ReadDateTime();
|
||||
sequenceReader.End.IsTrue();
|
||||
|
||||
result.Is(target);
|
||||
}
|
||||
|
@ -689,47 +695,44 @@ namespace MessagePack.Tests
|
|||
// Int32 can accepts UInt16
|
||||
// Int64 can accepts UInt32
|
||||
{
|
||||
int readSize;
|
||||
byte[] small = null;
|
||||
byte[] target = null;
|
||||
MessagePackBinary.WriteByte(ref small, 0, byte.MaxValue);
|
||||
MessagePackBinary.ReadInt16(small, 0, out readSize).Is(byte.MaxValue);
|
||||
new MessagePackReader(small).ReadInt16().Is(byte.MaxValue);
|
||||
MessagePackBinary.WriteInt16(ref target, 0, byte.MaxValue);
|
||||
target.SequenceEqual(small).IsTrue();
|
||||
}
|
||||
{
|
||||
int readSize;
|
||||
byte[] small = null;
|
||||
byte[] target = null;
|
||||
MessagePackBinary.WriteByte(ref small, 0, byte.MaxValue);
|
||||
MessagePackBinary.ReadInt32(small, 0, out readSize).Is(byte.MaxValue);
|
||||
new MessagePackReader(small).ReadInt32().Is(byte.MaxValue);
|
||||
MessagePackBinary.WriteInt32(ref target, 0, byte.MaxValue);
|
||||
target.SequenceEqual(small).IsTrue();
|
||||
|
||||
small = target = null;
|
||||
MessagePackBinary.WriteUInt16(ref small, 0, ushort.MaxValue);
|
||||
MessagePackBinary.ReadInt32(small, 0, out readSize).Is(ushort.MaxValue);
|
||||
new MessagePackReader(small).ReadInt32().Is(ushort.MaxValue);
|
||||
MessagePackBinary.WriteInt32(ref target, 0, ushort.MaxValue);
|
||||
target.SequenceEqual(small).IsTrue();
|
||||
}
|
||||
{
|
||||
int readSize;
|
||||
byte[] small = null;
|
||||
byte[] target = null;
|
||||
MessagePackBinary.WriteByte(ref small, 0, byte.MaxValue);
|
||||
MessagePackBinary.ReadInt64(small, 0, out readSize).Is(byte.MaxValue);
|
||||
new MessagePackReader(small).ReadInt64().Is(byte.MaxValue);
|
||||
MessagePackBinary.WriteInt64(ref target, 0, byte.MaxValue);
|
||||
target.SequenceEqual(small).IsTrue();
|
||||
|
||||
small = target = null;
|
||||
MessagePackBinary.WriteUInt16(ref small, 0, ushort.MaxValue);
|
||||
MessagePackBinary.ReadInt64(small, 0, out readSize).Is(ushort.MaxValue);
|
||||
new MessagePackReader(small).ReadInt64().Is(ushort.MaxValue);
|
||||
MessagePackBinary.WriteInt64(ref target, 0, ushort.MaxValue);
|
||||
target.SequenceEqual(small).IsTrue();
|
||||
|
||||
small = target = null;
|
||||
MessagePackBinary.WriteUInt32(ref small, 0, uint.MaxValue);
|
||||
MessagePackBinary.ReadInt64(small, 0, out readSize).Is(uint.MaxValue);
|
||||
new MessagePackReader(small).ReadInt64().Is(uint.MaxValue);
|
||||
MessagePackBinary.WriteInt64(ref target, 0, uint.MaxValue);
|
||||
target.SequenceEqual(small).IsTrue();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Resolvers;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
@ -45,9 +46,9 @@ namespace MessagePack.Tests
|
|||
|
||||
public class Int_x10Formatter : IMessagePackFormatter<int>
|
||||
{
|
||||
public int Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public int Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize) * 10;
|
||||
return reader.ReadInt32() * 10;
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, int value, IFormatterResolver formatterResolver)
|
||||
|
@ -58,9 +59,9 @@ namespace MessagePack.Tests
|
|||
|
||||
public class String_x2Formatter : IMessagePackFormatter<string>
|
||||
{
|
||||
public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public string Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var s = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
var s = reader.ReadString();
|
||||
return s + s;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using MessagePack.Formatters;
|
||||
using Nerdbank.Streams;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -45,9 +46,9 @@ namespace MessagePack.Tests
|
|||
byte[] bytes = null;
|
||||
GuidFormatter.Instance.Serialize(ref bytes, 0, original, null).Is(38);
|
||||
|
||||
int readSize;
|
||||
GuidFormatter.Instance.Deserialize(bytes, 0, null, out readSize).Is(original);
|
||||
readSize.Is(38);
|
||||
var sequenceReader = new MessagePackReader(bytes);
|
||||
GuidFormatter.Instance.Deserialize(ref sequenceReader, null).Is(original);
|
||||
sequenceReader.End.IsTrue();
|
||||
}
|
||||
{
|
||||
var c = new InClass() { MyProperty = 3414141, Guid = Guid.NewGuid() };
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using MessagePack.Resolvers;
|
||||
using SharedData;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
@ -56,7 +57,8 @@ namespace MessagePack.Tests
|
|||
{
|
||||
SimpleIntKeyData n = null;
|
||||
var bytes = serializer.Serialize(n);
|
||||
MessagePackBinary.IsNil(bytes, 0).IsTrue();
|
||||
var reader = new MessagePackReader(bytes);
|
||||
reader.IsNil.IsTrue();
|
||||
bytes.Length.Is(1);
|
||||
|
||||
serializer.Deserialize<SimpleIntKeyData>(bytes).IsNull();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
@ -38,7 +39,7 @@ namespace MessagePack.Tests
|
|||
var length = OldSpecBinaryFormatter.Instance.Serialize(ref messagePackBytes, 0, sourceBytes, StandardResolver.Instance);
|
||||
Assert.NotEmpty(messagePackBytes);
|
||||
Assert.Equal(length, messagePackBytes.Length);
|
||||
Assert.Equal(MessagePackCode.Nil, messagePackBytes[0]);
|
||||
Assert.Equal(MessagePackCode.Nil, messagePackBytes[0]);
|
||||
|
||||
var deserializedBytes = DeserializeByClassicMsgPack<byte[]>(messagePackBytes, MsgPack.Serialization.SerializationMethod.Array);
|
||||
Assert.Null(deserializedBytes);
|
||||
|
@ -70,9 +71,9 @@ namespace MessagePack.Tests
|
|||
public void DeserializeSimpleByteArray(int arrayLength)
|
||||
{
|
||||
var sourceBytes = Enumerable.Range(0, arrayLength).Select(i => unchecked((byte) i)).ToArray(); // long byte array
|
||||
var messagePackBytes = SerializeByClassicMsgPack(sourceBytes, MsgPack.Serialization.SerializationMethod.Array);
|
||||
|
||||
var deserializedBytes = OldSpecBinaryFormatter.Instance.Deserialize(messagePackBytes, 0, StandardResolver.Instance, out var readSize);
|
||||
var messagePackBytes = SerializeByClassicMsgPack(sourceBytes, MsgPack.Serialization.SerializationMethod.Array);
|
||||
var messagePackBytesReader = new MessagePackReader(messagePackBytes);
|
||||
var deserializedBytes = serializer.Deserialize<byte[]>(ref messagePackBytesReader);
|
||||
Assert.NotNull(deserializedBytes);
|
||||
Assert.Equal(sourceBytes, deserializedBytes);
|
||||
}
|
||||
|
@ -80,9 +81,9 @@ namespace MessagePack.Tests
|
|||
[Fact]
|
||||
public void DeserializeNil()
|
||||
{
|
||||
var messagePackBytes = new byte[]{ MessagePackCode.Nil };
|
||||
var messagePackReader = new MessagePackReader(new byte[] { MessagePackCode.Nil });
|
||||
|
||||
var deserializedObj = OldSpecBinaryFormatter.Instance.Deserialize(messagePackBytes, 0, StandardResolver.Instance, out var readSize);
|
||||
var deserializedObj = serializer.Deserialize<byte[]>(ref messagePackReader);
|
||||
Assert.Null(deserializedObj);
|
||||
}
|
||||
|
||||
|
@ -98,8 +99,8 @@ namespace MessagePack.Tests
|
|||
Value = Enumerable.Range(0, arrayLength).Select(i => unchecked((byte)i)).ToArray() // long byte array
|
||||
};
|
||||
var messagePackBytes = SerializeByClassicMsgPack(foo, MsgPack.Serialization.SerializationMethod.Map);
|
||||
|
||||
var deserializedFoo = serializer.Deserialize<Foo>(messagePackBytes);
|
||||
var oldSpecReader = new MessagePackReader(messagePackBytes);
|
||||
var deserializedFoo = serializer.Deserialize<Foo>(ref oldSpecReader);
|
||||
Assert.NotNull(deserializedFoo);
|
||||
Assert.Equal(foo.Id, deserializedFoo.Id);
|
||||
Assert.Equal(foo.Value, deserializedFoo.Value);
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Resolvers;
|
||||
using System;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Resolvers;
|
||||
using Xunit;
|
||||
|
||||
namespace MessagePack.Tests
|
||||
|
@ -22,7 +23,7 @@ namespace MessagePack.Tests
|
|||
|
||||
public class DummyStringFormatter : IMessagePackFormatter<string>
|
||||
{
|
||||
public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public string Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -35,7 +36,7 @@ namespace MessagePack.Tests
|
|||
|
||||
public class DummyDateTimeFormatter : IMessagePackFormatter<DateTime>
|
||||
{
|
||||
public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public DateTime Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -48,7 +49,7 @@ namespace MessagePack.Tests
|
|||
|
||||
public class DummyBinaryFormatter : IMessagePackFormatter<byte[]>
|
||||
{
|
||||
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public byte[] Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -145,7 +146,8 @@ namespace MessagePack.Tests
|
|||
|
||||
a.Is(b);
|
||||
|
||||
var r1 = defaultSerializer.Deserialize<string>(a, OldSpecResolver.Instance);
|
||||
var oldSpecReader = new MessagePackReader(a);
|
||||
var r1 = defaultSerializer.Deserialize<string>(ref oldSpecReader);
|
||||
var r2 = serializer.UnpackSingleObject(b);
|
||||
|
||||
r1.Is(r2);
|
||||
|
@ -169,7 +171,8 @@ namespace MessagePack.Tests
|
|||
|
||||
a.Is(b);
|
||||
|
||||
var r1 = defaultSerializer.Deserialize<byte[]>(a, OldSpecResolver.Instance);
|
||||
var oldSpecReader = new MessagePackReader(a);
|
||||
var r1 = defaultSerializer.Deserialize<byte[]>(ref oldSpecReader);
|
||||
var r2 = serializer.UnpackSingleObject(b);
|
||||
|
||||
r1.Is(r2);
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace MessagePack.Tests
|
|||
var bin = serializer.Serialize(new[] { bytesA, bytesB, bytesC });
|
||||
var ms = new MemoryStream(bin, 0, bin.Length, false, false);
|
||||
|
||||
var foo = serializer.Deserialize<byte[][]>(ms, true);
|
||||
var foo = serializer.Deserialize<byte[][]>(ms);
|
||||
|
||||
for (int i = 0; i < foo[0].Length; i++)
|
||||
{
|
||||
|
|
|
@ -1,9 +1,4 @@
|
|||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace MessagePack.Tests
|
||||
|
@ -29,9 +24,9 @@ namespace MessagePack.Tests
|
|||
|
||||
class NoObjectFormatter : IMessagePackFormatter<CustomClassObject>
|
||||
{
|
||||
public CustomClassObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public CustomClassObject Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var r = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
var r = reader.ReadInt32();
|
||||
return new CustomClassObject(r);
|
||||
}
|
||||
|
||||
|
@ -59,9 +54,9 @@ namespace MessagePack.Tests
|
|||
|
||||
class CustomStructObjectFormatter : IMessagePackFormatter<CustomStructObject>
|
||||
{
|
||||
public CustomStructObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public CustomStructObject Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var r = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
var r = reader.ReadInt32();
|
||||
return new CustomStructObject(r);
|
||||
}
|
||||
|
||||
|
@ -80,9 +75,9 @@ namespace MessagePack.Tests
|
|||
|
||||
class CustomEnumObjectFormatter : IMessagePackFormatter<CustomyEnumObject>
|
||||
{
|
||||
public CustomyEnumObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public CustomyEnumObject Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var r = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
var r = reader.ReadInt32();
|
||||
if (r == 0)
|
||||
{
|
||||
return CustomyEnumObject.A;
|
||||
|
@ -108,9 +103,9 @@ namespace MessagePack.Tests
|
|||
|
||||
class CustomInterfaceObjectFormatter : IMessagePackFormatter<ICustomInterfaceObject>
|
||||
{
|
||||
public ICustomInterfaceObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public ICustomInterfaceObject Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var r = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
var r = reader.ReadInt32();
|
||||
return new InheritDefault(r);
|
||||
}
|
||||
|
||||
|
@ -160,9 +155,9 @@ namespace MessagePack.Tests
|
|||
this.x = x;
|
||||
}
|
||||
|
||||
public CustomClassObjectWithArgument Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
public CustomClassObjectWithArgument Deserialize(ref MessagePackReader reader, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var r = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
var r = reader.ReadInt32();
|
||||
return new CustomClassObjectWithArgument(r);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,143 +0,0 @@
|
|||
using SharedData;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace MessagePack.Tests
|
||||
{
|
||||
public class StreamStrictTest
|
||||
{
|
||||
private MessagePackSerializer serializer = new MessagePackSerializer();
|
||||
|
||||
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 T DeserializeWithLengthPrefixExt<T>(Stream stream, IFormatterResolver resolver)
|
||||
{
|
||||
const int ExtTypeCode = 111; // sample ext code
|
||||
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(stream);
|
||||
if (header.TypeCode == ExtTypeCode)
|
||||
{
|
||||
// memo, read fully
|
||||
var buffer = new byte[1024];
|
||||
stream.Read(buffer, 0, (int)header.Length);
|
||||
|
||||
return resolver.GetFormatter<T>().Deserialize(buffer, 0, resolver, out var _);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
T DeserializeWithLengthPrefixExt2<T>(Stream stream, IFormatterResolver resolver)
|
||||
{
|
||||
const int ExtTypeCode = 111; // sample ext code
|
||||
|
||||
var header = MessagePackBinary.ReadExtensionFormat(stream);
|
||||
if (header.TypeCode == ExtTypeCode)
|
||||
{
|
||||
return serializer.Deserialize<T>(header.Data, resolver);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deserialize()
|
||||
{
|
||||
var testData = new SharedData.MyClass { MyProperty1 = int.MaxValue, MyProperty2 = int.MaxValue, MyProperty3 = int.MaxValue };
|
||||
|
||||
var ms = new MemoryStream();
|
||||
|
||||
SerializeWithLengthPrefixExt(ms, testData, Resolvers.ContractlessStandardResolver.Instance);
|
||||
|
||||
ms.Position = 0;
|
||||
var data = DeserializeWithLengthPrefixExt<MyClass>(ms, Resolvers.ContractlessStandardResolver.Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deserialize2()
|
||||
{
|
||||
var testData = new SharedData.MyClass { MyProperty1 = int.MaxValue, MyProperty2 = 99, MyProperty3 = 1341 };
|
||||
|
||||
Stream ms = new MemoryStream();
|
||||
|
||||
SerializeWithLengthPrefixExt(ms, testData, Resolvers.ContractlessStandardResolver.Instance);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
ms = new FixedSizeReadStream(ms, 2);
|
||||
|
||||
var data = DeserializeWithLengthPrefixExt2<MyClass>(ms, Resolvers.ContractlessStandardResolver.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
public class FixedSizeReadStream : Stream
|
||||
{
|
||||
readonly Stream stream;
|
||||
readonly int readSize;
|
||||
|
||||
public FixedSizeReadStream(Stream stream, int readSize)
|
||||
{
|
||||
this.stream = stream;
|
||||
this.readSize = readSize;
|
||||
}
|
||||
|
||||
public override bool CanRead => throw new NotImplementedException();
|
||||
|
||||
public override bool CanSeek => throw new NotImplementedException();
|
||||
|
||||
public override bool CanWrite => throw new NotImplementedException();
|
||||
|
||||
public override long Length => throw new NotImplementedException();
|
||||
|
||||
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return stream.Read(buffer, offset, Math.Min(readSize, count));
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,9 +17,9 @@ namespace MessagePack.Tests
|
|||
byte[] bin = null;
|
||||
BinaryGuidFormatter.Instance.Serialize(ref bin, 0, guid, null).Is(18);
|
||||
|
||||
int readSize;
|
||||
var nguid = BinaryGuidFormatter.Instance.Deserialize(bin, 0, null, out readSize);
|
||||
readSize.Is(18);
|
||||
var sequenceReader = new MessagePackReader(bin);
|
||||
var nguid = BinaryGuidFormatter.Instance.Deserialize(ref sequenceReader, null);
|
||||
Assert.True(sequenceReader.End);
|
||||
|
||||
guid.Is(nguid);
|
||||
}
|
||||
|
@ -31,9 +31,9 @@ namespace MessagePack.Tests
|
|||
byte[] bin = null;
|
||||
BinaryDecimalFormatter.Instance.Serialize(ref bin, 0, d, null).Is(18);
|
||||
|
||||
int readSize;
|
||||
var nd = BinaryDecimalFormatter.Instance.Deserialize(bin, 0, null, out readSize);
|
||||
readSize.Is(18);
|
||||
var sequenceReader = new MessagePackReader(bin);
|
||||
var nd = BinaryDecimalFormatter.Instance.Deserialize(ref sequenceReader, null);
|
||||
Assert.True(sequenceReader.End);
|
||||
|
||||
d.Is(nd);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче