Skip to content

Commit

Permalink
1.3.0, add MessagePackFormatterAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jun 8, 2017
1 parent ca47068 commit 32a2a2a
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 23 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,10 @@ Extension Point(IFormatterResolver)
| Resovler Name | Description |
| --- | --- |
| BuiltinResolver | Builtin primitive and standard classes resolver. It includes primitive(int, bool, string...) and there nullable, array and list. and some extra builtin types(Guid, Uri, BigInteger, etc...). |
| StandardResolver | Composited resolver . It resolves in the following order `builtin -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive object`. This is the default of MessagePackSerializer. |
| StandardResolver | Composited resolver . It resolves in the following order `builtin -> attribute -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive object`. This is the default of MessagePackSerializer. |
| ContractlessStandardResolver | Composited `StandardResolver` -> `DynamicContractlessObjectResolver`. It enables contractless serialization. |
| PrimitiveObjectResolver | MessagePack primitive object resolver. It is used fallback in `object` type and supports `bool`, `char`, `sbyte`, `byte`, `short`, `int`, `long`, `ushort`, `uint`, `ulong`, `float`, `double`, `DateTime`, `string`, `byte[]`, `ICollection`, `IDictionary`. |
| AttributeFormatterResolver | Get formatter from `[MessagePackFormatter]` attribute. |
| CompositeResolver | Singleton helper of setup custom resolvers. You can use `Register` or `RegisterAndSetAsDefault` API. |
| NativeDateTimeResolver | Serialize by .NET native DateTime binary format. |
| OldSpecResolver | str and bin serialize/deserialize follows old messagepack spec(use raw format) |
Expand Down Expand Up @@ -838,6 +839,40 @@ internal static class SampleCustomResolverGetFormatterHelper
}
```

MessaegPackFormatterAttribute
---
MessaegPackFormatterAttribute is lightweight extension point of class, struct, interface, enum. This is like JSON.NET's JsonConverterAttribute. For example, serialize private field.

```csharp
[MessagePackFormatter(typeof(CustomObjectFormatter))]
public class CustomObject
{
string internalId;

public CustomObject()
{
this.internalId = Guid.NewGuid().ToString();
}

// serialize/deserialize internal field.
class CustomObjectFormatter : IMessagePackFormatter<CustomObject>
{
public int Serialize(ref byte[] bytes, int offset, CustomObject value, IFormatterResolver formatterResolver)
{
return formatterResolver.GetFormatterWithVerify<string>().Serialize(ref bytes, offset, value.internalId, formatterResolver);
}

public CustomObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
{
var id = formatterResolver.GetFormatterWithVerify<string>().Deserialize(bytes, offset, formatterResolver, out readSize);
return new CustomObject { internalId = id };
}
}
}
```

Formatter is retrieved by `AttributeFormatterResolver`, it is included in `StandardResolver`.

for Unity
---
You can install by package and includes source code. If build target as PC, you can use as is but if build target uses IL2CPP, you can not use `Dynamic***Resolver` so use pre-code generation. Please see [pre-code generation section](https://github.com/neuecc/MessagePack-CSharp#pre-code-generationunityxamarin-supports).
Expand Down Expand Up @@ -935,6 +970,7 @@ MessagePack.Resolvers.CompositeResolver.RegisterAndSetAsDefault(

// finally, use builtin/primitive resolver(don't use StandardResolver, it includes dynamic generation)
MessagePack.Resolvers.BuiltinResolver.Instance,
AttributeFormatterResolver.Instance,
MessagePack.Resolvers.PrimitiveObjectResolver.Instance
);
```
Expand Down
4 changes: 2 additions & 2 deletions nuget/MessagePack.AspNetCoreMvcFormatter.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MessagePack.AspNetCoreMvcFormatter</id>
<version>1.2.3</version>
<version>1.3.0</version>
<title>ASP.NET Core MVC Input/Output MessagePack formatter</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand All @@ -13,7 +13,7 @@
<tags>MsgPack, MessagePack, Serialization, Formatter, Serializer, aspnetcore, aspnetcoremvc</tags>
<dependencies>
<group targetFramework=".NETStandard1.4">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="Microsoft.AspNetCore.Mvc.Abstractions" version="1.2.0" />
</group>
</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions nuget/MessagePack.ImmutableCollection.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MessagePack.ImmutableCollection</id>
<version>1.2.3</version>
<version>1.3.0</version>
<title>MessagePack for C# Extension Support for ImmutableCollection</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand All @@ -17,15 +17,15 @@
</frameworkAssemblies>
<dependencies>
<group targetFramework=".NETFramework4.7">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="System.Collections.Immutable" version="1.3.1" />
</group>
<group targetFramework=".NETFramework4.5">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="System.Collections.Immutable" version="1.3.1" />
</group>
<group targetFramework=".NETStandard1.4">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="System.Collections.Immutable" version="1.3.1" />
</group>
</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions nuget/MessagePack.ReactiveProperty.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MessagePack.ReactiveProperty</id>
<version>1.2.3</version>
<version>1.3.0</version>
<title>MessagePack for C# Extension Support for ReactiveProperty</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand All @@ -17,15 +17,15 @@
</frameworkAssemblies>
<dependencies>
<group targetFramework=".NETFramework4.7">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="ReactiveProperty" version="3.5.1" />
</group>
<group targetFramework=".NETFramework4.5">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="ReactiveProperty" version="3.5.1" />
</group>
<group targetFramework=".NETStandard1.4">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
<dependency id="ReactiveProperty" version="3.5.1" />
</group>
</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions nuget/MessagePack.UnityShims.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MessagePack.UnityShims</id>
<version>1.2.3</version>
<version>1.3.0</version>
<title>MessagePack for C# Extension Support for Unity(add pseudo Vector type and fast Vectory[] extension formatter)</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand All @@ -17,13 +17,13 @@
</frameworkAssemblies>
<dependencies>
<group targetFramework=".NETFramework4.7">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
</group>
<group targetFramework=".NETFramework4.5">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
</group>
<group targetFramework=".NETStandard1.4">
<dependency id="MessagePack" version="1.2.3" />
<dependency id="MessagePack" version="1.3.0" />
</group>
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion nuget/MessagePack.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MessagePack</id>
<version>1.2.3</version>
<version>1.3.0</version>
<title>MessagePack for C#</title>
<authors>neuecc</authors>
<owners>neuecc</owners>
Expand Down
10 changes: 5 additions & 5 deletions nuget/push.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
nuget push MessagePack.1.2.3.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.ImmutableCollection.1.2.3.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.ReactiveProperty.1.2.3.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.UnityShims.1.2.3.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.AspNetCoreMvcFormatter.1.2.3.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.1.3.0.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.ImmutableCollection.1.3.0.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.ReactiveProperty.1.3.0.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.UnityShims.1.3.0.nupkg -Source https://www.nuget.org/api/v2/package
nuget push MessagePack.AspNetCoreMvcFormatter.1.3.0.nupkg -Source https://www.nuget.org/api/v2/package
REM nuget push MessagePackAnalyzer.1.6.0.nupkg -Source https://www.nuget.org/api/v2/package
27 changes: 27 additions & 0 deletions sandbox/Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ public class BarClass : IUnionSample
}


[MessagePackFormatter(typeof(CustomObjectFormatter))]
public class CustomObject
{
string internalId;

public CustomObject()
{
this.internalId = Guid.NewGuid().ToString();
}

// serialize/deserialize internal field.
class CustomObjectFormatter : IMessagePackFormatter<CustomObject>
{
public int Serialize(ref byte[] bytes, int offset, CustomObject value, IFormatterResolver formatterResolver)
{
return formatterResolver.GetFormatterWithVerify<string>().Serialize(ref bytes, offset, value.internalId, formatterResolver);
}

public CustomObject Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
{
var id = formatterResolver.GetFormatterWithVerify<string>().Deserialize(bytes, offset, formatterResolver, out readSize);
return new CustomObject { internalId = id };
}
}
}


class Program
{
static void Main(string[] args)
Expand Down
2 changes: 1 addition & 1 deletion sandbox/SharedData/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MessagePack" version="1.2.3" targetFramework="net461" />
<package id="MessagePack" version="1.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net461" />
</packages>
1 change: 1 addition & 0 deletions src/MessagePack.UnityClient/MessagePack.UnityClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<Compile Include="Assets\Scripts\MessagePack\MessagePackSerializer.NonGeneric.cs" />
<Compile Include="Assets\Scripts\MessagePack\MessagePackSerializer.cs" />
<Compile Include="Assets\Scripts\MessagePack\Nil.cs" />
<Compile Include="Assets\Scripts\MessagePack\Resolvers\AttributeFormatterResolver.cs" />
<Compile Include="Assets\Scripts\MessagePack\Resolvers\BuiltinResolver.cs" />
<Compile Include="Assets\Scripts\MessagePack\Resolvers\CompositeResolver.cs" />
<Compile Include="Assets\Scripts\MessagePack\Resolvers\DynamicEnumAsStringResolver.cs" />
Expand Down
11 changes: 11 additions & 0 deletions src/MessagePack/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ public class SerializationConstructorAttribute : Attribute
{

}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum, AllowMultiple = false, Inherited = true)]
public class MessagePackFormatterAttribute : Attribute
{
public Type FormatterType { get; private set; }

public MessagePackFormatterAttribute(Type formatterType)
{
this.FormatterType = formatterType;
}
}
}
40 changes: 40 additions & 0 deletions src/MessagePack/Resolvers/AttributeFormatterResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using MessagePack.Formatters;
using System;
using System.Reflection;

namespace MessagePack.Resolvers
{
/// <summary>
/// Get formatter from [MessaegPackFromatter] attribute.
/// </summary>
public class AttributeFormatterResolver : IFormatterResolver
{
public static IFormatterResolver Instance = new AttributeFormatterResolver();

AttributeFormatterResolver()
{

}

public IMessagePackFormatter<T> GetFormatter<T>()
{
return FormatterCache<T>.formatter;
}

static class FormatterCache<T>
{
public static readonly IMessagePackFormatter<T> formatter;

static FormatterCache()
{
var attr = typeof(T).GetTypeInfo().GetCustomAttribute<MessagePackFormatterAttribute>();
if (attr == null)
{
return;
}

formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(attr.FormatterType);
}
}
}
}
4 changes: 3 additions & 1 deletion src/MessagePack/Resolvers/StandardResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace MessagePack.Resolvers
{
/// <summary>
/// Default composited resolver, builtin -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object.
/// Default composited resolver, builtin -> attribute -> dynamic enum -> dynamic generic -> dynamic union -> dynamic object -> primitive.
/// </summary>
public class StandardResolver : IFormatterResolver
{
Expand All @@ -13,6 +13,8 @@ public class StandardResolver : IFormatterResolver
{
BuiltinResolver.Instance, // Try Builtin

AttributeFormatterResolver.Instance, // Try use [MessagePackFormatter]

#if !NETSTANDARD1_4
MessagePack.Unity.UnityResolver.Instance,
#endif
Expand Down
1 change: 1 addition & 0 deletions tests/MessagePack.Tests/MessagePack.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<Compile Include="PrimitivelikeFormatterTest.cs" />
<Compile Include="PrimitiveResolverTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpecifiedFormatterResolverTest.cs" />
<Compile Include="UnionResolverTest.cs" />
<Compile Include="Utils\ChainingAssertion.Ext.cs" />
<Compile Include="Utils\ChainingAssertion.Xunit.cs" />
Expand Down

0 comments on commit 32a2a2a

Please sign in to comment.