22// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33
44using System . Text . Json ;
5+ using System . Text . Json . Nodes ;
56using System . Text . Json . Serialization ;
6- using Microsoft . OpenApi . Any ;
77
88namespace TodoApp . OpenApi ;
99
@@ -19,9 +19,9 @@ internal static class ExampleFormatter
1919 /// <typeparam name="TProvider">The type of the example provider.</typeparam>
2020 /// <param name="context">The JSON serializer context to use.</param>
2121 /// <returns>
22- /// The <see cref="IOpenApiAny "/> to use as the example.
22+ /// The <see cref="JsonNode "/> to use as the example.
2323 /// </returns>
24- public static IOpenApiAny AsJson < TSchema , TProvider > ( JsonSerializerContext context )
24+ public static JsonNode ? AsJson < TSchema , TProvider > ( JsonSerializerContext context )
2525 where TProvider : IExampleProvider < TSchema >
2626 => AsJson ( TProvider . GenerateExample ( ) , context ) ;
2727
@@ -32,87 +32,12 @@ public static IOpenApiAny AsJson<TSchema, TProvider>(JsonSerializerContext conte
3232 /// <param name="example">The example value to format as JSON.</param>
3333 /// <param name="context">The JSON serializer context to use.</param>
3434 /// <returns>
35- /// The <see cref="IOpenApiAny "/> to use as the example.
35+ /// The <see cref="JsonNode "/> to use as the example.
3636 /// </returns>
37- public static IOpenApiAny AsJson < T > ( T example , JsonSerializerContext context )
37+ public static JsonNode ? AsJson < T > ( T example , JsonSerializerContext context )
3838 {
3939 // Apply any formatting rules configured for the API (e.g. camel casing)
4040 var json = JsonSerializer . Serialize ( example , typeof ( T ) , context ) ;
41- using var document = JsonDocument . Parse ( json ) ;
42-
43- if ( document . RootElement . ValueKind == JsonValueKind . String )
44- {
45- return new OpenApiString ( document . RootElement . ToString ( ) ) ;
46- }
47-
48- var result = new OpenApiObject ( ) ;
49-
50- // Recursively build up the example from the properties of the object
51- foreach ( var token in document . RootElement . EnumerateObject ( ) )
52- {
53- if ( TryParse ( token . Value , out var any ) )
54- {
55- result [ token . Name ] = any ;
56- }
57- }
58-
59- return result ;
60- }
61-
62- private static bool TryParse ( JsonElement token , out IOpenApiAny ? any )
63- {
64- any = null ;
65-
66- switch ( token . ValueKind )
67- {
68- case JsonValueKind . Array :
69- var array = new OpenApiArray ( ) ;
70-
71- foreach ( var value in token . EnumerateArray ( ) )
72- {
73- if ( TryParse ( value , out var child ) )
74- {
75- array . Add ( child ) ;
76- }
77- }
78-
79- any = array ;
80- return true ;
81-
82- case JsonValueKind . False :
83- any = new OpenApiBoolean ( false ) ;
84- return true ;
85-
86- case JsonValueKind . True :
87- any = new OpenApiBoolean ( true ) ;
88- return true ;
89-
90- case JsonValueKind . Number :
91- any = new OpenApiDouble ( token . GetDouble ( ) ) ;
92- return true ;
93-
94- case JsonValueKind . String :
95- any = new OpenApiString ( token . GetString ( ) ) ;
96- return true ;
97-
98- case JsonValueKind . Object :
99- var obj = new OpenApiObject ( ) ;
100-
101- foreach ( var child in token . EnumerateObject ( ) )
102- {
103- if ( TryParse ( child . Value , out var value ) )
104- {
105- obj [ child . Name ] = value ;
106- }
107- }
108-
109- any = obj ;
110- return true ;
111-
112- case JsonValueKind . Null :
113- case JsonValueKind . Undefined :
114- default :
115- return false ;
116- }
41+ return JsonNode . Parse ( json ) ;
11742 }
11843}
0 commit comments