db620d33df
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
67 lines
2.6 KiB
C#
67 lines
2.6 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization.Metadata;
|
|
using Microsoft.Agents.AI.DurableTask.State;
|
|
using Microsoft.DurableTask;
|
|
|
|
namespace Microsoft.Agents.AI.DurableTask;
|
|
|
|
/// <summary>
|
|
/// Custom data converter for durable agents and workflows that ensures proper JSON serialization.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This converter handles special cases like <see cref="DurableAgentState"/> using source-generated
|
|
/// JSON contexts for AOT compatibility, and falls back to reflection-based serialization for other types.
|
|
/// </remarks>
|
|
internal sealed class DurableDataConverter : DataConverter
|
|
{
|
|
private static readonly JsonSerializerOptions s_options = new(DurableAgentJsonUtilities.DefaultOptions)
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
PropertyNameCaseInsensitive = true,
|
|
};
|
|
|
|
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Fallback uses reflection when metadata unavailable.")]
|
|
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Fallback uses reflection when metadata unavailable.")]
|
|
public override object? Deserialize(string? data, Type targetType)
|
|
{
|
|
if (data is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (targetType == typeof(DurableAgentState))
|
|
{
|
|
return JsonSerializer.Deserialize(data, DurableAgentStateJsonContext.Default.DurableAgentState);
|
|
}
|
|
|
|
JsonTypeInfo? typeInfo = s_options.GetTypeInfo(targetType);
|
|
return typeInfo is not null
|
|
? JsonSerializer.Deserialize(data, typeInfo)
|
|
: JsonSerializer.Deserialize(data, targetType, s_options);
|
|
}
|
|
|
|
[return: NotNullIfNotNull(nameof(value))]
|
|
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Fallback uses reflection when metadata unavailable.")]
|
|
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Fallback uses reflection when metadata unavailable.")]
|
|
public override string? Serialize(object? value)
|
|
{
|
|
if (value is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (value is DurableAgentState durableAgentState)
|
|
{
|
|
return JsonSerializer.Serialize(durableAgentState, DurableAgentStateJsonContext.Default.DurableAgentState);
|
|
}
|
|
|
|
JsonTypeInfo? typeInfo = s_options.GetTypeInfo(value.GetType());
|
|
return typeInfo is not null
|
|
? JsonSerializer.Serialize(value, typeInfo)
|
|
: JsonSerializer.Serialize(value, s_options);
|
|
}
|
|
}
|