@@ -19,7 +19,7 @@ internal class PhpSerializer {
19
19
20
20
public PhpSerializer ( PhpSerializiationOptions options = null ) {
21
21
_options = options ?? PhpSerializiationOptions . DefaultOptions ;
22
-
22
+
23
23
_seenObjects = new ( ) ;
24
24
}
25
25
@@ -54,22 +54,31 @@ public string Serialize(object input) {
54
54
case null : {
55
55
return "N;" ;
56
56
}
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 ] ;
68
78
if ( ! keyType . IsIConvertible ( ) && keyType != typeof ( object ) ) {
69
79
throw new Exception ( $ "Can not serialize into associative array with key type { keyType . FullName } ") ;
70
80
}
71
81
}
72
- _seenObjects . Add ( input ) ;
73
82
output . Append ( $ "a:{ dictionary . Count } :") ;
74
83
output . Append ( "{" ) ;
75
84
@@ -82,13 +91,6 @@ public string Serialize(object input) {
82
91
return output . ToString ( ) ;
83
92
}
84
93
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 ) ;
92
94
output . Append ( $ "a:{ collection . Count } :") ;
93
95
output . Append ( "{" ) ;
94
96
for ( int i = 0 ; i < collection . Count ; i ++ ) {
@@ -99,15 +101,8 @@ public string Serialize(object input) {
99
101
return output . ToString ( ) ;
100
102
}
101
103
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 ) ;
109
104
var inputType = input . GetType ( ) ;
110
-
105
+
111
106
if ( inputType . GetCustomAttribute < PhpClass > ( ) != null ) // TODO: add option.
112
107
{
113
108
return this . SerializeToObject ( input ) ;
0 commit comments