< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.ToolResultSerializer
Assembly: NexusLabs.Needlr.AgentFramework
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework/ToolResultSerializer.cs
Line coverage
80%
Covered lines: 8
Uncovered lines: 2
Coverable lines: 10
Total lines: 44
Line coverage: 80%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Serialize(...)75%9880%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework/ToolResultSerializer.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace NexusLabs.Needlr.AgentFramework;
 4
 5/// <summary>
 6/// Shared utility for converting tool result objects to a string representation
 7/// suitable for display or inclusion in chat messages. Handles the <see cref="JsonElement"/>
 8/// values produced by MEAI's <c>AIFunctionFactory</c>, plain strings, and arbitrary objects.
 9/// </summary>
 10public static class ToolResultSerializer
 11{
 12    /// <summary>
 13    /// Serializes a tool result for inclusion in a <see cref="Microsoft.Extensions.AI.FunctionResultContent"/>
 14    /// or for display. <see cref="JsonElement"/> values are rendered to raw JSON text.
 15    /// Strings are returned as-is. Null returns empty string. All other types are
 16    /// JSON-serialized.
 17    /// </summary>
 18    public static string Serialize(object? result)
 19    {
 13120        if (result is null)
 21        {
 122            return "";
 23        }
 24
 13025        if (result is JsonElement jsonElement)
 26        {
 11627            return jsonElement.GetRawText();
 28        }
 29
 1430        if (result is string s)
 31        {
 932            return s;
 33        }
 34
 35        try
 36        {
 537            return JsonSerializer.Serialize(result, result.GetType());
 38        }
 039        catch (JsonException)
 40        {
 041            return result.ToString() ?? "";
 42        }
 543    }
 44}

Methods/Properties

Serialize(System.Object)