Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add System.Half support for .NET 5.0 #1108

Merged
merged 7 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ These types can serialize by default:
* Primitives (`int`, `string`, etc...), `Enum`s, `Nullable<>`, `Lazy<>`
* `TimeSpan`, `DateTime`, `DateTimeOffset`
* `Guid`, `Uri`, `Version`, `StringBuilder`
* `BigInteger`, `Complex`
* `BigInteger`, `Complex`, `Half`
* `Array[]`, `Array[,]`, `Array[,,]`, `Array[,,,]`, `ArraySegment<>`, `BitArray`
* `KeyValuePair<,>`, `Tuple<,...>`, `ValueTuple<,...>`
* `ArrayList`, `Hashtable`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,28 @@ public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions
return (T)Type.GetType(reader.ReadString(), throwOnError: true);
}
}

#if NET5_0

public sealed class HalfFormatter : IMessagePackFormatter<Half>
{
public static readonly IMessagePackFormatter<Half> Instance = new HalfFormatter();

private HalfFormatter()
{
}

public void Serialize(ref MessagePackWriter writer, Half value, MessagePackSerializerOptions options)
{
writer.Write((float)value);
AArnott marked this conversation as resolved.
Show resolved Hide resolved
}

public Half Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
return (Half)reader.ReadSingle();
AArnott marked this conversation as resolved.
Show resolved Hide resolved
}
}

#endif

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ internal static class BuiltinResolverGetFormatterHelper
{ typeof(System.Numerics.BigInteger?), new StaticNullableFormatter<System.Numerics.BigInteger>(BigIntegerFormatter.Instance) },
{ typeof(System.Numerics.Complex), ComplexFormatter.Instance },
{ typeof(System.Numerics.Complex?), new StaticNullableFormatter<System.Numerics.Complex>(ComplexFormatter.Instance) },

#if NET5_0
{ typeof(System.Half), HalfFormatter.Instance },
#endif
};

internal static object GetFormatter(Type t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,16 @@ public void UriTest_Relative()
var relative = new Uri("/me/", UriKind.Relative);
this.Convert(relative).ToString().Is("/me/");
}

#if NET5_0

[Fact]
public void HalfTest()
{
Convert((Half)1.34f).Is((Half)1.34f);
Convert((Half)0).Is((Half)0);
}

#endif
}
}
2 changes: 1 addition & 1 deletion src/MessagePack/MessagePack.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp2.1;net5.0</TargetFrameworks>
<NoWarn>$(NoWarn);CS0649</NoWarn>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<DefineConstants Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">$(DefineConstants);SPAN_BUILTIN</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion tests/MessagePack.Tests/MessagePack.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>net472;netcoreapp2.1;net5.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>8.0</LangVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Expand Down