@@ -19,7 +19,7 @@ internal class PhpSerializer {
1919
2020 public PhpSerializer ( PhpSerializiationOptions options = null ) {
2121 _options = options ?? PhpSerializiationOptions . DefaultOptions ;
22-
22+
2323 _seenObjects = new ( ) ;
2424 }
2525
@@ -54,22 +54,31 @@ public string Serialize(object input) {
5454 case null : {
5555 return "N;" ;
5656 }
57- // TODO: There's enough shared code here to warrant refactoring.
58- // Probably doing this in a default and then have another method for serializing the 3 options.
59- case IDictionary dictionary : {
60- if ( _seenObjects . Contains ( input ) ) {
61- if ( _options . ThrowOnCircularReferences ) {
62- throw new ArgumentException ( "Input object has a circular reference." ) ;
63- }
64- return "N;" ;
65- }
66- if ( dictionary . GetType ( ) . GenericTypeArguments . Count ( ) > 0 ) {
67- var keyType = dictionary . GetType ( ) . GenericTypeArguments [ 0 ] ;
57+ default : {
58+ return this . SerializeComplex ( input ) ;
59+ }
60+ }
61+ }
62+
63+ private string SerializeComplex ( object input ) {
64+ if ( _seenObjects . Contains ( input ) ) {
65+ if ( _options . ThrowOnCircularReferences ) {
66+ throw new ArgumentException ( "Input object has a circular reference." ) ;
67+ }
68+ return "N;" ;
69+ }
70+ _seenObjects . Add ( input ) ;
71+
72+ StringBuilder output = new StringBuilder ( ) ;
73+ switch ( input ) {
74+ case IDictionary dictionary : {
75+ var dictionaryType = dictionary . GetType ( ) ;
76+ if ( dictionaryType . GenericTypeArguments . Count ( ) > 0 ) {
77+ var keyType = dictionaryType . GenericTypeArguments [ 0 ] ;
6878 if ( ! keyType . IsIConvertible ( ) && keyType != typeof ( object ) ) {
6979 throw new Exception ( $ "Can not serialize into associative array with key type { keyType . FullName } ") ;
7080 }
7181 }
72- _seenObjects . Add ( input ) ;
7382 output . Append ( $ "a:{ dictionary . Count } :") ;
7483 output . Append ( "{" ) ;
7584
@@ -82,13 +91,6 @@ public string Serialize(object input) {
8291 return output . ToString ( ) ;
8392 }
8493 case IList collection : {
85- if ( _seenObjects . Contains ( input ) ) {
86- if ( _options . ThrowOnCircularReferences ) {
87- throw new ArgumentException ( "Input object has a circular reference." ) ;
88- }
89- return "N;" ;
90- }
91- _seenObjects . Add ( input ) ;
9294 output . Append ( $ "a:{ collection . Count } :") ;
9395 output . Append ( "{" ) ;
9496 for ( int i = 0 ; i < collection . Count ; i ++ ) {
@@ -99,15 +101,8 @@ public string Serialize(object input) {
99101 return output . ToString ( ) ;
100102 }
101103 default : {
102- if ( _seenObjects . Contains ( input ) ) {
103- if ( _options . ThrowOnCircularReferences ) {
104- throw new ArgumentException ( "Input object has a circular reference." ) ;
105- }
106- return "N;" ; // See above.
107- }
108- _seenObjects . Add ( input ) ;
109104 var inputType = input . GetType ( ) ;
110-
105+
111106 if ( inputType . GetCustomAttribute < PhpClass > ( ) != null ) // TODO: add option.
112107 {
113108 return this . SerializeToObject ( input ) ;
0 commit comments