Skip to content

Commit

Permalink
v2.2.1, fix for ToObject()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-barg committed Jul 12, 2023
1 parent e69c3e6 commit 53db7dc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\sgKey.snk</AssemblyOriginatorKeyFile>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\sgKey.snk</AssemblyOriginatorKeyFile>

Expand Down
7 changes: 7 additions & 0 deletions src/Jsonata.Net.Native.Tests/ToObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ public static List<TestCaseData> GetTestCases()
new TestData("[{'a': 'b'}]", new List<object> { new Dictionary<string, object> { { "a", "b" } } }),
new TestData("[{'a': 'c'}]", new List<object> { new Dictionary<string, string> { { "a", "c" } } }),
new TestData("[{'a': 'd'}]", new List<Dictionary<string, string>> { new Dictionary<string, string> { { "a", "d" } } }),
new TestData("{'foo': 'goo', 'bar': 10}", new TestObj() { foo = "goo", bar = 10 }),
};

return tests
.Select(v => new TestCaseData(v) { TestName = v.json })
.ToList();
}

private sealed class TestObj
{
public string foo { get; set; } = default!;
public int? bar { get; set; }
}
}
}
10 changes: 5 additions & 5 deletions src/Jsonata.Net.Native/Json/JToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,12 @@ public T ToObject<T>()
throw new ArgumentException($"Failed to create instance of class {type.Name}: {ex.Message}", ex);
}

Dictionary<string, PropertyInfo> restultProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
Dictionary<string, PropertyInfo> resultProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(pi => pi.Name);

JObject thisObj = (JObject)this;

foreach (KeyValuePair<string, PropertyInfo> resultProperty in restultProperties)
foreach (KeyValuePair<string, PropertyInfo> resultProperty in resultProperties)
{
if (!thisObj.Properties.TryGetValue(resultProperty.Key, out JToken? thisProperty))
{
Expand All @@ -410,7 +410,7 @@ public T ToObject<T>()
object? value;
try
{
value = thisProperty.ToObject(resultProperty.GetType());
value = thisProperty.ToObject(resultProperty.Value.PropertyType);
}
catch (Exception ex)
{
Expand All @@ -427,9 +427,9 @@ public T ToObject<T>()
}
}

if (thisObj.Keys.Except(restultProperties.Keys).Any())
if (thisObj.Keys.Except(resultProperties.Keys).Any())
{
throw new ArgumentException($"Specified unknown properties: {String.Join(",", thisObj.Keys.Except(restultProperties.Keys))}");
throw new ArgumentException($"Specified unknown properties: {String.Join(",", thisObj.Keys.Except(resultProperties.Keys))}");
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Jsonata.Net.Native/Jsonata.Net.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
-->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\sgKey.snk</AssemblyOriginatorKeyFile>

Expand Down

0 comments on commit 53db7dc

Please sign in to comment.