Skip to content

Commit

Permalink
Allow deserialization of Dictionary<string, object> and object for pr…
Browse files Browse the repository at this point in the history
…imitive types (JValue) (#440)
  • Loading branch information
XibrenX committed Apr 19, 2022
1 parent 7abd20e commit 28c9480
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Neo4jClient.Tests/Serialization/UserSuppliedSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Neo4jClient.Serialization;
using Newtonsoft.Json;
Expand Down Expand Up @@ -265,5 +266,29 @@ public void JsonSerializerShouldNotSerializeNeo4JIgnoreAttribute()
// Assert
Assert.Equal(expectedValue, result);
}

[Fact]
//[Description("test part of https://github.com/DotNet4Neo4j/Neo4jClient/issues/439")]
public void JsonDeserializeShouldAcceptDictionaryObjectValues()
{
// Arrange
string rawInput = "{'a': 'a', 'b': 42, 'c': true, 'd': 1.2345}";

var serializer = new CustomJsonDeserializer(new JsonConverter[] { new TypeConverterBasedJsonConverter() });

// Act
var result = serializer.Deserialize<Dictionary<string, object>>(rawInput);

// Assert
Assert.Equal(result.Count, 4);
Assert.IsType<string>(result["a"]);
Assert.Equal("a", result["a"]);
Assert.IsType<long>(result["b"]);
Assert.Equal(42l, result["b"]);
Assert.IsType<bool>(result["c"]);
Assert.Equal(true, result["c"]);
Assert.IsType<double>(result["d"]);
Assert.Equal(1.2345, result["d"]);
}
}
}
4 changes: 4 additions & 0 deletions Neo4jClient/Serialization/CommonDeserializerMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ public static object CreateAndMap(DeserializationContext context, Type type, JTo
instance = Convert.ChangeType(element.ToString(), type);
}
}
else if (type == typeof(object) && element is JValue jValue)
{
instance = jValue.Value;
}
else
{
try
Expand Down

0 comments on commit 28c9480

Please sign in to comment.