Skip to content

Commit

Permalink
Deserialize native type on when it is explicity requested.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcweber committed Mar 30, 2023
1 parent 2ce44c6 commit 869895c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public NativeTypeDeserializerConverterFactory(Func<JValue, IGremlinQueryEnvironm
_deserializer = deserializer;
}

public IConverter<TSource, TTarget>? TryCreate<TSource, TTarget>(IGremlinQueryEnvironment environment) => typeof(TSource) == typeof(JValue) && typeof(TTarget).IsAssignableFrom(typeof(TNative))
public IConverter<TSource, TTarget>? TryCreate<TSource, TTarget>(IGremlinQueryEnvironment environment) => typeof(TSource) == typeof(JValue) && typeof(TTarget) == typeof(TNative)
? (IConverter<TSource, TTarget>?)Activator.CreateInstance(typeof(NativeTypeDeserializerConverter<>).MakeGenericType(typeof(TNative), typeof(TTarget)), _deserializer, environment)
: default;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
Value: 42
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
originalString
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ namespace ExRam.Gremlinq.Support.NewtonsoftJson.Tests
{
public class GraphsonSupportTest : GremlinqTestBase
{
private readonly struct NativeType
{
public NativeType(int value)
{
Value = value;
}

public int Value { get; }
}

private sealed class MetaPoco
{
public string? MetaKey { get; set; }
Expand Down Expand Up @@ -203,6 +213,32 @@ public void JToken_Load_does_not_reuse()
[Fact]
public Task VertexPropertyWithDateTimeOffset() => Verify<VertexProperty<string, PropertyValidity>>(JToken.Parse("[ { \"id\": 166, \"value\": \"bob\", \"label\": \"Name\", \"properties\": { \"ValidFrom\": 1548112365431 } } ]"));

[Fact]
public async Task NativeType_is_deserialized()
{
var data = JToken.Parse("[ 42 ]");

await Verify<NativeType>(data, _environment
.RegisterNativeType(
(nativeType, env, recurse) => 42,
(jValue, env, recurse) => jValue.Type is JTokenType.Integer
? new NativeType(jValue.Value<int>())
: default));
}

[Fact]
public async Task NativeType_is_only_deserialized_when_requested_explicitly()
{
var data = JToken.Parse("[ \"originalString\" ]");

await Verify<object>(data, _environment
.RegisterNativeType(
(nativeType, env, recurse) => 42,
(jValue, env, recurse) => jValue.Type is JTokenType.Integer
? new NativeType(jValue.Value<int>())
: default));
}

private static JToken GetJson(string name)
{
return JToken.Parse(new StreamReader(File.OpenRead($"../../../../files/GraphSon/{name}.json")).ReadToEnd());
Expand Down

0 comments on commit 869895c

Please sign in to comment.