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

Commit 106f2a2

Browse files
committed
don't attempt to serialize reflection types by default
1 parent b3d4b65 commit 106f2a2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Reflection;
45
using System.Runtime.CompilerServices;
56
using System.Runtime.Serialization;
67
using System.Text;
@@ -18,6 +19,12 @@ static JsConfig()
1819
//In-built default serialization, to Deserialize Color struct do:
1920
//JsConfig<System.Drawing.Color>.SerializeFn = c => c.ToString().Replace("Color ", "").Replace("[", "").Replace("]", "");
2021
//JsConfig<System.Drawing.Color>.DeSerializeFn = System.Drawing.Color.FromName;
22+
JsConfig<Type>.SerializeFn = x => x.ToString();
23+
JsConfig<MethodInfo>.SerializeFn = x => x.ToString();
24+
JsConfig<PropertyInfo>.SerializeFn = x => x.ToString();
25+
JsConfig<FieldInfo>.SerializeFn = x => x.ToString();
26+
JsConfig<MemberInfo>.SerializeFn = x => x.ToString();
27+
2128
Reset();
2229
LicenseUtils.Init();
2330
}

tests/ServiceStack.Text.Tests/CyclicalDependencyTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Runtime.Serialization;
67
using NUnit.Framework;
78

@@ -213,5 +214,35 @@ public void Ignore_Cyclical_dependencies()
213214
JsConfig<Node>.OnDeserializedFn = null;
214215
JsConfig.Reset();
215216
}
217+
218+
public class ReflectionType
219+
{
220+
public string Name { get; set; } = "A";
221+
public Type Type { get; set; }
222+
public MethodInfo MethodInfo { get; set; }
223+
public PropertyInfo PropertyInfo { get; set; }
224+
public FieldInfo FieldInfo;
225+
public MemberInfo MemberInfo { get; set; }
226+
227+
public void Method() {}
228+
}
229+
230+
[Test]
231+
public void Can_serialize_POCO_with_Type()
232+
{
233+
var dto = new ReflectionType {
234+
Type = typeof(ReflectionType),
235+
MethodInfo = typeof(ReflectionType).GetMethod(nameof(ReflectionType.Method)),
236+
PropertyInfo = typeof(ReflectionType).GetProperty(nameof(ReflectionType.PropertyInfo)),
237+
FieldInfo = typeof(ReflectionType).GetPublicFields().FirstOrDefault(),
238+
MemberInfo = typeof(ReflectionType).GetMembers().FirstOrDefault(),
239+
};
240+
241+
dto.Name.Print();
242+
dto.ToJson().Print();
243+
dto.ToJsv().Print();
244+
dto.PrintDump();
245+
}
246+
216247
}
217248
}

0 commit comments

Comments
 (0)