44using System . Collections . Generic ;
55using System . ComponentModel ;
66using System . Diagnostics ;
7+ using System . Diagnostics . CodeAnalysis ;
78using System . IO ;
89using System . Text . Json ;
10+ using System . Text . Json . Serialization . Metadata ;
911using System . Threading ;
1012using System . Threading . Tasks ;
1113using Azure . Core ;
@@ -77,6 +79,7 @@ internal SearchResult() { }
7779 /// that the operation should be canceled.
7880 /// </param>
7981 /// <returns>Deserialized SearchResults.</returns>
82+ [ RequiresUnreferencedCode ( JsonSerialization . TrimWarning ) ]
8083 internal static async Task < SearchResult < T > > DeserializeAsync (
8184 JsonElement element ,
8285 ObjectSerializer serializer ,
@@ -86,6 +89,89 @@ internal static async Task<SearchResult<T>> DeserializeAsync(
8689 #pragma warning restore CS1572
8790 {
8891 Debug . Assert ( options != null ) ;
92+ SearchResult < T > result = DeserializeEnvelope ( element ) ;
93+
94+ // Deserialize the model
95+ if ( serializer != null )
96+ {
97+ using Stream stream = element . ToStream ( ) ;
98+ T document = async ?
99+ ( T ) await serializer . DeserializeAsync ( stream , typeof ( T ) , cancellationToken ) . ConfigureAwait( false) :
100+ ( T) serializer. Deserialize( stream, typeof ( T ) , cancellationToken ) ;
101+ result . Document = document ;
102+ }
103+ else
104+ {
105+ T document ;
106+ if ( async)
107+ {
108+ using Stream stream = element. ToStream( ) ;
109+ document = await JsonSerializer . DeserializeAsync < T > ( stream , options , cancellationToken ) . ConfigureAwait ( false ) ;
110+ }
111+ else
112+ {
113+ document = JsonSerializer . Deserialize < T > ( element . GetRawText ( ) , options ) ;
114+ }
115+ result . Document = document ;
116+ }
117+
118+ return result;
119+ }
120+
121+ /// <summary>
122+ /// Deserialize a SearchResult and its model.
123+ /// </summary>
124+ /// <param name="element">A JSON element.</param>
125+ /// <param name="serializer">
126+ /// Optional serializer that can be used to customize the serialization
127+ /// of strongly typed models.
128+ /// </param>
129+ /// <param name="typeInfo">Metadata about the type to deserialize.</param>
130+ /// <param name="async">Whether to execute sync or async.</param>
131+ /// <param name="cancellationToken">
132+ /// Optional <see cref="CancellationToken"/> to propagate notifications
133+ /// that the operation should be canceled.
134+ /// </param>
135+ /// <returns>Deserialized SearchResults.</returns>
136+ internal static async Task < SearchResult < T > > DeserializeAsync (
137+ JsonElement element ,
138+ ObjectSerializer serializer ,
139+ JsonTypeInfo < T > typeInfo ,
140+ bool async ,
141+ CancellationToken cancellationToken )
142+ #pragma warning restore CS1572
143+ {
144+ Debug . Assert ( typeInfo != null ) ;
145+ SearchResult < T > result = DeserializeEnvelope ( element ) ;
146+ // Deserialize the model
147+ if ( serializer != null )
148+ {
149+ using Stream stream = element . ToStream ( ) ;
150+ T document = async ?
151+ ( T ) await serializer . DeserializeAsync ( stream , typeof ( T ) , cancellationToken ) . ConfigureAwait( false) :
152+ ( T) serializer. Deserialize( stream, typeof ( T ) , cancellationToken ) ;
153+ result . Document = document ;
154+ }
155+ else
156+ {
157+ T document ;
158+ if ( async)
159+ {
160+ using Stream stream = element. ToStream( ) ;
161+ document = await JsonSerializer . DeserializeAsync < T > ( stream , typeInfo , cancellationToken ) . ConfigureAwait ( false ) ;
162+ }
163+ else
164+ {
165+ document = JsonSerializer . Deserialize < T > ( element . GetRawText ( ) , typeInfo ) ;
166+ }
167+ result . Document = document ;
168+ }
169+
170+ return result ;
171+ }
172+
173+ private static SearchResult < T > DeserializeEnvelope ( JsonElement element )
174+ {
89175 SearchResult < T > result = new SearchResult < T > ( ) ;
90176 result . SemanticSearch = new SemanticSearchResult ( ) ;
91177 foreach ( JsonProperty prop in element . EnumerateObject ( ) )
@@ -136,30 +222,6 @@ internal static async Task<SearchResult<T>> DeserializeAsync(
136222 }
137223 }
138224
139- // Deserialize the model
140- if ( serializer != null )
141- {
142- using Stream stream = element . ToStream ( ) ;
143- T document = async ?
144- ( T ) await serializer . DeserializeAsync ( stream , typeof ( T ) , cancellationToken ) . ConfigureAwait( false) :
145- ( T) serializer. Deserialize( stream, typeof ( T ) , cancellationToken ) ;
146- result . Document = document ;
147- }
148- else
149- {
150- T document ;
151- if ( async)
152- {
153- using Stream stream = element. ToStream( ) ;
154- document = await JsonSerializer . DeserializeAsync < T > ( stream , options , cancellationToken ) . ConfigureAwait ( false ) ;
155- }
156- else
157- {
158- document = JsonSerializer . Deserialize < T > ( element . GetRawText ( ) , options ) ;
159- }
160- result . Document = document ;
161- }
162-
163225 return result ;
164226 }
165227 }
0 commit comments