Skip to content

Commit 5d076a6

Browse files
committed
support for json attributes by default
1 parent 9b4e7b8 commit 5d076a6

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/WebApiContrib.Core.Formatter.Csv/CsvFormatterOptions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ public class CsvFormatterOptions
77
public string CsvDelimiter { get; set; } = ";";
88

99
public string Encoding { get; set; } = "ISO-8859-1";
10-
11-
public bool UseJsonPropertyJsonIgnoreAttributes { get; set; } = true;
1210
}
1311
}

src/WebApiContrib.Core.Formatter.Csv/CsvInputFormatter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class CsvInputFormatter : InputFormatter
1919
{
2020
private readonly CsvFormatterOptions _options;
2121

22+
private readonly bool useJsonAttributes = true;
23+
2224
public CsvInputFormatter(CsvFormatterOptions csvFormatterOptions)
2325
{
2426
SupportedMediaTypes.Add(Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/csv"));
@@ -101,7 +103,7 @@ private object ReadStream(Type type, Stream stream)
101103
{
102104
var itemTypeInGeneric = list.GetType().GetTypeInfo().GenericTypeArguments[0];
103105
var item = Activator.CreateInstance(itemTypeInGeneric);
104-
var properties = _options.UseJsonPropertyJsonIgnoreAttributes
106+
var properties = useJsonAttributes
105107
? item.GetType().GetProperties().Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>().Any()).ToArray()
106108
: item.GetType().GetProperties();
107109
// TODO: Maybe refactor to not use positional mapping?, mapping by index could generate errors pretty easily :)

src/WebApiContrib.Core.Formatter.Csv/CsvOutputFormatter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class CsvOutputFormatter : OutputFormatter
2121
{
2222
private readonly CsvFormatterOptions _options;
2323

24+
private readonly bool useJsonAttributes = true;
25+
2426
public string ContentType { get; private set; }
2527

2628
public CsvOutputFormatter(CsvFormatterOptions csvFormatterOptions)
@@ -87,7 +89,7 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
8789

8890
if (_options.UseSingleLineHeaderInCsv)
8991
{
90-
var values = _options.UseJsonPropertyJsonIgnoreAttributes
92+
var values = useJsonAttributes
9193
? itemType.GetProperties().Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>(false).Any()) // Only get the properties that do not define JsonIgnore
9294
.Select(GetDisplayNameFromNewtonsoftJsonAnnotations)
9395
: itemType.GetProperties().Select(pi => pi.GetCustomAttribute<DisplayAttribute>(false)?.Name ?? pi.Name);
@@ -100,7 +102,7 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
100102
{
101103

102104
//IEnumerable<ObjectValue> vals;
103-
var vals = _options.UseJsonPropertyJsonIgnoreAttributes
105+
var vals = useJsonAttributes
104106
? obj.GetType().GetProperties()
105107
.Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>().Any())
106108
.Select(pi => new

0 commit comments

Comments
 (0)