Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ToonEncoder.cs
1using System;
4using System.Text;
5using System.Threading.Tasks;
9
10namespace Waher.Content.Toon
11{
16 {
20 public const string DefaultContentType = "text/toon";
21
25 public const string DefaultFileExtension = "toon";
26
30 public ToonEncoder()
31 {
32 }
33
37 public static readonly string[] ToonContentTypes = new string[]
38 {
40 };
41
45 public static readonly string[] ToonFileExtensions = new string[]
46 {
48 };
49
53 public string[] ContentTypes => ToonContentTypes;
54
59
66 public bool TryGetContentType(string FileExtension, out string ContentType)
67 {
68 if (string.Compare(FileExtension, DefaultFileExtension, true) == 0)
69 {
70 ContentType = DefaultContentType;
71 return true;
72 }
73 else
74 {
75 ContentType = null;
76 return false;
77 }
78 }
79
86 public bool TryGetFileExtension(string ContentType, out string FileExtension)
87 {
88 ContentType = ContentType.ToLower();
89
90 if (Array.IndexOf(ToonContentTypes, ContentType) >= 0)
91 {
92 FileExtension = DefaultFileExtension;
93 return true;
94 }
95 else
96 {
97 FileExtension = string.Empty;
98 return false;
99 }
100 }
101
109 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
110 {
111 if (InternetContent.IsAccepted(ToonContentTypes, AcceptedContentTypes))
112 {
113 if (Object is IEnumerable<KeyValuePair<string, object>>)
114 {
115 Grade = Grade.Ok;
116 return true;
117 }
118 else if (Object is IJsonEncodingHint Hint)
119 {
120 Grade = Hint.CanEncodeJson;
121 return Grade != Grade.NotAtAll;
122 }
123 else if (Object is null ||
124 Object is IEnumerable ||
125 Object is IVector ||
126 Object is string ||
127 Object is bool ||
128 Object is decimal ||
129 Object is double ||
130 Object is float ||
131 Object is int ||
132 Object is long ||
133 Object is short ||
134 Object is byte ||
135 Object is uint ||
136 Object is ulong ||
137 Object is ushort ||
138 Object is sbyte ||
139 Object is char)
140 {
141 Grade = Grade.Barely;
142 return true;
143 }
144 }
145
146 Grade = Grade.NotAtAll;
147 return false;
148 }
149
158 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
159 ICodecProgress Progress, params string[] AcceptedContentTypes)
160 {
161 string Toon = TOON.Encode(Object, false);
162
163 if (!InternetContent.IsAccepted(ToonContentTypes, out string ContentType,
164 AcceptedContentTypes))
165 {
166 ContentType = DefaultContentType;
167 }
168
169 if (Encoding is null)
170 {
171 Encoding = Encoding.UTF8;
172 ContentType += "; charset=utf-8";
173 }
174 else
175 ContentType += "; charset=" + Encoding.WebName;
176
177 return Task.FromResult(new ContentResponse(ContentType, Object, Encoding.GetBytes(Toon)));
178 }
179 }
180}
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 TOON-related tasks.
Definition: TOON.cs:12
static string Encode(object Object, bool Indent)
Encodes an object as TOON.
Definition: TOON.cs:37
const string DefaultContentType
text/toon
Definition: ToonEncoder.cs:20
static readonly string[] ToonFileExtensions
TOON file extensions.
Definition: ToonEncoder.cs:45
string[] FileExtensions
Supported file extensions.
Definition: ToonEncoder.cs:58
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: ToonEncoder.cs:66
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: ToonEncoder.cs:86
const string DefaultFileExtension
toon
Definition: ToonEncoder.cs:25
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: ToonEncoder.cs:109
string[] ContentTypes
Supported content types.
Definition: ToonEncoder.cs:53
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: ToonEncoder.cs:158
static readonly string[] ToonContentTypes
TOON content types.
Definition: ToonEncoder.cs:37
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Provides a JSON Encoding hint for an object that implements this interface.
Basic interface for vectors.
Definition: IVector.cs:9
Grade
Grade enumeration
Definition: Grade.cs:7