@@ -7,6 +7,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
7
7
using System ;
8
8
using System . Collections ;
9
9
using System . Collections . Generic ;
10
+ using System . Dynamic ;
10
11
using System . Globalization ;
11
12
using System . Linq ;
12
13
using System . Reflection ;
@@ -72,19 +73,28 @@ private string SerializeComplex(object input) {
72
73
StringBuilder output = new StringBuilder ( ) ;
73
74
switch ( input ) {
74
75
case IDictionary dictionary : {
75
- var dictionaryType = dictionary . GetType ( ) ;
76
- if ( dictionaryType . GenericTypeArguments . Count ( ) > 0 ) {
77
- var keyType = dictionaryType . GenericTypeArguments [ 0 ] ;
78
- if ( ! keyType . IsIConvertible ( ) && keyType != typeof ( object ) ) {
79
- throw new Exception ( $ "Can not serialize into associative array with key type { keyType . FullName } ") ;
76
+ if ( input is IPhpObject phpObject ) {
77
+ output . Append ( "O:" ) ;
78
+ output . Append ( phpObject . GetClassName ( ) . Length ) ;
79
+ output . Append ( ":\" " ) ;
80
+ output . Append ( phpObject . GetClassName ( ) ) ;
81
+ output . Append ( "\" :" ) ;
82
+ output . Append ( dictionary . Count ) ;
83
+ output . Append ( ":{" ) ;
84
+ } else {
85
+ var dictionaryType = dictionary . GetType ( ) ;
86
+ if ( dictionaryType . GenericTypeArguments . Count ( ) > 0 ) {
87
+ var keyType = dictionaryType . GenericTypeArguments [ 0 ] ;
88
+ if ( ! keyType . IsIConvertible ( ) && keyType != typeof ( object ) ) {
89
+ throw new Exception ( $ "Can not serialize into associative array with key type { keyType . FullName } ") ;
90
+ }
91
+ }
92
+
93
+ output . Append ( $ "a:{ dictionary . Count } :") ;
94
+ output . Append ( "{" ) ;
80
95
}
81
- }
82
- output . Append ( $ "a:{ dictionary . Count } :") ;
83
- output . Append ( "{" ) ;
84
-
85
96
86
97
foreach ( DictionaryEntry entry in dictionary ) {
87
-
88
98
output . Append ( $ "{ this . Serialize ( entry . Key ) } { Serialize ( entry . Value ) } ") ;
89
99
}
90
100
output . Append ( "}" ) ;
@@ -100,6 +110,8 @@ private string SerializeComplex(object input) {
100
110
output . Append ( "}" ) ;
101
111
return output . ToString ( ) ;
102
112
}
113
+ case DynamicObject :
114
+ throw new System . NotSupportedException ( "Serialization of dynamic objects isn't supported yet." ) ;
103
115
default : {
104
116
var inputType = input . GetType ( ) ;
105
117
@@ -124,7 +136,13 @@ private string SerializeComplex(object input) {
124
136
}
125
137
126
138
private string SerializeToObject ( object input ) {
127
- var className = input . GetType ( ) . GetCustomAttribute < PhpClass > ( ) . Name ;
139
+ string className ;
140
+ if ( input is IPhpObject phpObject ) {
141
+ className = phpObject . GetClassName ( ) ;
142
+ } else {
143
+ className = input . GetType ( ) . GetCustomAttribute < PhpClass > ( ) ? . Name ;
144
+ }
145
+
128
146
if ( string . IsNullOrEmpty ( className ) ) {
129
147
className = "stdClass" ;
130
148
}
0 commit comments