Skip to content

Commit

Permalink
Ugh! I hate having to do this
Browse files Browse the repository at this point in the history
* both of the leading MsgPack implementations for .NET have problems, either ser/deser [issues](msgpack/msgpack-cli#320) or complexity in custom-mapping our TF attributes to their representation
* trying to create a super-simple, limited-purpose MP impl for our needs
  • Loading branch information
ebekker committed Mar 30, 2019
1 parent af08274 commit 679f6ad
Show file tree
Hide file tree
Showing 20 changed files with 2,498 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/MsgPackSharp.CLI/MsgPackSharp.CLI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MsgPackSharp\MsgPackSharp.csproj" />
<ProjectReference Include="..\Terrascape.LocalProvider\Terrascape.LocalProvider.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.JSON" Version="12.0.1" />
</ItemGroup>

</Project>
435 changes: 435 additions & 0 deletions src/MsgPackSharp.CLI/Program.cs

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions src/MsgPackSharp/Converters/ArrayConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace MsgPackSharp.Converters
{
public class ArrayConverter : IConverter
{
public static readonly ArrayConverter Instance = new ArrayConverter();

public bool CanDecode(Type type)
{
return type.IsArray
|| (type.GetConstructor(Type.EmptyTypes) != null
&& ((Util.GetSubclassOfGenericTypeDefinition(typeof(IList<>), type) != null)
|| typeof(IList).IsAssignableFrom(type)));
}

public bool CanEncode(Type type)
{
return typeof(IEnumerable).IsAssignableFrom(type);
}

public object Decode(IConverterContext ctx, Type type, MPObject mpo)
{
if (mpo.Type == MPType.Nil)
if (!type.IsValueType)
return null;
else
throw new MPConversionException(type, mpo,
message: $"cannot return null value for target type [{type.FullName}]");

if (mpo.Type == MPType.Array)
{
var mpoArray = (IList<MPObject>)mpo.Value;

if (type.IsArray)
{
var itemType = type.GetElementType();
var arr = Array.CreateInstance(itemType, mpoArray.Count);
var ndx = 0;
foreach (var mpoItem in mpoArray)
{
var item = ctx.Decode(itemType, mpoItem);
arr.SetValue(item, ndx++);
}
return arr;
}

var gt = Util.GetSubclassOfGenericTypeDefinition(typeof(IList<>), type);
if (gt != null)
{
var itemType = gt.GenericTypeArguments[0];
var arr = Activator.CreateInstance(type);
var mth = type.GetMethod(nameof(IList<object>.Add), new[] { itemType });
foreach (var mpoItem in mpoArray)
{
var item = ctx.Decode(itemType, mpoItem);
mth.Invoke(arr, new[] { item });
}
return arr;
}

if (typeof(IList).IsAssignableFrom(type))
{
var arr = (IList)Activator.CreateInstance(type);
foreach (var mpoItem in mpoArray)
{
var item = ctx.Decode(typeof(object), mpoItem);
arr.Add(item);
}
return arr;
}
}

throw new MPConversionException(type, mpo,
message: $"could not decode [{mpo.Type}] as array object");
}

public MPObject Encode(IConverterContext ctx, Type type, object obj)
{
if (obj == null)
return MPObject.Nil;

var itemType = typeof(object);
var gt = Util.GetSubclassOfGenericTypeDefinition(typeof(IList<>), type);
if (type.IsArray)
{
itemType = type.GetElementType();
}
else if (gt != null)
{
itemType = gt.GenericTypeArguments[0];
}

var arr = new List<MPObject>();
var enm = (IEnumerable)obj;
foreach (var item in enm)
arr.Add(ctx.Encode(itemType, item));

return new MPObject(MPType.Array, arr);
}
}
}
Loading

0 comments on commit 679f6ad

Please sign in to comment.