11using System . Text . Json ;
22using CompactifAI . Client . Models ;
3+ using CompactifAI . Client . Serialization ;
34using Xunit ;
45
56namespace CompactifAI . Client . Tests ;
67
78public class ModelsTests
89{
9- private readonly JsonSerializerOptions _jsonOptions = new ( )
10- {
11- PropertyNamingPolicy = JsonNamingPolicy . SnakeCaseLower
12- } ;
10+ // Use source-generated context for high-performance serialization
11+ private static CompactifAIJsonContext JsonContext => CompactifAIJsonContext . Default ;
1312
1413 #region ChatMessage Tests
1514
@@ -56,7 +55,7 @@ public void ChatCompletionRequest_Serializes_WithRequiredFields()
5655 }
5756 } ;
5857
59- var json = JsonSerializer . Serialize ( request , _jsonOptions ) ;
58+ var json = JsonSerializer . Serialize ( request , JsonContext . ChatCompletionRequest ) ;
6059
6160 Assert . Contains ( "\" model\" :\" test-model\" " , json ) ;
6261 Assert . Contains ( "\" messages\" :" , json ) ;
@@ -73,7 +72,7 @@ public void ChatCompletionRequest_Serializes_OptionalFieldsOmittedWhenNull()
7372 Messages = new List < ChatMessage > { ChatMessage . User ( "Test" ) }
7473 } ;
7574
76- var json = JsonSerializer . Serialize ( request , _jsonOptions ) ;
75+ var json = JsonSerializer . Serialize ( request , JsonContext . ChatCompletionRequest ) ;
7776
7877 Assert . DoesNotContain ( "temperature" , json ) ;
7978 Assert . DoesNotContain ( "max_tokens" , json ) ;
@@ -93,7 +92,7 @@ public void ChatCompletionRequest_Serializes_WithAllOptionalFields()
9392 FrequencyPenalty = 0.5
9493 } ;
9594
96- var json = JsonSerializer . Serialize ( request , _jsonOptions ) ;
95+ var json = JsonSerializer . Serialize ( request , JsonContext . ChatCompletionRequest ) ;
9796
9897 Assert . Contains ( "\" temperature\" :0.7" , json ) ;
9998 Assert . Contains ( "\" max_tokens\" :100" , json ) ;
@@ -132,7 +131,7 @@ public void ChatCompletionResponse_Deserializes_Correctly()
132131 }
133132 """ ;
134133
135- var response = JsonSerializer . Deserialize < ChatCompletionResponse > ( json , _jsonOptions ) ;
134+ var response = JsonSerializer . Deserialize ( json , JsonContext . ChatCompletionResponse ) ;
136135
137136 Assert . NotNull ( response ) ;
138137 Assert . Equal ( "chatcmpl-123" , response . Id ) ;
@@ -161,7 +160,7 @@ public void CompletionRequest_Serializes_Correctly()
161160 TopP = 0.95
162161 } ;
163162
164- var json = JsonSerializer . Serialize ( request , _jsonOptions ) ;
163+ var json = JsonSerializer . Serialize ( request , JsonContext . CompletionRequest ) ;
165164
166165 Assert . Contains ( "\" model\" :\" test-model\" " , json ) ;
167166 Assert . Contains ( "\" prompt\" :\" Once upon a time\" " , json ) ;
@@ -198,7 +197,7 @@ public void CompletionResponse_Deserializes_Correctly()
198197 }
199198 """ ;
200199
201- var response = JsonSerializer . Deserialize < CompletionResponse > ( json , _jsonOptions ) ;
200+ var response = JsonSerializer . Deserialize ( json , JsonContext . CompletionResponse ) ;
202201
203202 Assert . NotNull ( response ) ;
204203 Assert . Equal ( "cmpl-123" , response . Id ) ;
@@ -230,7 +229,7 @@ public void TranscriptionResponse_Deserializes_Correctly()
230229 }
231230 """ ;
232231
233- var response = JsonSerializer . Deserialize < TranscriptionResponse > ( json , _jsonOptions ) ;
232+ var response = JsonSerializer . Deserialize ( json , JsonContext . TranscriptionResponse ) ;
234233
235234 Assert . NotNull ( response ) ;
236235 Assert . Equal ( "transcribe" , response . Task ) ;
@@ -270,7 +269,7 @@ public void ModelsResponse_Deserializes_Correctly()
270269 }
271270 """ ;
272271
273- var response = JsonSerializer . Deserialize < ModelsResponse > ( json , _jsonOptions ) ;
272+ var response = JsonSerializer . Deserialize ( json , JsonContext . ModelsResponse ) ;
274273
275274 Assert . NotNull ( response ) ;
276275 Assert . Equal ( "list" , response . Object ) ;
@@ -309,7 +308,7 @@ public void ModelsResponse_ParametersNumber_HandlesVariousFormats(string paramet
309308 }
310309 """ ;
311310
312- var response = JsonSerializer . Deserialize < ModelsResponse > ( json , _jsonOptions ) ;
311+ var response = JsonSerializer . Deserialize ( json , JsonContext . ModelsResponse ) ;
313312
314313 Assert . NotNull ( response ) ;
315314 Assert . Single ( response . Data ) ;
@@ -338,7 +337,7 @@ public void ModelsResponse_Deserializes_WithMissingParametersNumber()
338337 }
339338 """ ;
340339
341- var response = JsonSerializer . Deserialize < ModelsResponse > ( json , _jsonOptions ) ;
340+ var response = JsonSerializer . Deserialize ( json , JsonContext . ModelsResponse ) ;
342341
343342 Assert . NotNull ( response ) ;
344343 Assert . Single ( response . Data ) ;
@@ -352,18 +351,21 @@ public void ModelsResponse_Deserializes_WithMissingParametersNumber()
352351 [ Fact ]
353352 public void Tool_Serializes_Correctly ( )
354353 {
354+ // Create JsonElement from anonymous object for parameters
355+ var parametersJson = JsonSerializer . SerializeToElement ( new { type = "object" , properties = new { location = new { type = "string" } } } ) ;
356+
355357 var tool = new Tool
356358 {
357359 Type = "function" ,
358360 Function = new ToolFunction
359361 {
360362 Name = "get_weather" ,
361363 Description = "Get the current weather" ,
362- Parameters = new { type = "object" , properties = new { location = new { type = "string" } } }
364+ Parameters = parametersJson
363365 }
364366 } ;
365367
366- var json = JsonSerializer . Serialize ( tool , _jsonOptions ) ;
368+ var json = JsonSerializer . Serialize ( tool , JsonContext . Tool ) ;
367369
368370 Assert . Contains ( "\" type\" :\" function\" " , json ) ;
369371 Assert . Contains ( "\" name\" :\" get_weather\" " , json ) ;
0 commit comments