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

Commit af7e807

Browse files
committed
Add NewArray ext method to make mutating and assigning arrays easier
1 parent 265d1b1 commit af7e807

File tree

3 files changed

+64
-46
lines changed

3 files changed

+64
-46
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ public static HashSet<Type> ExcludeTypes
717717
public static string[] IgnoreAttributesNamed
718718
{
719719
set { ReflectionExtensions.IgnoreAttributesNamed = value; }
720+
get { return ReflectionExtensions.IgnoreAttributesNamed; }
720721
}
721722

722723
public static void Reset()

src/ServiceStack.Text/ListExtensions.cs

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,58 @@
99

1010
namespace ServiceStack
1111
{
12-
public static class ListExtensions
13-
{
14-
public static string Join<T>(this IEnumerable<T> values)
15-
{
16-
return Join(values, JsWriter.ItemSeperatorString);
17-
}
18-
19-
public static string Join<T>(this IEnumerable<T> values, string seperator)
20-
{
21-
var sb = new StringBuilder();
22-
foreach (var value in values)
23-
{
24-
if (sb.Length > 0)
25-
sb.Append(seperator);
26-
sb.Append(value);
27-
}
28-
return sb.ToString();
29-
}
30-
31-
public static bool IsNullOrEmpty<T>(this List<T> list)
32-
{
33-
return list == null || list.Count == 0;
34-
}
35-
36-
//TODO: make it work
37-
public static IEnumerable<TFrom> SafeWhere<TFrom>(this List<TFrom> list, Func<TFrom, bool> predicate)
38-
{
39-
return list.Where(predicate);
40-
}
41-
42-
public static int NullableCount<T>(this List<T> list)
43-
{
44-
return list == null ? 0 : list.Count;
45-
}
46-
47-
public static void AddIfNotExists<T>(this List<T> list, T item)
48-
{
49-
if (!list.Contains(item))
50-
list.Add(item);
51-
}
52-
}
12+
public static class ListExtensions
13+
{
14+
public static string Join<T>(this IEnumerable<T> values)
15+
{
16+
return Join(values, JsWriter.ItemSeperatorString);
17+
}
18+
19+
public static string Join<T>(this IEnumerable<T> values, string seperator)
20+
{
21+
var sb = new StringBuilder();
22+
foreach (var value in values)
23+
{
24+
if (sb.Length > 0)
25+
sb.Append(seperator);
26+
sb.Append(value);
27+
}
28+
return sb.ToString();
29+
}
30+
31+
public static bool IsNullOrEmpty<T>(this List<T> list)
32+
{
33+
return list == null || list.Count == 0;
34+
}
35+
36+
//TODO: make it work
37+
public static IEnumerable<TFrom> SafeWhere<TFrom>(this List<TFrom> list, Func<TFrom, bool> predicate)
38+
{
39+
return list.Where(predicate);
40+
}
41+
42+
public static int NullableCount<T>(this List<T> list)
43+
{
44+
return list == null ? 0 : list.Count;
45+
}
46+
47+
public static void AddIfNotExists<T>(this List<T> list, T item)
48+
{
49+
if (!list.Contains(item))
50+
list.Add(item);
51+
}
52+
53+
public static T[] NewArray<T>(this T[] array, T with = null, T without = null) where T : class
54+
{
55+
var to = new List<T>(array);
56+
57+
if (with != null)
58+
to.Add(with);
59+
60+
if (without != null)
61+
to.Remove(without);
62+
63+
return to.ToArray();
64+
}
65+
}
5366
}

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Linq.Expressions;
55
using System.Runtime.Serialization;
6+
using System.Web.Script.Serialization;
67
using NUnit.Framework;
78
using ServiceStack.Text.Tests.DynamicModels;
89

@@ -549,6 +550,9 @@ public class IgnoredModel
549550

550551
[JsonIgnore]
551552
public int JsonIgnoreId { get; set; }
553+
554+
[ScriptIgnore]
555+
public int ScriptIgnoreId { get; set; }
552556
}
553557

554558
//Matches JSON.NET's [JsonIgnore] by name
@@ -557,13 +561,13 @@ public class JsonIgnoreAttribute : AttributeBase { }
557561
[Test]
558562
public void Can_change_ignored_properties()
559563
{
560-
var dto = new IgnoredModel();
564+
JsConfig.IgnoreAttributesNamed = JsConfig.IgnoreAttributesNamed.NewArray(
565+
with: typeof(ScriptIgnoreAttribute).Name,
566+
without: typeof(JsonIgnoreAttribute).Name);
561567

562-
JsConfig.IgnoreAttributesNamed = new[] {
563-
typeof(IgnoreDataMemberAttribute).Name //i.e. Remove [JsonIgnore]
564-
};
568+
var dto = new IgnoredModel { JsonIgnoreId = 1, ScriptIgnoreId = 2 };
565569

566-
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":0,\"JsonIgnoreId\":0}"));
570+
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":0,\"JsonIgnoreId\":1}"));
567571
}
568572
}
569573

0 commit comments

Comments
 (0)