Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TsvCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
8
9namespace Waher.Content.Text
10{
15 {
19 public TsvCodec()
20 {
21 }
22
26 public static readonly string[] TsvContentTypes = new string[] { "text/tab-separated-values" };
27
31 public static readonly string[] TsvFileExtensions = new string[] { "tsv" };
32
36 public string[] ContentTypes => TsvContentTypes;
37
42
49 public bool Decodes(string ContentType, out Grade Grade)
50 {
51 if (ContentType == "text/tab-separated-values")
52 {
53 Grade = Grade.Excellent;
54 return true;
55 }
56 else
57 {
58 Grade = Grade.NotAtAll;
59 return false;
60 }
61 }
62
73 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
74 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
75 {
76 string s = Strings.GetString(Data, Encoding);
77 return Task.FromResult(new ContentResponse(ContentType, TSV.Parse(s), Data));
78 }
79
80
87 public bool TryGetContentType(string FileExtension, out string ContentType)
88 {
89 if (string.Compare(FileExtension, "tsv", true) == 0)
90 {
91 ContentType = "text/tab-separated-values";
92 return true;
93 }
94 else
95 {
96 ContentType = null;
97 return false;
98 }
99 }
100
107 public bool TryGetFileExtension(string ContentType, out string FileExtension)
108 {
109 switch (ContentType.ToLower())
110 {
111 case "text/tab-separated-values":
112 FileExtension = "tsv";
113 return true;
114
115 default:
116 FileExtension = string.Empty;
117 return false;
118 }
119 }
120
128 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
129 {
130 if (InternetContent.IsAccepted(TsvContentTypes, AcceptedContentTypes) &&
131 (Object is string[][] || Object is IMatrix))
132 {
133 Grade = Grade.Ok;
134 return true;
135 }
136 else
137 {
138 Grade = Grade.NotAtAll;
139 return false;
140 }
141 }
142
151 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
152 ICodecProgress Progress, params string[] AcceptedContentTypes)
153 {
154 string Tsv;
155
156 if (Object is string[][] Records)
157 Tsv = TSV.Encode(Records);
158 else if (Object is IMatrix M)
159 Tsv = TSV.Encode(M);
160 else
161 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode as TSV.", nameof(Object))));
162
163 if (Encoding is null)
164 Encoding = Encoding.UTF8;
165
166 return Task.FromResult(new ContentResponse("text/tab-separated-values", Object, Encoding.GetBytes(Tsv)));
167 }
168 }
169}
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.
Helps with common TSV-related tasks. (TSV=TAB Separated Values)
Definition: TSV.cs:14
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
Definition: TSV.cs:202
static string[][] Parse(string Tsv)
Parses a TSV string.
Definition: TSV.cs:22
string[] ContentTypes
Supported content types.
Definition: TsvCodec.cs:36
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: TsvCodec.cs:73
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: TsvCodec.cs:107
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: TsvCodec.cs:128
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: TsvCodec.cs:87
static readonly string[] TsvFileExtensions
TSV content types.
Definition: TsvCodec.cs:31
string[] FileExtensions
Supported file extensions.
Definition: TsvCodec.cs:41
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: TsvCodec.cs:49
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: TsvCodec.cs:151
static readonly string[] TsvContentTypes
TSV content types.
Definition: TsvCodec.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