8
8
using System . Reflection ;
9
9
using Microsoft . AspNetCore . Mvc . Formatters ;
10
10
using System . ComponentModel . DataAnnotations ;
11
- using Newtonsoft . Json ;
12
11
13
12
namespace WebApiContrib . Core . Formatter . Csv
14
13
{
@@ -51,21 +50,6 @@ private bool IsTypeOfIEnumerable(Type type)
51
50
52
51
return false ;
53
52
}
54
-
55
- /// <summary>
56
- /// Returns the JsonProperty data annotation name
57
- /// </summary>
58
- /// <param name="pi">Property Info</param>
59
- /// <returns></returns>
60
- private string GetDisplayNameFromNewtonsoftJsonAnnotations ( PropertyInfo pi )
61
- {
62
- if ( pi . GetCustomAttribute < JsonPropertyAttribute > ( false ) ? . PropertyName is string value )
63
- {
64
- return value ;
65
- }
66
-
67
- return pi . GetCustomAttribute < DisplayAttribute > ( false ) ? . Name ?? pi . Name ;
68
- }
69
53
70
54
public async override Task WriteResponseBodyAsync ( OutputFormatterWriteContext context )
71
55
{
@@ -87,31 +71,22 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
87
71
88
72
if ( _options . UseSingleLineHeaderInCsv )
89
73
{
90
- var values = _options . UseJsonPropertyJsonIgnoreAttributes
91
- ? itemType . GetProperties ( ) . Where ( pi => ! pi . GetCustomAttributes < JsonIgnoreAttribute > ( false ) . Any ( ) ) // Only get the properties that do not define JsonIgnore
92
- . Select ( GetDisplayNameFromNewtonsoftJsonAnnotations )
93
- : itemType . GetProperties ( ) . Select ( pi => pi . GetCustomAttribute < DisplayAttribute > ( false ) ? . Name ?? pi . Name ) ;
94
-
95
- await streamWriter . WriteLineAsync ( string . Join ( _options . CsvDelimiter , values ) ) ;
74
+ await streamWriter . WriteLineAsync (
75
+ string . Join (
76
+ _options . CsvDelimiter , itemType . GetProperties ( ) . Select ( x => x . GetCustomAttribute < DisplayAttribute > ( false ) ? . Name ?? x . Name )
77
+ )
78
+ ) ;
96
79
}
97
80
98
-
99
81
foreach ( var obj in ( IEnumerable < object > ) context . Object )
100
82
{
101
83
102
- //IEnumerable<ObjectValue> vals;
103
- var vals = _options . UseJsonPropertyJsonIgnoreAttributes
104
- ? obj . GetType ( ) . GetProperties ( )
105
- . Where ( pi => ! pi . GetCustomAttributes < JsonIgnoreAttribute > ( ) . Any ( ) )
106
- . Select ( pi => new
107
- {
108
- Value = pi . GetValue ( obj , null )
109
- } )
110
- : obj . GetType ( ) . GetProperties ( ) . Select (
111
- pi => new
112
- {
113
- Value = pi . GetValue ( obj , null )
114
- } ) ;
84
+ var vals = obj . GetType ( ) . GetProperties ( ) . Select (
85
+ pi => new
86
+ {
87
+ Value = pi . GetValue ( obj , null )
88
+ }
89
+ ) ;
115
90
116
91
string valueLine = string . Empty ;
117
92
@@ -122,8 +97,11 @@ public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext co
122
97
123
98
var _val = val . Value . ToString ( ) ;
124
99
125
- //Check if the value contains a comma and place it in quotes if so
126
- if ( _val . Contains ( "," ) )
100
+ //Escape quotas
101
+ _val = _val . Replace ( "\" " , "\" \" " ) ;
102
+
103
+ //Check if the value contans a delimiter and place it in quotes if so
104
+ if ( _val . Contains ( _options . CsvDelimiter ) )
127
105
_val = string . Concat ( "\" " , _val , "\" " ) ;
128
106
129
107
//Replace any \r or \n special characters from a new line with a space
0 commit comments