Skip to content

Commit

Permalink
suggested improvements to CSV formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Jun 5, 2018
1 parent 7b0999f commit 924e1ae
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

namespace WebApiContrib.Core.Samples.Model
{
public class LocalizationRecord
{
//[JsonIgnore]
public long Id { get; set; }
public string Key { get; set; }
public string Text { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class CsvFormatterOptions

public string Encoding { get; set; } = "ISO-8859-1";

public bool UseNewtonsoftJsonDataAnnotations { get; set; } = false;
public bool UseJsonPropertyJsonIgnoreAttributes { get; set; } = true;
}
}
2 changes: 1 addition & 1 deletion src/WebApiContrib.Core.Formatter.Csv/CsvInputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private object ReadStream(Type type, Stream stream)
{
var itemTypeInGeneric = list.GetType().GetTypeInfo().GenericTypeArguments[0];
var item = Activator.CreateInstance(itemTypeInGeneric);
var properties = _options.UseNewtonsoftJsonDataAnnotations
var properties = _options.UseJsonPropertyJsonIgnoreAttributes
? item.GetType().GetProperties().Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>().Any()).ToArray()
: item.GetType().GetProperties();
// TODO: Maybe refactor to not use positional mapping?, mapping by index could generate errors pretty easily :)
Expand Down
4 changes: 2 additions & 2 deletions src/WebApiContrib.Core.Formatter.Csv/CsvOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co

if (_options.UseSingleLineHeaderInCsv)
{
var values = _options.UseNewtonsoftJsonDataAnnotations
var values = _options.UseJsonPropertyJsonIgnoreAttributes
? itemType.GetProperties().Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>(false).Any()) // Only get the properties that do not define JsonIgnore
.Select(GetDisplayNameFromNewtonsoftJsonAnnotations)
: itemType.GetProperties().Select(pi => pi.GetCustomAttribute<DisplayAttribute>(false)?.Name ?? pi.Name);
Expand All @@ -100,7 +100,7 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
{

//IEnumerable<ObjectValue> vals;
var vals = _options.UseNewtonsoftJsonDataAnnotations
var vals = _options.UseJsonPropertyJsonIgnoreAttributes
? obj.GetType().GetProperties()
.Where(pi => !pi.GetCustomAttributes<JsonIgnoreAttribute>().Any())
.Select(pi => new
Expand Down

0 comments on commit 924e1ae

Please sign in to comment.