Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M.E.AI - Add support for string JsonElement parsing to primitive #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static JsonSerializerOptions CreateDefaultOptions()
JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver(),
Converters = { new JsonStringEnumConverter() },
Converters = { new JsonStringEnumConverter(), new JsonStringBooleanConverter() },
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.Extensions.AI;

internal sealed class JsonStringBooleanConverter : JsonConverter<bool>
RogerBarreto marked this conversation as resolved.
Show resolved Hide resolved
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String && bool.TryParse(reader.GetString(), out var result))
RogerBarreto marked this conversation as resolved.
Show resolved Hide resolved
{
return result;
}

return reader.GetBoolean();
}

public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,23 @@ public async Task AIFunctionFactory_JsonElementValues_ValuesDeserialized()
Dictionary<string, object?> arguments = JsonSerializer.Deserialize<Dictionary<string, object?>>("""
{
"a": ["Monday", "Tuesday", "Wednesday"],
"b": 123.4,
"b1": 123.4,
"b2": "123.4",
"c": "072c2d93-7cf6-4d0d-aebc-acc51e6ee7ee",
"d": {
"property1": "42",
"property2": "43",
"property3": "44"
}
},
"e1": true,
"e2": "false"
}
""", TestJsonSerializerContext.Default.Options)!;
Assert.All(arguments.Values, v => Assert.IsType<JsonElement>(v));

AIFunction function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options);
AIFunction function = AIFunctionFactory.Create(
(DayOfWeek[] a, double b1, float b2, Guid c, Dictionary<string, string> d, bool e1, bool e2) => b1,
serializerOptions: TestJsonSerializerContext.Default.Options);
var result = await function.InvokeAsync(arguments);
AssertExtensions.EqualFunctionCallResults(123.4, result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.Extensions.AI;

internal sealed class JsonStringBooleanConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String && bool.TryParse(reader.GetString(), out var result))
{
return result;
}

return reader.GetBoolean();
}

public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace Microsoft.Extensions.AI;

[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = new[] { typeof(JsonStringBooleanConverter) },
UseStringEnumConverter = true)]
[JsonSerializable(typeof(ChatCompletion))]
[JsonSerializable(typeof(StreamingChatCompletionUpdate))]
Expand All @@ -28,4 +30,6 @@ namespace Microsoft.Extensions.AI;
[JsonSerializable(typeof(DayOfWeek[]))] // Used in Content tests
[JsonSerializable(typeof(Guid))] // Used in Content tests
[JsonSerializable(typeof(decimal))] // Used in Content tests
[JsonSerializable(typeof(bool))] // Used in Content tests
[JsonSerializable(typeof(float))] // Used in Content tests
internal sealed partial class TestJsonSerializerContext : JsonSerializerContext;