Skip to content

Commit 39b560c

Browse files
sommmenStringEpsilon
authored andcommitted
Add option to Deserialize to target type w/o type parameter
1 parent 1010eba commit 39b560c

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

PhpSerializerNET/PhpDeserializer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ public object Deserialize() {
3535
return this.DeserializeToken(this._token);
3636
}
3737

38+
public object Deserialize(Type targetType) {
39+
return this.DeserializeToken(targetType, this._token);
40+
}
41+
3842
public T Deserialize<T>() {
39-
Type targetType = typeof(T);
40-
return (T)this.DeserializeToken(targetType, this._token);
43+
return (T) this.Deserialize(typeof(T));
4144
}
4245

4346
/// <summary>

PhpSerializerNET/PhpSerialization.cs

Lines changed: 34 additions & 4 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 System.Collections.Generic;
89

910
namespace PhpSerializerNET {
@@ -30,7 +31,7 @@ public static class PhpSerialization {
3031
/// </returns>
3132
public static object Deserialize(string input, PhpDeserializationOptions options = null) {
3233
if (string.IsNullOrEmpty(input)) {
33-
throw new System.ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
34+
throw new ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
3435
}
3536
return new PhpDeserializer(input, options).Deserialize();
3637
}
@@ -56,11 +57,38 @@ public static T Deserialize<T>(
5657
PhpDeserializationOptions options = null
5758
) {
5859
if (string.IsNullOrEmpty(input)) {
59-
throw new System.ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
60+
throw new ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
6061
}
6162
return new PhpDeserializer(input, options).Deserialize<T>();
6263
}
6364

65+
/// <summary>
66+
/// The serialized data to deserialize.
67+
/// </summary>
68+
/// <param name="input">
69+
/// Data in the PHP de/serialization format.
70+
/// </param>
71+
/// <param name="options">
72+
/// Options for deserialization. See the <see cref="PhpDeserializationOptions"/> class for more details.
73+
/// </param>
74+
/// <param name="type">
75+
/// The desired output type.
76+
/// This should be one of the primitives or a class with a public parameterless constructor.
77+
/// </typeparam>
78+
/// <returns>
79+
/// The deserialized object.
80+
/// </returns>
81+
public static object Deserialize(
82+
string input,
83+
Type type,
84+
PhpDeserializationOptions options = null
85+
) {
86+
if (string.IsNullOrEmpty(input)) {
87+
throw new ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
88+
}
89+
return new PhpDeserializer(input, options).Deserialize(type);
90+
}
91+
6492
/// <summary>
6593
/// Serialize an object into the PHP format.
6694
/// </summary>
@@ -81,12 +109,14 @@ public static string Serialize(object input, PhpSerializiationOptions options =
81109
/// Reset the type lookup cache.
82110
/// Can be useful for scenarios in which new types are loaded at runtime in between deserialization tasks.
83111
/// </summary>
84-
public static void ClearTypeCache() => PhpDeserializer.ClearTypeCache();
112+
public static void ClearTypeCache() =>
113+
PhpDeserializer.ClearTypeCache();
85114

86115
/// <summary>
87116
/// Reset the property info cache.
88117
/// Can be useful for scenarios in which new types are loaded at runtime in between deserialization tasks.
89118
/// </summary>
90-
public static void ClearPropertyInfoCache() => PhpDeserializer.ClearPropertyInfoCache();
119+
public static void ClearPropertyInfoCache() =>
120+
PhpDeserializer.ClearPropertyInfoCache();
91121
}
92122
}

0 commit comments

Comments
 (0)