@@ -129,7 +129,7 @@ private static void AppendParameters(StringBuilder queryBuilder, ArraySerializat
129129 // load element value as string
130130 if ( pair . Value is ICollection )
131131 {
132- paramKeyValPair = FlattenCollection ( pair . Value as ICollection , arraySerialization , GetSeparator ( arraySerialization ) , true , Uri . EscapeDataString ( pair . Key ) ) ;
132+ paramKeyValPair = FlattenCollection ( pair . Value as ICollection , arraySerialization , Uri . EscapeDataString ( pair . Key ) ) ;
133133 }
134134 else
135135 {
@@ -196,7 +196,7 @@ internal static List<KeyValuePair<string, object>> PrepareFormFieldsFromObject(s
196196 }
197197 else if ( value is IList enumerable )
198198 {
199- PrepareFormFieldsForEnumerable ( name , arraySerializationFormat , keys , propInfo , enumerable ) ;
199+ PrepareFormFieldsForEnumerable ( name , enumerable , arraySerializationFormat , keys , propInfo ) ;
200200 }
201201 else if ( value is Stream || value is JToken || value is Enum )
202202 {
@@ -205,30 +205,21 @@ internal static List<KeyValuePair<string, object>> PrepareFormFieldsFromObject(s
205205 }
206206 else if ( value is IDictionary dictionary )
207207 {
208- PrepareFormFieldsForDictionary ( name , arraySerializationFormat , keys , propInfo , dictionary ) ;
209- }
210- else if ( value is CoreJsonObject jsonObject )
211- {
212- PrepareFormFieldsFromObject ( name , RemoveNullValues ( jsonObject . GetStoredObject ( ) ) , arraySerializationFormat , keys , propInfo ) ;
208+ PrepareFormFieldsForDictionary ( name , dictionary , arraySerializationFormat , keys , propInfo ) ;
209+ return keys ;
213210 }
214- else if ( value is CoreJsonValue jsonValue )
211+ else if ( value is CoreJsonObject || value is CoreJsonValue )
215212 {
216- PrepareFormFieldsFromObject ( name , jsonValue . GetStoredObject ( ) , arraySerializationFormat , keys , propInfo ) ;
213+ PrepareFormFieldsFromObject ( name , GetProcessedValue ( value ) , arraySerializationFormat , keys , propInfo ) ;
217214 }
218215 else if ( ! value . GetType ( ) . Namespace . StartsWith ( "System" ) )
219216 {
220217 PrepareFormFieldsForCustomTypes ( name , value , arraySerializationFormat , keys ) ;
221218 }
222- else if ( value is DateTime dateTime )
223- {
224- var convertedValue = GetConvertedValue ( value , propInfo ) ;
225- keys . Add ( new KeyValuePair < string , object > ( name , convertedValue ?? dateTime . ToString ( DateTimeFormat ) ) ) ;
226- }
227219 else
228220 {
229- keys . Add ( new KeyValuePair < string , object > ( name , GetProcessedValue ( value ) ) ) ;
221+ keys . Add ( new KeyValuePair < string , object > ( name , GetProcessedValue ( value , propInfo ) ) ) ;
230222 }
231-
232223 return keys ;
233224 }
234225
@@ -285,7 +276,7 @@ private static void PrepareFormFieldsForCustomTypes(string name, object value, A
285276 }
286277 }
287278
288- private static void PrepareFormFieldsForDictionary ( string name , ArraySerialization arraySerializationFormat , List < KeyValuePair < string , object > > keys , PropertyInfo propInfo , IDictionary dictionary )
279+ private static void PrepareFormFieldsForDictionary ( string name , IDictionary dictionary , ArraySerialization arraySerializationFormat , List < KeyValuePair < string , object > > keys = null , PropertyInfo propInfo = null )
289280 {
290281 foreach ( var sName in dictionary . Keys )
291282 {
@@ -296,21 +287,10 @@ private static void PrepareFormFieldsForDictionary(string name, ArraySerializati
296287 }
297288 }
298289
299- private static void PrepareFormFieldsForEnumerable ( string name , ArraySerialization arraySerializationFormat , List < KeyValuePair < string , object > > keys , PropertyInfo propInfo , IList enumerable )
290+ private static void PrepareFormFieldsForEnumerable ( string name , IList enumerable , ArraySerialization arraySerializationFormat , List < KeyValuePair < string , object > > keys , PropertyInfo propInfo )
300291 {
301292 var enumerator = enumerable . GetEnumerator ( ) ;
302-
303- var hasNested = false ;
304- while ( enumerator . MoveNext ( ) )
305- {
306- var subValue = enumerator . Current ;
307- if ( subValue != null && ( subValue is JObject || subValue is IList || subValue is IDictionary || ! subValue . GetType ( ) . Namespace . StartsWith ( "System" ) ) )
308- {
309- hasNested = true ;
310- break ;
311- }
312- }
313-
293+ bool hasNested = HasCustomeNestedType ( enumerator ) ;
314294 int i = 0 ;
315295 enumerator . Reset ( ) ;
316296 while ( enumerator . MoveNext ( ) )
@@ -330,28 +310,50 @@ private static void PrepareFormFieldsForEnumerable(string name, ArraySerializati
330310 {
331311 continue ;
332312 }
333-
334313 PrepareFormFieldsFromObject ( fullSubName , subValue , arraySerializationFormat , keys , propInfo ) ;
335314 i ++ ;
336315 }
337316 }
338317
339- private static object GetProcessedValue ( object value )
318+ private static bool HasCustomeNestedType ( IEnumerator enumerator )
319+ {
320+ while ( enumerator . MoveNext ( ) )
321+ {
322+ var subValue = enumerator . Current ;
323+ if ( subValue != null && ( subValue is JObject || subValue is IList || subValue is IDictionary || ! subValue . GetType ( ) . Namespace . StartsWith ( "System" ) ) )
324+ {
325+ return true ;
326+ }
327+ }
328+ return false ;
329+ }
330+
331+ private static object GetProcessedValue ( object value , PropertyInfo propInfo = null )
340332 {
341333 if ( value is Stream )
342334 {
343335 return value ;
344336 }
345-
346337 if ( value is Enum )
347338 {
348339 return JsonSerialize ( value ) . Trim ( '\" ' ) ;
349340 }
350-
351341 if ( value is JToken )
352342 {
353343 return value . ToString ( ) ;
354344 }
345+ if ( value is CoreJsonObject jsonObject )
346+ {
347+ return RemoveNullValues ( jsonObject . GetStoredObject ( ) ) ;
348+ }
349+ if ( value is CoreJsonValue jsonValue )
350+ {
351+ return jsonValue . GetStoredObject ( ) ;
352+ }
353+ if ( value is DateTime dateTime )
354+ {
355+ return GetConvertedValue ( dateTime , propInfo ) ?? dateTime . ToString ( DateTimeFormat ) ;
356+ }
355357 return value ;
356358 }
357359
@@ -459,18 +461,17 @@ private static int IndexOf(StringBuilder stringBuilder, string strCheck)
459461 private static string FlattenCollection (
460462 ICollection array ,
461463 ArraySerialization fmt ,
462- char separator ,
463- bool urlEncode ,
464464 string key = "" )
465465 {
466466 StringBuilder builder = new StringBuilder ( ) ;
467467 string format = GetFormatString ( fmt , key , builder ) ;
468468
469469 // append all elements in the array into a string
470470 int index = 0 ;
471+ char separator = GetSeparator ( fmt ) ;
471472 foreach ( object element in array )
472473 {
473- builder . AppendFormat ( format , GetElementValue ( element , urlEncode ) , separator , index ++ ) ;
474+ builder . AppendFormat ( format , GetElementValue ( element , true ) , separator , index ++ ) ;
474475 }
475476
476477 // remove the last separator, if appended
@@ -545,46 +546,54 @@ private static List<KeyValuePair<string, object>> ProcessQueryParamsForCustomTyp
545546
546547 if ( kvp . Value . GetType ( ) . Namespace . StartsWith ( "System" ) )
547548 {
548- if ( kvp . Value is IList )
549- {
550- var list = kvp . Value as IList ;
551-
552- if ( list ? . Count != 0 )
553- {
554- var item = list [ 0 ] ;
555-
556- if ( item . GetType ( ) . Namespace . StartsWith ( "System" ) )
557- {
558- // List of scalar type
559- processedParameters . Add ( kvp ) ;
560- }
561- else
562- {
563- // List of custom type
564- var innerList = PrepareFormFieldsFromObject ( kvp . Key , kvp . Value , arraySerializationFormat : ArraySerialization . Indexed ) ;
565- innerList = ApplySerializationFormatToScalarArrays ( innerList ) ;
566- processedParameters . AddRange ( innerList ) ;
567- }
568- }
569- }
570- else
571- {
572- // Scalar type
573- processedParameters . Add ( kvp ) ;
574- }
549+ HandlePrimitiveTypes ( processedParameters , kvp ) ;
575550 }
576551 else
577552 {
578553 // Custom type
579- var list = PrepareFormFieldsFromObject ( kvp . Key , kvp . Value , arraySerializationFormat : ArraySerialization . Indexed ) ;
580- list = ApplySerializationFormatToScalarArrays ( list ) ;
581- processedParameters . AddRange ( list ) ;
554+ HandleCustomType ( processedParameters , kvp ) ;
582555 }
583556 }
584557
585558 return processedParameters ;
586559 }
587560
561+ private static void HandleCustomType ( List < KeyValuePair < string , object > > processedParameters , KeyValuePair < string , object > kvp )
562+ {
563+ var list = PrepareFormFieldsFromObject ( kvp . Key , kvp . Value , arraySerializationFormat : ArraySerialization . Indexed ) ;
564+ list = ApplySerializationFormatToScalarArrays ( list ) ;
565+ processedParameters . AddRange ( list ) ;
566+ }
567+
568+ private static void HandlePrimitiveTypes ( List < KeyValuePair < string , object > > processedParameters , KeyValuePair < string , object > kvp )
569+ {
570+ if ( kvp . Value is IList )
571+ {
572+ var list = kvp . Value as IList ;
573+
574+ if ( list ? . Count != 0 )
575+ {
576+ var item = list [ 0 ] ;
577+
578+ if ( item . GetType ( ) . Namespace . StartsWith ( "System" ) )
579+ {
580+ // List of scalar type
581+ processedParameters . Add ( kvp ) ;
582+ }
583+ else
584+ {
585+ // List of custom type
586+ HandleCustomType ( processedParameters , kvp ) ;
587+ }
588+ }
589+ }
590+ else
591+ {
592+ // Scalar type
593+ processedParameters . Add ( kvp ) ;
594+ }
595+ }
596+
588597 /// <summary>
589598 /// Apply serialization to scalar arrays in custom objects.
590599 /// </summary>
0 commit comments