Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Dahomey.Json/JsonSerializerOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ public static void SetReadOnlyPropertyHandling(this JsonSerializerOptions option
options.GetState().ReadOnlyPropertyHandling = referenceHandling;
}

private static JsonSerializerOptionsState GetState(this JsonSerializerOptions options)
{
return (JsonSerializerOptionsState)options.GetConverter<JsonSerializerOptionsState>();
}

public static MissingMemberHandling GetMissingMemberHandling(this JsonSerializerOptions options)
{
return options.GetState().MissingMemberHandling;
Expand All @@ -79,5 +74,10 @@ public static void SetMissingMemberHandling(this JsonSerializerOptions options,
{
options.GetState().MissingMemberHandling = missingMemberHandling;
}

internal static JsonSerializerOptionsState GetState(this JsonSerializerOptions options)
{
return (JsonSerializerOptionsState)options.GetConverter<JsonSerializerOptionsState>();
}
}
}
34 changes: 32 additions & 2 deletions src/Dahomey.Json/JsonSerializerOptionsState.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using Dahomey.Json.Serialization.Conventions;
using Dahomey.Json.Serialization;
using Dahomey.Json.Serialization.Conventions;
using Dahomey.Json.Serialization.Converters.DictionaryKeys;
using Dahomey.Json.Serialization.Converters.Mappings;
using Dahomey.Json.Util;
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;

namespace Dahomey.Json
{
public class JsonSerializerOptionsState : JsonConverter<JsonSerializerOptionsState>
internal class JsonSerializerOptionsState : JsonConverter<JsonSerializerOptionsState>
{
public ObjectMappingRegistry ObjectMappingRegistry { get; }
public ObjectMappingConventionRegistry ObjectMappingConventionRegistry { get; }
Expand All @@ -16,13 +22,37 @@ public class JsonSerializerOptionsState : JsonConverter<JsonSerializerOptionsSta
public ReferenceHandling ReferenceHandling { get; set; }
public ReadOnlyPropertyHandling ReadOnlyPropertyHandling { get; set; }
public MissingMemberHandling MissingMemberHandling { get; set; }
public Utf8JsonWriterExtensions.WriteFormatedNumberValueFuncDelegate WriteFormatedNmberValueFunc { get; }
public ConcurrentDictionary<Type, object?> DefaultValues { get; }
public readonly byte[] ID_MEMBER_NAME = Encoding.ASCII.GetBytes("$id");
public readonly byte[] REF_MEMBER_NAME = Encoding.ASCII.GetBytes("$ref");
public readonly byte[] VALUES_MEMBER_NAME = Encoding.ASCII.GetBytes("$values");
public AsyncLocal<ReferenceContext> CurrentReferenceContext { get; } = new AsyncLocal<ReferenceContext>();

public JsonSerializerOptionsState(JsonSerializerOptions options)
{
ObjectMappingRegistry = new ObjectMappingRegistry(options);
ObjectMappingConventionRegistry = new ObjectMappingConventionRegistry();
DiscriminatorConventionRegistry = new DiscriminatorConventionRegistry(options);
DictionaryKeyConverterRegistry = new DictionaryKeyConverterRegistry(options);

MethodInfo? method = typeof(Utf8JsonWriter).GetMethod(
nameof(Utf8JsonWriter.WriteNumberValue),
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new[] { typeof(ReadOnlySpan<byte>) },
null);

if (method == null)
{
throw new JsonException("Unexpected");
}

WriteFormatedNmberValueFunc =
(Utf8JsonWriterExtensions.WriteFormatedNumberValueFuncDelegate)method
.CreateDelegate(typeof(Utf8JsonWriterExtensions.WriteFormatedNumberValueFuncDelegate));

DefaultValues = new ConcurrentDictionary<Type, object?>();
}

public override JsonSerializerOptionsState Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override TC Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSeria

public override void Read(ref Utf8JsonReader reader, ref TC obj, JsonSerializerOptions options)
{
using (new DepthHandler(options))
using (ReferenceHandler referenceHandler = new ReferenceHandler(options))
{
string? id = null;

Expand All @@ -57,21 +57,22 @@ public override void Read(ref Utf8JsonReader reader, ref TC obj, JsonSerializerO

reader.Read();
ReadOnlySpan<byte> memberName = reader.GetRawString();
JsonSerializerOptionsState state = options.GetState();

if (memberName.SequenceEqual(ReferenceHandler.ID_MEMBER_NAME))
if (memberName.SequenceEqual(state.ID_MEMBER_NAME))
{
reader.Read();
id = reader.GetString();

reader.Read();
memberName = reader.GetRawString();
if (!memberName.SequenceEqual(ReferenceHandler.VALUES_MEMBER_NAME))
if (!memberName.SequenceEqual(state.VALUES_MEMBER_NAME))
{
throw new JsonException("Expected $values member name");
}
reader.Read();
}
else if (memberName.SequenceEqual(ReferenceHandler.REF_MEMBER_NAME))
else if (memberName.SequenceEqual(state.REF_MEMBER_NAME))
{
reader.Read();
string? @ref = reader.GetString();
Expand All @@ -81,7 +82,7 @@ public override void Read(ref Utf8JsonReader reader, ref TC obj, JsonSerializerO
throw new JsonException($"Cannot resolve null reference");
}

object? @object = SerializationContext.Current.ReferenceHandler.ResolveReference(@ref);
object? @object = referenceHandler.ResolveReference(@ref);

if (@object == null)
{
Expand Down Expand Up @@ -129,7 +130,7 @@ public override void Read(ref Utf8JsonReader reader, ref TC obj, JsonSerializerO
obj = InstantiateCollection(workingCollection);
if (!string.IsNullOrEmpty(id))
{
SerializationContext.Current.ReferenceHandler.AddReference(obj, id);
referenceHandler.AddReference(obj, id);
}
}
else if (obj is ICollection<TI> collection)
Expand All @@ -154,28 +155,23 @@ public override void Write(Utf8JsonWriter writer, TC value, JsonSerializerOption
return;
}

using (new DepthHandler(options))
using (ReferenceHandler referenceHandler = new ReferenceHandler(options))
{
ReferenceHandler? referenceResolver = null;
if (_referenceHandling == ReferenceHandling.Ignore)
{
referenceResolver = SerializationContext.Current.ReferenceHandler;
}
else if (_referenceHandling == ReferenceHandling.Preserve)
if (_referenceHandling == ReferenceHandling.Preserve)
{
writer.WriteStartObject();

referenceResolver = SerializationContext.Current.ReferenceHandler;
string reference = referenceResolver.GetReference(value, out bool firstReference);
string reference = referenceHandler.GetReference(value, out bool firstReference);
JsonSerializerOptionsState state = options.GetState();

if (firstReference)
{
writer.WriteString(ReferenceHandler.ID_MEMBER_NAME, reference);
writer.WritePropertyName(ReferenceHandler.VALUES_MEMBER_NAME);
writer.WriteString(state.ID_MEMBER_NAME, reference);
writer.WritePropertyName(state.VALUES_MEMBER_NAME);
}
else
{
writer.WriteString(ReferenceHandler.REF_MEMBER_NAME, reference);
writer.WriteString(state.REF_MEMBER_NAME, reference);
writer.WriteEndObject();
return;
}
Expand All @@ -187,13 +183,13 @@ public override void Write(Utf8JsonWriter writer, TC value, JsonSerializerOption
{
if (_referenceHandling == ReferenceHandling.Ignore)
{
if (referenceResolver!.IsReferenced(item!))
if (referenceHandler!.IsReferenced(item!))
{
continue;
}
else
{
referenceResolver.AddReference(item!);
referenceHandler.AddReference(item!);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override TC Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSeria

public override void Read(ref Utf8JsonReader reader, ref TC obj, JsonSerializerOptions options)
{
using (new DepthHandler(options))
using (new ReferenceHandler(options))
{
if (reader.TokenType != JsonTokenType.StartObject)
{
Expand Down Expand Up @@ -94,7 +94,7 @@ public override void Write(Utf8JsonWriter writer, TC value, JsonSerializerOption
return;
}

using (new DepthHandler(options))
using (new ReferenceHandler(options))
{
writer.WriteStartObject();

Expand Down
10 changes: 5 additions & 5 deletions src/Dahomey.Json/Serialization/Converters/JsonNodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public override JsonNode Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
switch (reader.TokenType)
{
case JsonTokenType.StartObject:
using (new DepthHandler(options))
using (new ReferenceHandler(options))
{
return ReadObject(ref reader, options);
}

case JsonTokenType.StartArray:
using (new DepthHandler(options))
using (new ReferenceHandler(options))
{
return ReadArray(ref reader, options);
}
Expand Down Expand Up @@ -96,14 +96,14 @@ public override void Write(Utf8JsonWriter writer, JsonNode value, JsonSerializer
switch (value.ValueKind)
{
case JsonValueKind.Object:
using (new DepthHandler(options))
using (new ReferenceHandler(options))
{
WriteObject(writer, (JsonObject)value, options);
}
break;

case JsonValueKind.Array:
using (new DepthHandler(options))
using (new ReferenceHandler(options))
{
WriteArray(writer, (JsonArray)value, options);
}
Expand All @@ -114,7 +114,7 @@ public override void Write(Utf8JsonWriter writer, JsonNode value, JsonSerializer
break;

case JsonValueKind.Number:
writer.WriteNumberValue(Encoding.ASCII.GetBytes(value.ToString()));
writer.WriteNumberValue(options, Encoding.ASCII.GetBytes(value.ToString()));
break;

case JsonValueKind.True:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Dahomey.Json.Serialization.Converters.Mappings
{
public class AnonymousObjectMappingConventionProvider : IObjectMappingConventionProvider
{
private static readonly AnonymousObjectMappingConvention _anonymousObjectMappingConvention
private readonly AnonymousObjectMappingConvention _anonymousObjectMappingConvention
= new AnonymousObjectMappingConvention();

public IObjectMappingConvention? GetConvention(Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Dahomey.Json.Serialization.Converters.Mappings
{
public class DefaultObjectMappingConventionProvider : IObjectMappingConventionProvider
{
private static readonly DefaultObjectMappingConvention _defaultObjectMappingConvention = new DefaultObjectMappingConvention();
private readonly DefaultObjectMappingConvention _defaultObjectMappingConvention = new DefaultObjectMappingConvention();

public IObjectMappingConvention GetConvention(Type type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MemberMapping(JsonSerializerOptions options,
_options = options;
MemberInfo = memberInfo;
MemberType = memberType;
DefaultValue = Default.Value(memberType);
DefaultValue = Default.Value(options, memberType);
}

public MemberMapping<T> SetMemberName(string memberName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Dahomey.Json.Serialization.Converters.Mappings
{
public class StandardObjectMappingConventionProvider : IObjectMappingConventionProvider
{
private static readonly StandardObjectMappingConvention _standardObjectMappingConvention = new StandardObjectMappingConvention();
private readonly StandardObjectMappingConvention _standardObjectMappingConvention = new StandardObjectMappingConvention();

public IObjectMappingConvention? GetConvention(Type type)
{
Expand Down
Loading