@@ -127,12 +127,7 @@ private object DeserializeInteger(Type targetType, in PhpToken token) {
127127 TypeCode . UInt32 => uint . Parse ( token . Value . GetSlice ( _input ) , CultureInfo . InvariantCulture ) ,
128128 TypeCode . UInt64 => ulong . Parse ( token . Value . GetSlice ( _input ) , CultureInfo . InvariantCulture ) ,
129129 TypeCode . SByte => sbyte . Parse ( token . Value . GetSlice ( _input ) , CultureInfo . InvariantCulture ) ,
130- _ => this . DeserializeTokenFromSimpleType (
131- targetType ,
132- token . Type ,
133- this . GetString ( token ) ,
134- token . Position
135- ) ,
130+ _ => this . DeserializeTokenFromSimpleType ( targetType , token . Type , this . GetString ( token ) , token . Position ) ,
136131 } ;
137132 }
138133
@@ -143,7 +138,7 @@ private object DeserializeDouble(Type targetType, in PhpToken token) {
143138
144139 string value = this . GetString ( token ) ;
145140 if ( value == "INF" ) {
146- value = double . PositiveInfinity . ToString ( CultureInfo . InvariantCulture ) ;
141+ value = double . PositiveInfinity . ToString ( CultureInfo . InvariantCulture ) ;
147142 } else if ( value == "-INF" ) {
148143 value = double . NegativeInfinity . ToString ( CultureInfo . InvariantCulture ) ;
149144 }
@@ -159,13 +154,12 @@ private object DeserializeBoolean(Type targetType, in PhpToken token) {
159154 underlyingType = targetType . GenericTypeArguments [ 0 ] ;
160155 }
161156
162- if ( underlyingType . IsIConvertible ( ) ) {
163- return ( ( IConvertible ) token . Value . GetBool ( _input ) ) . ToType ( underlyingType , CultureInfo . InvariantCulture ) ;
164- } else {
157+ if ( ! underlyingType . IsIConvertible ( ) ) {
165158 throw new DeserializationException (
166159 $ "Can not assign value \" { this . GetString ( token ) } \" (at position { token . Position } ) to target type of { targetType . Name } ."
167160 ) ;
168161 }
162+ return ( ( IConvertible ) token . Value . GetBool ( _input ) ) . ToType ( underlyingType , CultureInfo . InvariantCulture ) ;
169163 }
170164
171165 private object DeserializeTokenFromSimpleType (
@@ -351,7 +345,7 @@ private object MakeObject(Type targetType, in PhpToken token) {
351345 DeserializeToken ( property . PropertyType )
352346 ) ;
353347 } catch ( Exception exception ) {
354- var valueToken = _tokens [ _currentToken - 1 ] ;
348+ var valueToken = _tokens [ _currentToken - 1 ] ;
355349 throw new DeserializationException (
356350 $ "Exception encountered while trying to assign '{ this . GetString ( valueToken ) } ' to { targetType . Name } .{ property . Name } . See inner exception for details.",
357351 exception
@@ -368,24 +362,24 @@ private object MakeArray(Type targetType, in PhpToken token) {
368362 var elementType = targetType . GetElementType ( ) ?? throw new InvalidOperationException ( "targetType.GetElementType() returned null" ) ;
369363 Array result = Array . CreateInstance ( elementType , token . Length ) ;
370364
371- var arrayIndex = 0 ;
372- for ( int i = 0 ; i < token . Length ; i ++ ) {
373- _currentToken ++ ;
374- result . SetValue (
375- elementType == typeof ( object )
376- ? DeserializeToken ( )
377- : DeserializeToken ( elementType ) ,
378- arrayIndex
379- ) ;
380- arrayIndex ++ ;
365+ if ( elementType == typeof ( object ) ) {
366+ for ( int i = 0 ; i < token . Length ; i ++ ) {
367+ _currentToken ++ ;
368+ result . SetValue ( DeserializeToken ( ) , i ) ;
369+ }
370+ } else {
371+ for ( int i = 0 ; i < token . Length ; i ++ ) {
372+ _currentToken ++ ;
373+ result . SetValue ( DeserializeToken ( elementType ) , i ) ;
374+ }
381375 }
382376 return result ;
383377 }
384378
385379 private object MakeList ( Type targetType , in PhpToken token ) {
386- for ( int i = 0 ; i < token . Length * 2 ; i += 2 ) {
387- if ( this . _tokens [ _currentToken + i ] . Type != PhpDataType . Integer ) {
388- var badToken = this . _tokens [ _currentToken + i ] ;
380+ for ( int i = 0 ; i < token . Length * 2 ; i += 2 ) {
381+ if ( this . _tokens [ _currentToken + i ] . Type != PhpDataType . Integer ) {
382+ var badToken = this . _tokens [ _currentToken + i ] ;
389383 throw new DeserializationException (
390384 $ "Can not deserialize array at position { token . Position } to list: " +
391385 $ "It has a non-integer key '{ this . GetString ( badToken ) } ' at element { i } (position { badToken . Position } )."
@@ -396,24 +390,23 @@ private object MakeList(Type targetType, in PhpToken token) {
396390 if ( targetType . IsArray ) {
397391 return MakeArray ( targetType , token ) ;
398392 }
399- var result = ( IList ) Activator . CreateInstance ( targetType , token . Length ) ;
400- if ( result == null ) {
393+ if ( Activator . CreateInstance ( targetType , token . Length ) is not IList result ) {
401394 throw new NullReferenceException ( "Activator.CreateInstance(targetType) returned null" ) ;
402395 }
403- Type itemType ;
404- if ( targetType . GenericTypeArguments . Length >= 1 ) {
405- itemType = targetType . GenericTypeArguments [ 0 ] ;
406- } else {
407- itemType = typeof ( object ) ;
408- }
396+ Type itemType = targetType . GenericTypeArguments . Length >= 1
397+ ? targetType . GenericTypeArguments [ 0 ]
398+ : typeof ( object ) ;
409399
410- for ( int i = 0 ; i < token . Length ; i ++ ) {
411- _currentToken ++ ;
412- result . Add (
413- itemType == typeof ( object )
414- ? DeserializeToken ( )
415- : DeserializeToken ( itemType )
416- ) ;
400+ if ( itemType == typeof ( object ) ) {
401+ for ( int i = 0 ; i < token . Length ; i ++ ) {
402+ _currentToken ++ ;
403+ result . Add ( DeserializeToken ( ) ) ;
404+ }
405+ } else {
406+ for ( int i = 0 ; i < token . Length ; i ++ ) {
407+ _currentToken ++ ;
408+ result . Add ( DeserializeToken ( itemType ) ) ;
409+ }
417410 }
418411 return result ;
419412 }
@@ -425,10 +418,7 @@ private object MakeDictionary(Type targetType, in PhpToken token) {
425418 }
426419 if ( ! targetType . GenericTypeArguments . Any ( ) ) {
427420 for ( int i = 0 ; i < token . Length ; i ++ ) {
428- result . Add (
429- DeserializeToken ( ) ,
430- DeserializeToken ( )
431- ) ;
421+ result . Add ( DeserializeToken ( ) , DeserializeToken ( ) ) ;
432422 }
433423 return result ;
434424 }
@@ -455,12 +445,12 @@ private object MakeCollection(in PhpToken token) {
455445 long previousKey = - 1 ;
456446 bool isList = true ;
457447 bool consecutive = true ;
458- for ( int i = 0 ; i < token . Length * 2 ; i += 2 ) {
459- if ( this . _tokens [ _currentToken + i ] . Type != PhpDataType . Integer ) {
448+ for ( int i = 0 ; i < token . Length * 2 ; i += 2 ) {
449+ if ( this . _tokens [ _currentToken + i ] . Type != PhpDataType . Integer ) {
460450 isList = false ;
461451 break ;
462452 } else {
463- var key = this . _tokens [ _currentToken + i ] . Value . GetLong ( _input ) ;
453+ var key = this . _tokens [ _currentToken + i ] . Value . GetLong ( _input ) ;
464454 if ( i == 0 || key == previousKey + 1 ) {
465455 previousKey = key ;
466456 } else {
@@ -471,10 +461,7 @@ private object MakeCollection(in PhpToken token) {
471461 if ( ! isList || ( this . _options . UseLists == ListOptions . Default && consecutive == false ) ) {
472462 var result = new Dictionary < object , object > ( token . Length ) ;
473463 for ( int i = 0 ; i < token . Length ; i ++ ) {
474- result . Add (
475- this . DeserializeToken ( ) ,
476- this . DeserializeToken ( )
477- ) ;
464+ result . Add ( this . DeserializeToken ( ) , this . DeserializeToken ( ) ) ;
478465 }
479466 return result ;
480467 } else {
0 commit comments