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

Commit 045ffac

Browse files
committed
Serialize fields explicitly marked with [DataMember] attribute
1 parent c0fa9f0 commit 045ffac

File tree

4 files changed

+251
-211
lines changed

4 files changed

+251
-211
lines changed

src/ServiceStack.Text/Common/DeserializeTypeRef.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static Dictionary<string, TypeAccessor> GetTypeAccessorMap(TypeConfig t
3838
var type = typeConfig.Type;
3939

4040
var propertyInfos = type.GetSerializableProperties();
41-
var fieldInfos = JsConfig.IncludePublicFields ? type.GetSerializableFields() : new FieldInfo[0];
41+
var fieldInfos = type.GetSerializableFields();
4242
if (propertyInfos.Length == 0 && fieldInfos.Length == 0) return null;
4343

4444
var map = new Dictionary<string, TypeAccessor>(StringComparer.OrdinalIgnoreCase);

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static bool Init()
112112
if (!typeof(T).IsClass() && !typeof(T).IsInterface() && !JsConfig.TreatAsRefType(typeof(T))) return false;
113113

114114
var propertyInfos = TypeConfig<T>.Properties;
115-
var fieldInfos = JsConfig.IncludePublicFields ? TypeConfig<T>.Fields : new FieldInfo[0];
115+
var fieldInfos = TypeConfig<T>.Fields;
116116
var propertyNamesLength = propertyInfos.Length;
117117
var fieldNamesLength = fieldInfos.Length;
118118
PropertyWriters = new TypePropertyWriter[propertyNamesLength + fieldNamesLength];

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,13 @@ public static FieldInfo[] GetSerializableFields(this Type type)
656656
{
657657
if (type.IsDto())
658658
{
659-
return new FieldInfo[0];
659+
return type.GetAllFields().Where(attr =>
660+
attr.HasAttribute<DataMemberAttribute>()).ToArray();
660661
}
661662

663+
if (!JsConfig.IncludePublicFields)
664+
return new FieldInfo[0];
665+
662666
var publicFields = type.GetPublicFields();
663667

664668
// else return those properties that are not decorated with IgnoreDataMember
@@ -850,6 +854,20 @@ public static PropertyInfo[] Properties(this Type type)
850854
#endif
851855
}
852856

857+
public static FieldInfo[] GetAllFields(this Type type)
858+
{
859+
if (type.IsInterface())
860+
{
861+
return new FieldInfo[0];
862+
}
863+
864+
#if (NETFX_CORE || PCL)
865+
return type.GetRuntimeFields().Where(p => !p.isStatic).ToArray();
866+
#else
867+
return type.GetPublicFields();
868+
#endif
869+
}
870+
853871
public static FieldInfo[] GetPublicFields(this Type type)
854872
{
855873
if (type.IsInterface())

0 commit comments

Comments
 (0)