Skip to content

Commit 18c4d0c

Browse files
committed
Support serialization of IPhpObject.
1 parent 6d2403d commit 18c4d0c

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

PhpSerializerNET.Test/TestObjects.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,27 @@ public void DeserializesToSpecifiedDictionary() {
134134
Assert.AreEqual(2.718, result["Jane"]);
135135
}
136136

137-
138-
139137
[TestMethod]
140138
public void DeserializeNesting(){
141-
var result = (List<dynamic>)PhpSerialization.Deserialize(
142-
"a:1:{i:0;O:14:\"ABC\\Epsilon\\42\":3:{s:4:\"date\";O:8:\"DateTime\":3:{s:4:\"date\";s:26:\"2021-08-18 09:10:23.441055\";s:13:\"timezone_type\";i:3;s:8:\"timezone\";s:3:\"UTC\";}}}",
143-
new PhpDeserializationOptions() { StdClass = StdClassOption.Dynamic}
139+
var result = (List<object>)PhpSerialization.Deserialize(
140+
"a:1:{i:0;O:14:\"ABC\\Epsilon\\42\":1:{s:6:\"People\";O:6:\"people\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}}}"
144141
);
142+
var firstEntry = (PhpObjectDictionary)result[0];
145143
Assert.AreEqual(1, result.Count);
146-
Assert.AreEqual("ABC\\Epsilon\\42", result[0].GetClassName());
147-
Assert.AreEqual("2021-08-18 09:10:23.441055", result[0].date.Date);
144+
Assert.AreEqual("ABC\\Epsilon\\42", firstEntry.GetClassName());
145+
Assert.AreEqual(
146+
3.14,
147+
((PhpObjectDictionary)firstEntry["People"])["John"]
148+
);
149+
Assert.AreEqual(
150+
2.718,
151+
((PhpObjectDictionary)firstEntry["People"])["Jane"]
152+
);
153+
154+
Assert.AreEqual(
155+
"a:1:{i:0;O:14:\"ABC\\Epsilon\\42\":1:{s:6:\"People\";O:6:\"people\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}}}",
156+
PhpSerialization.Serialize(result)
157+
);
148158
}
149159
}
150160
}

PhpSerializerNET/PhpSerializer.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
77
using System;
88
using System.Collections;
99
using System.Collections.Generic;
10+
using System.Dynamic;
1011
using System.Globalization;
1112
using System.Linq;
1213
using System.Reflection;
@@ -72,19 +73,28 @@ private string SerializeComplex(object input) {
7273
StringBuilder output = new StringBuilder();
7374
switch (input) {
7475
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("{");
8095
}
81-
}
82-
output.Append($"a:{dictionary.Count}:");
83-
output.Append("{");
84-
8596

8697
foreach (DictionaryEntry entry in dictionary) {
87-
8898
output.Append($"{this.Serialize(entry.Key)}{Serialize(entry.Value)}");
8999
}
90100
output.Append("}");
@@ -100,6 +110,8 @@ private string SerializeComplex(object input) {
100110
output.Append("}");
101111
return output.ToString();
102112
}
113+
case DynamicObject:
114+
throw new System.NotSupportedException("Serialization of dynamic objects isn't supported yet.");
103115
default: {
104116
var inputType = input.GetType();
105117

@@ -124,7 +136,13 @@ private string SerializeComplex(object input) {
124136
}
125137

126138
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+
128146
if (string.IsNullOrEmpty(className)) {
129147
className = "stdClass";
130148
}

0 commit comments

Comments
 (0)