// Copyright (c) Microsoft. All rights reserved. using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; namespace Microsoft.Extensions.AI.Agents.Runtime; /// /// Provides extension methods for JSON serialization with source generation support. /// internal static class JsonSerializerExtensions { /// /// Gets the JsonTypeInfo for a type, preferring the one from options if available, /// otherwise falling back to the source-generated context. /// /// The type to get JsonTypeInfo for. /// The JsonSerializerOptions to check first. /// The fallback JsonSerializerContext to use if not found in options. /// The JsonTypeInfo for the requested type. public static JsonTypeInfo GetTypeInfo(this JsonSerializerOptions options, JsonSerializerContext fallbackContext) { // Try to get from the options first (if a context is configured) if (options.TypeInfoResolver?.GetTypeInfo(typeof(T), options) is JsonTypeInfo typeInfo) { return typeInfo; } // Fall back to the provided source-generated context return (JsonTypeInfo)fallbackContext.GetTypeInfo(typeof(T))!; } }