// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Microsoft.Agents.AI.Workflows.Execution;
namespace Microsoft.Agents.AI.Workflows.Checkpointing;
///
/// Provides support for using values as dictionary keys when serializing and deserializing JSON.
///
internal sealed class ExecutorIdentityConverter() : JsonConverterDictionarySupportBase
{
protected override JsonTypeInfo TypeInfo
=> WorkflowsJsonUtilities.JsonContext.Default.ExecutorIdentity;
protected override ExecutorIdentity Parse(string propertyName)
{
if (propertyName.Length == 0)
{
return ExecutorIdentity.None;
}
if (propertyName[0] == '@')
{
return new() { Id = propertyName.Substring(1) };
}
throw new JsonException($"Invalid ExecutorIdentity key Expecting empty string or a value that is prefixed with '@'. Got '{propertyName}'");
}
protected override string Stringify(ExecutorIdentity value)
{
return value == ExecutorIdentity.None
? string.Empty
: $"@{value.Id}";
}
}