Skip to content

Commit 46a580a

Browse files
mjuenmjuen
authored andcommitted
Merge branch 'master' of https://github.com/simonmau/enum_ext
2 parents 9dd76b4 + 3907d2f commit 46a580a

File tree

6 files changed

+132
-4
lines changed

6 files changed

+132
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Newtonsoft.Json;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace Enum.Ext.Tests
8+
{
9+
public class ConvertTests
10+
{
11+
[SetUp]
12+
public void InitWeekday()
13+
{
14+
Initialize.InitStaticFields<Weekday>();
15+
}
16+
17+
[Test]
18+
public void Test_ConvertToJson()
19+
{
20+
var tempClass = new ClassToSerialize
21+
{
22+
Weekday = Weekday.Monday
23+
};
24+
25+
var json = JsonConvert.SerializeObject(tempClass);
26+
27+
Assert.AreEqual("{\"Weekday\":1}", json);
28+
}
29+
30+
[Test]
31+
public void Test_ConvertFromJson()
32+
{
33+
var tempClass = JsonConvert.DeserializeObject<ClassToSerialize>("{\"Weekday\":1}");
34+
35+
Assert.AreEqual(Weekday.Monday, tempClass.Weekday);
36+
}
37+
38+
public class ClassToSerialize
39+
{
40+
public Weekday Weekday { get; set; }
41+
}
42+
}
43+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Reflection;
5+
using System.Text;
6+
7+
namespace Enum.Ext.Converter
8+
{
9+
public class JsonTypeSafeEnumConverter : JsonConverter
10+
{
11+
public override bool CanConvert(Type objectType)
12+
{
13+
Type currentType = objectType.BaseType;
14+
while (currentType != typeof(object))
15+
{
16+
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == typeof(TypeSafeEnum<,>))
17+
return true;
18+
19+
currentType = currentType.BaseType;
20+
}
21+
22+
return false;
23+
}
24+
25+
private Type GetKeyType(Type objectType)
26+
{
27+
Type currentType = objectType.BaseType;
28+
while (currentType != typeof(object))
29+
{
30+
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == typeof(TypeSafeEnum<,>))
31+
return currentType.GenericTypeArguments[1];
32+
33+
currentType = currentType.BaseType;
34+
}
35+
36+
return null;
37+
}
38+
39+
private MethodInfo GetBaseMethod(Type objectType)
40+
{
41+
Type currentType = objectType.BaseType;
42+
while (currentType != typeof(object))
43+
{
44+
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == typeof(TypeSafeEnum<,>))
45+
return currentType.GetMethod("GetById");
46+
47+
currentType = currentType.BaseType;
48+
}
49+
50+
return null;
51+
}
52+
53+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
54+
{
55+
if (!CanConvert(objectType))
56+
{
57+
throw new NotImplementedException();
58+
}
59+
60+
var keyType = GetKeyType(objectType);
61+
var method = GetBaseMethod(objectType);
62+
63+
return method.Invoke(null, new[] { Convert.ChangeType(reader.Value, keyType) });
64+
}
65+
66+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
67+
{
68+
var idProperty = value.GetType().GetProperty("Id");
69+
70+
var ser = new JsonSerializer();
71+
ser.Serialize(writer, idProperty.GetValue(value));
72+
}
73+
74+
public override bool CanRead => true;
75+
}
76+
}

Enum.Ext/Enum.Ext/Enum.Ext.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.0.0.1</Version>
6+
<Version>1.0.1.0</Version>
77
<Authors>Mauracher Simon, Juen Marcel</Authors>
88
<PackageProjectUrl>https://github.com/simonmau/enum_ext</PackageProjectUrl>
99
<RepositoryUrl>https://github.com/simonmau/enum_ext</RepositoryUrl>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
11-
<PackageIconUrl>https://github.com/simonmau/enum_ext/blob/master/Resources/Logo/EXT.png?raw=true</PackageIconUrl>
11+
<PackageIconUrl>https://github.com/simonmau/enum_ext/blob/master/Resources/Logo/EXT_64.png?raw=true</PackageIconUrl>
1212
</PropertyGroup>
1313

1414
<ItemGroup>
@@ -18,4 +18,8 @@
1818
</None>
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
23+
</ItemGroup>
24+
2125
</Project>

Enum.Ext/Enum.Ext/TypeSafeEnum.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System;
4+
using Newtonsoft.Json;
5+
using Enum.Ext.Converter;
46

57
namespace Enum.Ext
68
{
7-
public abstract class TypeSafeEnum<TValue, TKey>
9+
[JsonConverter(typeof(JsonTypeSafeEnumConverter))]
10+
public abstract class TypeSafeEnum<TValue, TKey> where TKey : struct
811
{
912
protected static readonly Dictionary<TKey, TypeSafeEnum<TValue, TKey>> Dictionary = new Dictionary<TKey, TypeSafeEnum<TValue, TKey>>();
1013

Enum.Ext/Enum.Ext/TypeSafeNameEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Enum.Ext
22
{
3-
public abstract class TypeSafeNameEnum<TValue, TKey> : TypeSafeEnum<TypeSafeNameEnum<TValue, TKey>, TKey>
3+
public abstract class TypeSafeNameEnum<TValue, TKey> : TypeSafeEnum<TypeSafeNameEnum<TValue, TKey>, TKey> where TKey : struct
44
{
55
protected TypeSafeNameEnum(TKey id, string name) : base(id)
66
{

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Enum.Ext provides a `TypeSafeEnum` that has a bunch of advantages compared to th
77
For example is it possible, to store additional information directly with the enum. You are also able to
88
query an enum based on the information stored with it.
99

10+
There is also a Json-Serializer Implemented, so you dont have to cast from DTOs manually.
11+
1012
### Installation
1113
https://www.nuget.org/packages/Enum.Ext/
1214

0 commit comments

Comments
 (0)