Skip to content

Commit 6f3561f

Browse files
committed
Support enums. Fixes #7
1 parent c4543ff commit 6f3561f

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.7.0:
2+
- Support de/serialization of enums
3+
- Added serialization option `NumericEnums`:
4+
Whether or not to serialize enums as integer values
5+
Defaults to true. If set to false, the enum.ToString() representation will be used.
6+
17
# 0.6.0:
28

39
- Allow more (valid) characters in object class names.

PhpSerializerNET.Test/TestEnum.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
This Source Code Form is subject to the terms of the Mozilla Public
4+
License, v. 2.0. If a copy of the MPL was not distributed with this
5+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
**/
7+
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace PhpSerializerNET.Test {
11+
public enum IntEnum : int {
12+
A = 1,
13+
B,
14+
}
15+
public enum LongEnum : long {
16+
A = 1,
17+
B,
18+
}
19+
20+
[TestClass]
21+
public class TestEnum {
22+
23+
[TestMethod]
24+
public void DeserializeEnums() {
25+
Assert.AreEqual(
26+
IntEnum.A,
27+
PhpSerialization.Deserialize<IntEnum>("i:1;")
28+
);
29+
30+
Assert.AreEqual(
31+
LongEnum.A,
32+
PhpSerialization.Deserialize<LongEnum>("i:1;")
33+
);
34+
35+
Assert.AreEqual(
36+
LongEnum.A,
37+
PhpSerialization.Deserialize<LongEnum>("s:1:\"A\";")
38+
);
39+
}
40+
41+
[TestMethod]
42+
public void SerializeOne() {
43+
Assert.AreEqual(
44+
"i:1;",
45+
PhpSerialization.Serialize(IntEnum.A)
46+
);
47+
}
48+
}
49+
}

PhpSerializerNET/Extensions/TypeExtensions.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
using System;
7+
using System.Globalization;
78
using System.Reflection;
89

910
namespace PhpSerializerNET {
@@ -26,5 +27,11 @@ internal static object GetValue(this MemberInfo member, object input) {
2627
}
2728
return null;
2829
}
30+
31+
internal static string GetNumericString(this Enum enumValue) {
32+
return ((IConvertible)enumValue)
33+
.ToType(enumValue.GetType().GetEnumUnderlyingType(), CultureInfo.InvariantCulture)
34+
.ToString();
35+
}
2936
}
3037
}

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ private object DeserializeToken(Type targetType, PhpSerializeToken token) {
132132
case PhpSerializerType.Integer:
133133
case PhpSerializerType.Floating:
134134
case PhpSerializerType.String:
135+
if (targetType.IsEnum){
136+
if (token.Type != PhpSerializerType.String){
137+
return ((IConvertible)token.Value).ToType(targetType.GetEnumUnderlyingType(), CultureInfo.InvariantCulture);
138+
} else {
139+
return targetType
140+
.GetFields()
141+
.FirstOrDefault(y => y.Name == token.Value)
142+
.GetRawConstantValue();
143+
}
144+
}
135145
if (targetType.IsIConvertible()) {
136146
if (targetType == typeof(bool)) {
137147
if (_options.NumberStringToBool && (token.Value == "0" || token.Value == "1")) {

PhpSerializerNET/PhpSerializer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public PhpSerializer(PhpSerializiationOptions options = null) {
2727
public string Serialize(object input) {
2828
StringBuilder output = new StringBuilder();
2929
switch (input) {
30+
case Enum enumValue: {
31+
if (this._options.NumericEnums) {
32+
return $"i:{enumValue.GetNumericString()};";
33+
} else {
34+
return $"i:{enumValue.ToString()};";
35+
}
36+
}
3037
case long longValue: {
3138
return $"i:{longValue.ToString()};";
3239
}

PhpSerializerNET/PhpSerializiationOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ This Source Code Form is subject to the terms of the Mozilla Public
33
License, v. 2.0. If a copy of the MPL was not distributed with this
44
file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
**/
6+
using System;
67

78
namespace PhpSerializerNET {
89

@@ -16,6 +17,12 @@ public class PhpSerializiationOptions {
1617
/// </summary>
1718
public bool ThrowOnCircularReferences { get; set; } = false;
1819

20+
/// <summary>
21+
/// Whether to serialize enums as numeric values (integers) or using their string representation
22+
/// (via <see cref="Enum.ToString()"/>)
23+
/// </summary>
24+
public bool NumericEnums { get; set; } = true;
25+
1926
internal static PhpSerializiationOptions DefaultOptions = new();
2027
}
2128
}

0 commit comments

Comments
 (0)