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

Commit 3b9972f

Browse files
committed
Add JsConfig.IgnoreAttributesNamed to change what attributes ignore properties
1 parent b07cf03 commit 3b9972f

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,11 @@ public static HashSet<Type> ExcludeTypes
647647
}
648648
}
649649

650+
public static string[] IgnoreAttributesNamed
651+
{
652+
set { ReflectionExtensions.IgnoreAttributesNamed = value; }
653+
}
654+
650655
public static void Reset()
651656
{
652657
foreach (var rawSerializeType in HasSerializeFn.ToArray())
@@ -692,6 +697,7 @@ public static void Reset()
692697
sParsePrimitiveIntegerTypes = null;
693698
sParsePrimitiveFloatingPointTypes = null;
694699
PlatformExtensions.ClearRuntimeAttributes();
700+
ReflectionExtensions.Reset();
695701
}
696702

697703
static void Reset(Type cachesForType)

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,19 @@ public static PropertyInfo[] GetPublicProperties(this Type type)
632632
}
633633

634634
public const string DataMember = "DataMemberAttribute";
635-
public const string IgnoreDataMember = "IgnoreDataMemberAttribute";
636-
public const string JsonIgnoreMembar = "JsonIgnoreAttribute";
635+
636+
internal static string[] IgnoreAttributesNamed = new[] {
637+
"IgnoreDataMemberAttribute",
638+
"JsonIgnoreAttribute"
639+
};
640+
641+
internal static void Reset()
642+
{
643+
IgnoreAttributesNamed = new[] {
644+
"IgnoreDataMemberAttribute",
645+
"JsonIgnoreAttribute"
646+
};
647+
}
637648

638649
public static PropertyInfo[] GetSerializableProperties(this Type type)
639650
{
@@ -648,12 +659,11 @@ public static PropertyInfo[] GetSerializableProperties(this Type type)
648659

649660
// else return those properties that are not decorated with IgnoreDataMember
650661
return publicReadableProperties
651-
.Where( prop => prop.AllAttributes().All( attr =>
652-
{
662+
.Where(prop => prop.AllAttributes().All(attr => {
653663
var name = attr.GetType().Name;
654-
return name != IgnoreDataMember && name != JsonIgnoreMembar;
655-
} ) )
656-
.Where( prop => !JsConfig.ExcludeTypes.Contains( prop.PropertyType ) )
664+
return !IgnoreAttributesNamed.Contains(name);
665+
}))
666+
.Where(prop => !JsConfig.ExcludeTypes.Contains(prop.PropertyType))
657667
.ToArray();
658668
}
659669

@@ -672,7 +682,8 @@ public static FieldInfo[] GetSerializableFields(this Type type)
672682

673683
// else return those properties that are not decorated with IgnoreDataMember
674684
return publicFields
675-
.Where(prop => prop.AllAttributes().All(attr => attr.GetType().Name != IgnoreDataMember))
685+
.Where(prop => prop.AllAttributes()
686+
.All(attr => !IgnoreAttributesNamed.Contains(attr.GetType().Name)))
676687
.Where(prop => !JsConfig.ExcludeTypes.Contains(prop.FieldType))
677688
.ToArray();
678689
}
@@ -1299,7 +1310,7 @@ public static TAttribute FirstAttribute<TAttribute>(this MemberInfo memberInfo)
12991310
{
13001311
return memberInfo.AllAttributes<TAttribute>().FirstOrDefault();
13011312
}
1302-
1313+
13031314
public static TAttribute FirstAttribute<TAttribute>(this ParameterInfo paramInfo)
13041315
{
13051316
return paramInfo.AllAttributes<TAttribute>().FirstOrDefault();

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public class ModelWithIgnoredFields
152152
public int Ignored { get; set; }
153153
}
154154

155-
public class ReadOnlyAttribute : AttributeBase {}
155+
public class ReadOnlyAttribute : AttributeBase { }
156156

157157
[TestFixture]
158158
public class AutoMappingTests
@@ -526,8 +526,8 @@ public void Does_ignore_properties_without_attributes()
526526
{
527527
var model = new ModelWithIgnoredFields
528528
{
529-
Id = 1,
530-
Name = "Foo",
529+
Id = 1,
530+
Name = "Foo",
531531
Ignored = 2
532532
};
533533

@@ -539,5 +539,30 @@ public void Does_ignore_properties_without_attributes()
539539
Assert.That(dto.Ignored, Is.EqualTo(10));
540540
}
541541

542+
public class IgnoredModel
543+
{
544+
public int Id { get; set; }
545+
546+
[IgnoreDataMember]
547+
public int DataMemberIgnoreId { get; set; }
548+
549+
[JsonIgnore]
550+
public int JsonIgnoreId { get; set; }
551+
}
552+
553+
//Matches JSON.NET's [JsonIgnore] by name
554+
public class JsonIgnoreAttribute : AttributeBase { }
555+
556+
[Test]
557+
public void Can_change_ignored_properties()
558+
{
559+
var dto = new IgnoredModel();
560+
561+
JsConfig.IgnoreAttributesNamed = new[] {
562+
typeof(IgnoreDataMemberAttribute).Name //i.e. Remove [JsonIgnore]
563+
};
564+
565+
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":0,\"JsonIgnoreId\":0}"));
566+
}
542567
}
543568
}

0 commit comments

Comments
 (0)