Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CsvCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
8
10{
15 {
19 public CsvCodec()
20 {
21 }
22
26 public const string ContentType = "text/csv";
27
31 public static readonly string[] CsvContentTypes = new string[] { ContentType };
32
36 public static readonly string[] CsvFileExtensions = new string[] { "csv" };
37
41 public string[] ContentTypes => CsvContentTypes;
42
47
54 public bool Decodes(string ContentType, out Grade Grade)
55 {
57 {
58 Grade = Grade.Excellent;
59 return true;
60 }
61 else
62 {
63 Grade = Grade.NotAtAll;
64 return false;
65 }
66 }
67
78 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
79 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
80 {
81 string s = Strings.GetString(Data, Encoding);
82 return Task.FromResult(new ContentResponse(ContentType, CSV.Parse(s), Data));
83 }
84
85
92 public bool TryGetContentType(string FileExtension, out string ContentType)
93 {
94 if (string.Compare(FileExtension, "csv", true) == 0)
95 {
97 return true;
98 }
99 else
100 {
101 ContentType = null;
102 return false;
103 }
104 }
105
112 public bool TryGetFileExtension(string ContentType, out string FileExtension)
113 {
114 switch (ContentType.ToLower())
115 {
117 FileExtension = "csv";
118 return true;
119
120 default:
121 FileExtension = string.Empty;
122 return false;
123 }
124 }
125
133 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
134 {
135 if (InternetContent.IsAccepted(CsvContentTypes, AcceptedContentTypes) &&
136 (Object is string[][] || Object is IMatrix))
137 {
138 Grade = Grade.Ok;
139 return true;
140 }
141 else
142 {
143 Grade = Grade.NotAtAll;
144 return false;
145 }
146 }
147
156 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
157 ICodecProgress Progress, params string[] AcceptedContentTypes)
158 {
159 string Csv;
160
161 if (Object is string[][] Records)
162 Csv = CSV.Encode(Records);
163 else if (Object is IMatrix M)
164 Csv = CSV.Encode(M);
165 else
166 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode as CSV.", nameof(Object))));
167
168 if (Encoding is null)
169 Encoding = Encoding.UTF8;
170
171 return Task.FromResult(new ContentResponse(ContentType, Csv, Encoding.GetBytes(Csv)));
172 }
173 }
174}
Helps with common CSV-related tasks. (CSV=Comma Separated Values)
Definition: CSV.cs:21
static string[][] Parse(string Csv)
Parses a CSV string.
Definition: CSV.cs:29
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
Definition: CSV.cs:209
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: CsvCodec.cs:133
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: CsvCodec.cs:92
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: CsvCodec.cs:54
string[] FileExtensions
Supported file extensions.
Definition: CsvCodec.cs:46
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: CsvCodec.cs:156
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: CsvCodec.cs:78
string[] ContentTypes
Supported content types.
Definition: CsvCodec.cs:41
static readonly string[] CsvContentTypes
CSV content types.
Definition: CsvCodec.cs:31
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: CsvCodec.cs:112
static readonly string[] CsvFileExtensions
CSV content types.
Definition: CsvCodec.cs:36
const string ContentType
Content-Type for CSV files.
Definition: CsvCodec.cs:26
Static class managing binary representations of strings.
Definition: Strings.cs:10
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:148
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Basic interface for matrices.
Definition: IMatrix.cs:7
Grade
Grade enumeration
Definition: Grade.cs:7