Skip to content

Commit

Permalink
Cache an inner serializer that is not really dependent on the type ar…
Browse files Browse the repository at this point in the history
…guments.
  • Loading branch information
danielcweber committed Oct 26, 2024
1 parent 47853da commit 9facd8f
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Newtonsoft.Json.Serialization;
using System.Reflection;
using ExRam.Gremlinq.Core.GraphElements;
using System.Runtime.CompilerServices;

namespace ExRam.Gremlinq.Support.NewtonsoftJson
{
Expand Down Expand Up @@ -118,13 +119,19 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
[ThreadStatic]
private static bool _canConvert;

public GraphsonJsonSerializer(IGremlinQueryEnvironment environment)
private static readonly ConditionalWeakTable<IGremlinQueryEnvironment, GraphsonJsonSerializer> Serializers = new();

private GraphsonJsonSerializer(IGremlinQueryEnvironment environment)
{
DefaultValueHandling = DefaultValueHandling.Ignore;
ContractResolver = new GremlinContractResolver(environment.Model);
Converters.Add(new JTokenConverterConverter(environment));
}

public static GraphsonJsonSerializer From(IGremlinQueryEnvironment environment) => Serializers.GetValue(
environment,
static environment => new GraphsonJsonSerializer(environment));

public T? Deserialize<T>(JToken token)
{
_canConvert = false;
Expand All @@ -140,7 +147,7 @@ private sealed class NewtonsoftJsonSerializerConverter<TSource, TTarget> : IConv

public NewtonsoftJsonSerializerConverter(IGremlinQueryEnvironment environment)
{
_serializer = new GraphsonJsonSerializer(environment);
_serializer = GraphsonJsonSerializer.From(environment);
}

public bool TryConvert(TSource source, ITransformer defer, ITransformer recurse, [NotNullWhen(true)] out TTarget? value)
Expand Down

0 comments on commit 9facd8f

Please sign in to comment.