Skip to content

Commit 6e32821

Browse files
committed
Support string to System.Guid deserialization.
Fixes #10
1 parent 6303413 commit 6e32821

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.7.1:
2+
- Fixed issue with nested array / object validation (issue #11)
3+
- Added support for System.Guid (issue #10)
4+
5+
16
# 0.7.0:
27
- Support de/serialization of enums
38
- Added serialization option `NumericEnums`:

PhpSerializerNET.Test/DeserializeObjects.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class MappedClass {
2626

2727
[PhpIgnore]
2828
public string it { get; set; }
29+
30+
public Guid guid {get;set;}
2931
}
3032

3133
[TestMethod]
@@ -65,7 +67,7 @@ public void DeserializeObjectWithExcessKeys() {
6567

6668
public void Test_Issue11() {
6769
var deserializedObject = PhpSerialization.Deserialize(
68-
"a:1:{i:0;a:7:{s:1:\"A\";N;s:1:\"B\";N;s:1:\"C\";s:1:\"C\";s:5:\"odSdr\";i:1;s:1:\"D\";d:1;s:1:\"E\";N;s:1:\"F\";a:3:{s:1:\"X\";i:8;s:1:\"Y\";N;s:1:\"Z\";N;}}}",
70+
"a:1:{i:0;a:7:{s:1:\"A\";N;s:1:\"B\";N;s:1:\"C\";s:1:\"C\";s:5:\"odSdr\";i:1;s:1:\"D\";d:1;s:1:\"E\";N;s:1:\"F\";a:3:{s:1:\"X\";i:8;s:1:\"Y\";N;s:1:\"Z\";N;}}}"
6971
);
7072
Assert.IsNotNull(deserializedObject);
7173
}
@@ -79,6 +81,15 @@ public void ThrowsOnExcessKeys() {
7981
Assert.AreEqual("Could not bind the key \"es\" to object of type MappedClass: No such property.", ex.Message);
8082
}
8183

84+
[TestMethod]
85+
public void AssignsGuids() {
86+
var result = PhpSerialization.Deserialize<MappedClass>(
87+
"a:1:{s:4:\"guid\";s:36:\"82e2ebf0-43e6-4c10-82cf-57d60383a6be\";}",
88+
new PhpDeserializationOptions() { AllowExcessKeys = true }
89+
);
90+
Assert.AreEqual(new Guid("82e2ebf0-43e6-4c10-82cf-57d60383a6be"), result.guid);
91+
}
92+
8293

8394
[TestMethod]
8495
public void DeserializeList() {

PhpSerializerNET.Test/DeserializePrimitives.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
**/
66

7+
using System;
78
using Microsoft.VisualStudio.TestTools.UnitTesting;
89

910
namespace PhpSerializerNET.Test {
@@ -69,6 +70,12 @@ public void DeserializeLong() {
6970
);
7071
}
7172

73+
[TestMethod]
74+
public void DeserializeGUID() {
75+
Guid guid = PhpSerialization.Deserialize<Guid>("s:36:\"82e2ebf0-43e6-4c10-82cf-57d60383a6be\";");
76+
Assert.AreEqual("82e2ebf0-43e6-4c10-82cf-57d60383a6be", guid.ToString());
77+
}
78+
7279
[TestMethod]
7380
public void DeserializesDouble() {
7481
Assert.AreEqual(

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ private object DeserializeToken(Type targetType, PhpSerializeToken token) {
149149
}
150150
}
151151
return ((IConvertible)token.Value).ToType(targetType, CultureInfo.InvariantCulture);
152+
} else if(targetType == typeof(System.Guid)) {
153+
return new Guid(token.Value);
152154
} else {
153155
throw new DeserializationException(
154156
$"Can not assign value \"{token.Value}\" (at position {token.Position}) to target type of {targetType.Name}."

0 commit comments

Comments
 (0)