Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2b0952e

Browse files
committed
Support serializing delegates in Dump(), skip in JSON/JSV
1 parent 5aeab04 commit 2b0952e

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/ServiceStack.Text/JsonSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static object DeserializeFromReader(TextReader reader, Type type)
5454

5555
public static string SerializeToString<T>(T value)
5656
{
57-
if (value == null) return null;
57+
if (value == null || value is Delegate) return null;
5858
if (typeof(T) == typeof(object) || typeof(T).IsAbstract() || typeof(T).IsInterface())
5959
{
6060
if (typeof(T).IsAbstract() || typeof(T).IsInterface()) JsState.IsWritingDynamic = true;

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static object DeserializeFromReader(TextReader reader, Type type)
7878

7979
public static string SerializeToString<T>(T value)
8080
{
81-
if (value == null) return null;
81+
if (value == null || value is Delegate) return null;
8282
if (typeof(T) == typeof(string)) return value as string;
8383
if (typeof(T) == typeof(object) || typeof(T).IsAbstract() || typeof(T).IsInterface())
8484
{
@@ -239,9 +239,30 @@ public static void Print(this string text, params object[] args)
239239

240240
public static string SerializeAndFormat<T>(this T instance)
241241
{
242+
var fn = instance as Delegate;
243+
if (fn != null)
244+
return Dump(fn);
245+
242246
var dtoStr = SerializeToString(instance);
243247
var formatStr = JsvFormatter.Format(dtoStr);
244248
return formatStr;
245249
}
250+
251+
public static string Dump(this Delegate fn)
252+
{
253+
var method = fn.GetType().GetMethod("Invoke");
254+
var sb = new StringBuilder();
255+
foreach (var param in method.GetParameters())
256+
{
257+
if (sb.Length > 0)
258+
sb.Append(", ");
259+
260+
sb.AppendFormat("{0} {1}", param.ParameterType.Name, param.Name);
261+
}
262+
263+
var info = "{0} {1}({2})".Fmt(method.ReturnType.Name, fn.Method.Name, sb);
264+
return info;
265+
}
266+
246267
}
247268
}

tests/ServiceStack.Text.Tests/SpecialTypesTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,32 @@ public void Can_Serialize_HashTable()
6464
Assert.That(fromJson["B"].ToString(), Is.EqualTo(h["B"].ToString()));
6565
}
6666

67+
[Test]
68+
public void Can_serialize_delegate()
69+
{
70+
Action x = () => { };
71+
72+
Assert.That(x.ToJson(), Is.Null);
73+
Assert.That(x.ToJsv(), Is.Null);
74+
Assert.That(x.Dump(), Is.Not.Null);
75+
}
76+
77+
string MethodWithArgs(int id, string name)
78+
{
79+
return null;
80+
}
81+
82+
[Test]
83+
public void Does_dump_delegate_info()
84+
{
85+
Action d = Can_Serialize_ByteArray;
86+
Assert.That(d.Dump(), Is.EqualTo("Void Can_Serialize_ByteArray()"));
87+
88+
Func<int, string, string> methodWithArgs = MethodWithArgs;
89+
Assert.That(methodWithArgs.Dump(), Is.EqualTo("String MethodWithArgs(Int32 arg1, String arg2)"));
90+
91+
Action x = () => { };
92+
Assert.That(x.Dump(), Is.EqualTo("Void <Does_dump_delegate_info>b__4()"));
93+
}
6794
}
6895
}

0 commit comments

Comments
 (0)