Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
JsonCodec.cs
1using System;
4using System.Text;
5using System.Threading.Tasks;
9
10namespace Waher.Content.Json
11{
16 {
20 public const string DefaultContentType = "application/json";
21
25 public const string JsonRpcContentType = "application/json-rpc";
26
30 public const string DefaultFileExtension = "json";
31
35 public JsonCodec()
36 {
37 }
38
42 public static readonly string[] JsonContentTypes = new string[]
43 {
46 "application/jsonrequest",
47 "text/x-json"
48 };
49
53 public static readonly string[] JsonFileExtensions = new string[]
54 {
56 };
57
61 public string[] ContentTypes => JsonContentTypes;
62
67
74 public bool Decodes(string ContentType, out Grade Grade)
75 {
76 if (Array.IndexOf(JsonContentTypes, ContentType) >= 0)
77 {
78 Grade = Grade.Excellent;
79 return true;
80 }
81 else if (ContentType.StartsWith("application/") && ContentType.EndsWith("+json"))
82 {
83 Grade = Grade.Ok;
84 return true;
85 }
86 else
87 {
88 Grade = Grade.NotAtAll;
89 return false;
90 }
91 }
92
103 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
104 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
105 {
106 string s = Strings.GetString(Data, Encoding ?? Encoding.UTF8);
107 return Task.FromResult(new ContentResponse(ContentType, JSON.Parse(s), Data));
108 }
109
116 public bool TryGetContentType(string FileExtension, out string ContentType)
117 {
118 if (string.Compare(FileExtension, DefaultFileExtension, true) == 0)
119 {
120 ContentType = DefaultContentType;
121 return true;
122 }
123 else
124 {
125 ContentType = null;
126 return false;
127 }
128 }
129
136 public bool TryGetFileExtension(string ContentType, out string FileExtension)
137 {
138 ContentType = ContentType.ToLower();
139
140 if (Array.IndexOf(JsonContentTypes, ContentType) >= 0)
141 {
142 FileExtension = DefaultFileExtension;
143 return true;
144 }
145 else if (ContentType.StartsWith("application/") && ContentType.EndsWith("+json"))
146 {
147 FileExtension = ContentType.Substring(12, ContentType.Length - 5 - 12);
148 return true;
149 }
150 else
151 {
152 FileExtension = string.Empty;
153 return false;
154 }
155 }
156
164 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
165 {
166 if (InternetContent.IsAccepted(JsonContentTypes, AcceptedContentTypes))
167 {
168 if (Object is IEnumerable<KeyValuePair<string, object>>)
169 {
170 Grade = Grade.Ok;
171 return true;
172 }
173 else if (Object is IJsonEncodingHint Hint)
174 {
175 Grade = Hint.CanEncodeJson;
176 return Grade != Grade.NotAtAll;
177 }
178 else if (Object is null ||
179 Object is IEnumerable ||
180 Object is IVector ||
181 Object is string ||
182 Object is bool ||
183 Object is decimal ||
184 Object is double ||
185 Object is float ||
186 Object is int ||
187 Object is long ||
188 Object is short ||
189 Object is byte ||
190 Object is uint ||
191 Object is ulong ||
192 Object is ushort ||
193 Object is sbyte ||
194 Object is char)
195 {
196 Grade = Grade.Barely;
197 return true;
198 }
199 }
200
201 Grade = Grade.NotAtAll;
202 return false;
203 }
204
213 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding,
214 ICodecProgress Progress, params string[] AcceptedContentTypes)
215 {
216 string Json = JSON.Encode(Object, false);
217
218 if (!InternetContent.IsAccepted(JsonContentTypes, out string ContentType,
219 AcceptedContentTypes))
220 {
221 ContentType = DefaultContentType;
222 }
223
224 if (Encoding is null)
225 {
226 Encoding = Encoding.UTF8;
227 ContentType += "; charset=utf-8";
228 }
229 else
230 ContentType += "; charset=" + Encoding.WebName;
231
232 return Task.FromResult(new ContentResponse(ContentType, Object, Encoding.GetBytes(Json)));
233 }
234 }
235}
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 JSON-related tasks.
Definition: JSON.cs:16
static object Parse(string Json)
Parses a JSON string.
Definition: JSON.cs:45
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
const string DefaultContentType
application/json
Definition: JsonCodec.cs:20
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: JsonCodec.cs:136
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: JsonCodec.cs:103
const string DefaultFileExtension
json
Definition: JsonCodec.cs:30
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: JsonCodec.cs:74
static readonly string[] JsonContentTypes
JSON content types.
Definition: JsonCodec.cs:42
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: JsonCodec.cs:164
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: JsonCodec.cs:213
const string JsonRpcContentType
application/json-rpc
Definition: JsonCodec.cs:25
string[] FileExtensions
Supported file extensions.
Definition: JsonCodec.cs:66
static readonly string[] JsonFileExtensions
JSON file extensions.
Definition: JsonCodec.cs:53
string[] ContentTypes
Supported content types.
Definition: JsonCodec.cs:61
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: JsonCodec.cs:116
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...
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