Skip to content
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

#nullable enable

namespace OpenRouter
{ /// <summary>
///
/// </summary>
public sealed partial class AlphaExtractionsSourceGenerationContext : global::System.Text.Json.Serialization.JsonSerializerContext
{
private static readonly global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver Resolver = new LazyChunkResolver();

[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver TypeInfoResolver => Resolver;


private static readonly global::System.Text.Json.JsonSerializerOptions DefaultOptions = CreateDefaultOptions();

/// <summary>
///
/// </summary>
public static AlphaExtractionsSourceGenerationContext Default { get; } = new(DefaultOptions);

private AlphaExtractionsSourceGenerationContext(global::System.Text.Json.JsonSerializerOptions options)
: base(options)
{
}

/// <inheritdoc />
protected override global::System.Text.Json.JsonSerializerOptions? GeneratedSerializerOptions => DefaultOptions;

/// <inheritdoc />
public override global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? GetTypeInfo(global::System.Type type)
{
return Resolver.GetTypeInfo(type, Options);
}

/// <summary>
/// Adds this package's converters to <paramref name="options"/>.
/// </summary>
/// <remarks>
/// A converter has to be on the options a chained resolver builds its JsonTypeInfo against,
/// and a context resolves types from every package below it. Each package contributes only
/// what it owns and calls down the chain for the rest, so the family's converter table is
/// written once rather than copied into all of them.
/// </remarks>
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public static void AddConverters(global::System.Text.Json.JsonSerializerOptions options)
{
global::OpenRouter.SourceGenerationContext.AddConverters(options);
}

private static global::System.Text.Json.JsonSerializerOptions CreateDefaultOptions()
{
var options = new global::System.Text.Json.JsonSerializerOptions
{
DefaultIgnoreCondition = global::System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
TypeInfoResolver = Resolver,
};
AddConverters(options);

return options;
}

private sealed class LazyChunkResolver : global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver
{
private readonly object _gate = new();
private readonly global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver?[] _resolvers = new global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver?[1];

public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? GetTypeInfo(
global::System.Type type,
global::System.Text.Json.JsonSerializerOptions options)
{
for (var index = 0; index < _resolvers.Length; index++)
{
var typeInfo = GetResolver(index).GetTypeInfo(type, options);
if (typeInfo is not null)
{
return typeInfo;
}
}

return null;
}

private global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver GetResolver(int index)
{
var resolver = global::System.Threading.Volatile.Read(ref _resolvers[index]);
if (resolver is not null)
{
return resolver;
}

lock (_gate)
{
return _resolvers[index] ??= CreateResolver(index);
}
}

private static global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver CreateResolver(int index)
{
return index switch
{
0 => global::OpenRouter.SourceGenerationContext.TypeInfoResolver,
_ => throw new global::System.ArgumentOutOfRangeException(nameof(index)),
};
}
}
}
}
Loading
Loading