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;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5using System.Threading.Tasks;
8
9namespace Waher.Content.Json
10{
15 {
19 public const string DefaultContentType = "application/json";
20
24 public const string DefaultFileExtension = "json";
25
29 public JsonCodec()
30 {
31 }
32
36 public static readonly string[] JsonContentTypes = new string[]
37 {
39 "text/x-json"
40 };
41
45 public static readonly string[] JsonFileExtensions = new string[]
46 {
48 };
49
53 public string[] ContentTypes => JsonContentTypes;
54
59
66 public bool Decodes(string ContentType, out Grade Grade)
67 {
68 if (ContentType == DefaultContentType || ContentType == "text/x-json")
69 {
70 Grade = Grade.Excellent;
71 return true;
72 }
73 else if (ContentType.StartsWith("application/") && ContentType.EndsWith("+json"))
74 {
75 Grade = Grade.Ok;
76 return true;
77 }
78 else
79 {
80 Grade = Grade.NotAtAll;
81 return false;
82 }
83 }
84
95 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
96 {
97 string s = CommonTypes.GetString(Data, Encoding ?? Encoding.UTF8);
98 return Task.FromResult(JSON.Parse(s));
99 }
100
107 public bool TryGetContentType(string FileExtension, out string ContentType)
108 {
109 if (string.Compare(FileExtension, "json", true) == 0)
110 {
111 ContentType = DefaultContentType;
112 return true;
113 }
114 else
115 {
116 ContentType = null;
117 return false;
118 }
119 }
120
127 public bool TryGetFileExtension(string ContentType, out string FileExtension)
128 {
129 ContentType = ContentType.ToLower();
130
131 if (Array.IndexOf(JsonContentTypes, ContentType) >= 0)
132 {
133 FileExtension = "json";
134 return true;
135 }
136 else if (ContentType.StartsWith("application/") && ContentType.EndsWith("+json"))
137 {
138 FileExtension = ContentType.Substring(12, ContentType.Length - 5 - 12);
139 return true;
140 }
141 else
142 {
143 FileExtension = string.Empty;
144 return false;
145 }
146 }
147
155 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
156 {
157 if (InternetContent.IsAccepted(JsonContentTypes, AcceptedContentTypes))
158 {
159 if (Object is IEnumerable<KeyValuePair<string, object>>)
160 {
161 Grade = Grade.Ok;
162 return true;
163 }
164 else if (Object is IJsonEncodingHint Hint)
165 {
166 Grade = Hint.CanEncodeJson;
167 return Grade != Grade.NotAtAll;
168 }
169 else if (Object is null ||
170 Object is IEnumerable ||
171 Object is IVector ||
172 Object is string ||
173 Object is bool ||
174 Object is decimal ||
175 Object is double ||
176 Object is float ||
177 Object is int ||
178 Object is long ||
179 Object is short ||
180 Object is byte ||
181 Object is uint ||
182 Object is ulong ||
183 Object is ushort ||
184 Object is sbyte ||
185 Object is char)
186 {
187 Grade = Grade.Barely;
188 return true;
189 }
190 }
191
192 Grade = Grade.NotAtAll;
193 return false;
194 }
195
204 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
205 {
206 string Json = JSON.Encode(Object, false);
207 string ContentType = DefaultContentType;
208
209 if (Encoding is null)
210 {
211 Encoding = Encoding.UTF8;
212 ContentType += "; charset=utf-8";
213 }
214 else
215 ContentType += "; charset=" + Encoding.WebName;
216
217 return Task.FromResult(new KeyValuePair<byte[], string>(Encoding.GetBytes(Json), ContentType));
218 }
219 }
220}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
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:14
static object Parse(string Json)
Parses a JSON string.
Definition: JSON.cs:43
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
const string DefaultContentType
application/json
Definition: JsonCodec.cs:19
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: JsonCodec.cs:127
const string DefaultFileExtension
json
Definition: JsonCodec.cs:24
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: JsonCodec.cs:66
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: JsonCodec.cs:95
static readonly string[] JsonContentTypes
JSON content types.
Definition: JsonCodec.cs:36
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: JsonCodec.cs:155
string[] FileExtensions
Supported file extensions.
Definition: JsonCodec.cs:58
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Definition: JsonCodec.cs:204
static readonly string[] JsonFileExtensions
JSON file extensions.
Definition: JsonCodec.cs:45
string[] ContentTypes
Supported content types.
Definition: JsonCodec.cs:53
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: JsonCodec.cs:107
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