Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -188,7 +188,7 @@ public static JsonElement CreateJsonSchema(
}

/// <summary>Gets the default JSON schema to be used by types or functions.</summary>
internal static JsonElement DefaultJsonSchema { get; } = ParseJsonElement("{}"u8);
internal static JsonElement DefaultJsonSchema { get; } = JsonElement.Parse("{}"u8);

/// <summary>Validates the provided JSON schema document.</summary>
internal static void ValidateSchemaDocument(JsonElement document, [CallerArgumentExpression("document")] string? paramName = null)
Expand Down Expand Up @@ -750,12 +750,6 @@ private static void InsertAtStart(this JsonObject jsonObject, string key, JsonNo
#endif
}

private static JsonElement ParseJsonElement(ReadOnlySpan<byte> utf8Json)
{
Utf8JsonReader reader = new(utf8Json);
return JsonElement.ParseValue(ref reader);
}

/// <summary>
/// Tries to get the effective default value for a parameter, checking both C# default value syntax and DefaultValueAttribute.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static object ToToolResult(ChatMessageContent content)
part.Write(writer, ModelReaderWriterOptions.Json);
}

return JsonSerializer.Deserialize(ms.GetBuffer().AsSpan(0, (int)ms.Position), AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!;
return JsonElement.Parse(ms.GetBuffer().AsSpan(0, (int)ms.Position));
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void Serialization_JsonRoundtrips()
public void Serialization_ForJsonSchemaRoundtrips()
{
string json = JsonSerializer.Serialize(
ChatResponseFormat.ForJsonSchema(JsonSerializer.Deserialize<JsonElement>("[1,2,3]", AIJsonUtilities.DefaultOptions), "name", "description"),
ChatResponseFormat.ForJsonSchema(JsonElement.Parse("[1,2,3]"), "name", "description"),
TestJsonSerializerContext.Default.ChatResponseFormat);
Assert.Equal("""{"$type":"json","schema":[1,2,3],"schemaName":"name","schemaDescription":"description"}""", json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Serialization_Roundtrips()

Assert.NotNull(deserialized.AdditionalProperties);
Assert.Single(deserialized.AdditionalProperties);
Assert.Equal(JsonSerializer.Deserialize<JsonElement>("\"value\"", AIJsonUtilities.DefaultOptions).ToString(), deserialized.AdditionalProperties["key"]!.ToString());
Assert.Equal(JsonElement.Parse("\"value\"").ToString(), deserialized.AdditionalProperties["key"]!.ToString());

Assert.Null(deserialized.RawRepresentation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void Serialization_Roundtrips()

Assert.NotNull(deserialized.AdditionalProperties);
Assert.Single(deserialized.AdditionalProperties);
Assert.Equal(JsonSerializer.Deserialize<JsonElement>("\"value\"", AIJsonUtilities.DefaultOptions).ToString(), deserialized.AdditionalProperties["key"]!.ToString());
Assert.Equal(JsonElement.Parse("\"value\"").ToString(), deserialized.AdditionalProperties["key"]!.ToString());

Assert.Null(deserialized.RawRepresentation);
Assert.Equal("snippet", deserialized.Snippet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ public async Task ResponseFormat_JsonSchema_NonStreaming()

Assert.NotNull(await client.GetResponseAsync("hello", new()
{
ResponseFormat = ChatResponseFormat.ForJsonSchema(JsonSerializer.Deserialize<JsonElement>("""
ResponseFormat = ChatResponseFormat.ForJsonSchema(JsonElement.Parse("""
{
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ private sealed class CustomAIFunction(string name, string jsonSchema, IReadOnlyD
{
public override string Name => name;
public override IReadOnlyDictionary<string, object?> AdditionalProperties => additionalProperties;
public override JsonElement JsonSchema { get; } = JsonSerializer.Deserialize<JsonElement>(jsonSchema, AIJsonUtilities.DefaultOptions);
public override JsonElement JsonSchema { get; } = JsonElement.Parse(jsonSchema);
protected override ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken) => throw new NotSupportedException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public void AsOpenAIChatMessages_ProducesExpectedOutput(bool withOptions)
{
["param1"] = "value1",
["param2"] = 42
}), JsonSerializer.Deserialize<JsonElement>(tc.FunctionArguments.ToMemory().Span)));
}), JsonElement.Parse(tc.FunctionArguments.ToMemory().Span)));
Assert.Equal("JohnSmith", m2.ParticipantName);

ToolChatMessage m3 = Assert.IsType<ToolChatMessage>(convertedMessages[index + 3], exactMatch: false);
Expand Down Expand Up @@ -541,7 +541,7 @@ public void AsOpenAIResponseItems_ProducesExpectedOutput()
{
["param1"] = "value1",
["param2"] = 42
}), JsonSerializer.Deserialize<JsonElement>(m3.FunctionArguments.ToMemory().Span)));
}), JsonElement.Parse(m3.FunctionArguments.ToMemory().Span)));

FunctionCallOutputResponseItem m4 = Assert.IsAssignableFrom<FunctionCallOutputResponseItem>(convertedItems[4]);
Assert.Equal("callid123", m4.CallId);
Expand Down
Loading