Skip to content

Commit 73ffa79

Browse files
committed
some fixes
1 parent 8741ce9 commit 73ffa79

File tree

10 files changed

+21
-28
lines changed

10 files changed

+21
-28
lines changed

JsonKnownTypes.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonKnownTypes", "src\JsonK
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonKnownTypes.UnitTests", "tests\JsonKnownTypes.UnitTests\JsonKnownTypes.UnitTests.csproj", "{3DE82BA5-F66A-4E39-AC18-427D22778E32}"
99
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonKnownTypes.Benchmark", "tests\JsonKnownTypes.Benchmark\JsonKnownTypes.Benchmark.csproj", "{F8736333-CFCC-4C27-9199-7EC2134AE462}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{3DE82BA5-F66A-4E39-AC18-427D22778E32}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{3DE82BA5-F66A-4E39-AC18-427D22778E32}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{3DE82BA5-F66A-4E39-AC18-427D22778E32}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{F8736333-CFCC-4C27-9199-7EC2134AE462}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{F8736333-CFCC-4C27-9199-7EC2134AE462}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{F8736333-CFCC-4C27-9199-7EC2134AE462}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{F8736333-CFCC-4C27-9199-7EC2134AE462}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# JsonKnownTypes .Net Standard
2-
[![nuget](https://img.shields.io/badge/nuget-v0.4.0-orange?style=flat-square)](https://www.nuget.org/packages/JsonKnownTypes)
2+
[![nuget](https://img.shields.io/badge/nuget-v0.4.1-orange?style=flat-square)](https://www.nuget.org/packages/JsonKnownTypes)
33
[![downloads](https://img.shields.io/nuget/dt/JsonKnownTypes?style=flat-square)](https://www.nuget.org/packages/JsonKnownTypes)
44
[![lisence](https://img.shields.io/badge/lisence-MIT-green?style=flat-square)](https://github.com/dmitry-bym/JsonKnownTypes/blob/master/LICENSE)
55

resources/json.net.png

12.2 KB
Loading

resources/v0.3.0.png

5.5 KB
Loading

resources/v0.4.1.png

11.6 KB
Loading

src/JsonKnownTypes/DiscriminatorValues.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Linq.Expressions;
53
using JsonKnownTypes.Exceptions;
64

75
namespace JsonKnownTypes
@@ -11,14 +9,12 @@ internal class DiscriminatorValues
119
public string FieldName { get; }
1210
private Dictionary<string, Type> DiscriminatorToType { get; }
1311
private Dictionary<Type, string> TypeToDiscriminator { get; }
14-
private Dictionary<Type, Func<object>> TypeToConstructor { get; }
1512

1613
public DiscriminatorValues(string fieldName)
1714
{
1815
FieldName = fieldName;
1916
DiscriminatorToType = new Dictionary<string, Type>();
2017
TypeToDiscriminator = new Dictionary<Type, string>();
21-
TypeToConstructor = new Dictionary<Type, Func<object>>();
2218
}
2319

2420
public int Count
@@ -46,25 +42,8 @@ public void AddType(Type type, string discriminator)
4642
if (DiscriminatorToType.ContainsKey(discriminator))
4743
throw new JsonKnownTypesException($"{discriminator} discriminator already in use");
4844

49-
TypeToConstructor.Add(type, GenerateConstructor(type));
5045
TypeToDiscriminator.Add(type, discriminator);
5146
DiscriminatorToType.Add(discriminator, type);
5247
}
53-
54-
55-
public object CreateInstance(Type type)
56-
=> TypeToConstructor[type]();
57-
58-
private static Func<object> GenerateConstructor(Type type)
59-
{
60-
var constructor = type.GetConstructors().FirstOrDefault(x => x.GetParameters().Length == 0)
61-
?? throw new JsonKnownTypesException($"{type.Name} type has no constructor without parameters");
62-
63-
var newExpr = Expression.New(constructor);
64-
65-
var convertExpr = Expression.Convert(newExpr, typeof(object));
66-
var lambda = Expression.Lambda<Func<object>>(convertExpr);
67-
return lambda.Compile();
68-
}
6948
}
7049
}

src/JsonKnownTypes/JsonKnownTypes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<PropertyGroup>
1212
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
13-
<Version>0.4.0</Version>
13+
<Version>0.4.1</Version>
1414
<Authors>Dmitry Kaznacheev</Authors>
1515
<PackageProjectUrl>https://github.com/dmitry-bym/JsonKnownTypes</PackageProjectUrl>
1616
<PackageLicenseUrl>https://github.com/dmitry-bym/JsonKnownTypes/blob/master/LICENSE</PackageLicenseUrl>

src/JsonKnownTypes/JsonKnownTypesConverter.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ private readonly DiscriminatorValues _typesDiscriminatorValues
1717
public override bool CanConvert(Type objectType)
1818
=> _typesDiscriminatorValues.Contains(objectType);
1919

20+
private readonly ThreadLocal<bool> _isInRead = new ThreadLocal<bool>();
21+
22+
public override bool CanRead { get => !_isInRead.Value; }
23+
2024
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
2125
{
2226
if (reader.TokenType == JsonToken.Null) return null;
@@ -31,10 +35,14 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3135
if (_typesDiscriminatorValues.TryGetType(discriminator, out var typeForObject))
3236
{
3337
var jsonReader = jObject.CreateReader();
34-
var target = _typesDiscriminatorValues.CreateInstance(typeForObject);
38+
39+
_isInRead.Value = true;
40+
41+
var obj = serializer.Deserialize(jsonReader, typeForObject);
3542

36-
serializer.Populate(jsonReader, target);
37-
return target;
43+
_isInRead.Value = false;
44+
45+
return obj;
3846
}
3947

4048
throw new JsonKnownTypesException($"{discriminator} discriminator is not registered for {nameof(T)} type");

JsonKnownTypes.Benchmark/JsonKnownTypes.Benchmark.csproj renamed to tests/JsonKnownTypes.Benchmark/JsonKnownTypes.Benchmark.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="AutoFixture" Version="4.11.0" />
910
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
1011
</ItemGroup>
1112

1213
<ItemGroup>
13-
<ProjectReference Include="..\src\JsonKnownTypes\JsonKnownTypes.csproj" />
14-
<ProjectReference Include="..\tests\JsonKnownTypes.UnitTests\JsonKnownTypes.UnitTests.csproj" />
14+
<ProjectReference Include="..\..\src\JsonKnownTypes\JsonKnownTypes.csproj" />
1515
</ItemGroup>
1616

1717
</Project>

0 commit comments

Comments
 (0)