Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
JsonLdCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
8
10{
15 {
19 public JsonLdCodec()
20 {
21 }
22
26 public string[] ContentTypes => JsonLdContentTypes;
27
28 private static readonly string[] JsonLdContentTypes = new string[]
29 {
31 };
32
36 public string[] FileExtensions => JsonLdFileExtensions;
37
38 private static readonly string[] JsonLdFileExtensions = new string[]
39 {
41 };
42
46 public const string DefaultContentType = "application/ld+json";
47
51 public const string DefaultExtension = "jsonld";
52
59 public bool Decodes(string ContentType, out Grade Grade)
60 {
61 if (Array.IndexOf(JsonLdContentTypes, ContentType) >= 0)
62 {
63 Grade = Grade.Excellent;
64 return true;
65 }
66 else
67 {
68 Grade = Grade.NotAtAll;
69 return false;
70 }
71 }
72
83 public async Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
84 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
85 {
86 string s = Strings.GetString(Data, Encoding ?? Encoding.UTF8);
87 JsonLdDocument Parsed = await JsonLdDocument.CreateAsync(s, BaseUri, "n", BlankNodeIdMode.Guid);
88 return new ContentResponse(ContentType, Parsed, Data);
89 }
90
98 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
99 {
100 if (Object is JsonLdDocument &&
101 InternetContent.IsAccepted(JsonLdContentTypes, AcceptedContentTypes))
102 {
103 Grade = Grade.Excellent;
104 return true;
105 }
106 else if (Object is ISemanticModel)
107 {
108 if (InternetContent.IsAccepted(JsonLdContentTypes, AcceptedContentTypes))
109 {
110 Grade = Grade.Ok;
111 return true;
112 }
113 else if (InternetContent.IsAccepted(JsonCodec.JsonContentTypes, AcceptedContentTypes))
114 {
115 Grade = Grade.Barely;
116 return true;
117 }
118 }
119
120 Grade = Grade.NotAtAll;
121 return false;
122 }
123
132 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
133 {
134 if (Encoding is null)
135 Encoding = Encoding.UTF8;
136
137 string Text;
138
139 if (Object is JsonLdDocument Doc)
140 Text = Doc.Text;
141 else
142 {
143 if (!(Object is ISemanticModel Model))
144 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object.", nameof(Object))));
145
146 StringBuilder sb = new StringBuilder();
147
148 foreach (ISemanticTriple Triple in Model)
149 {
150 sb.Append(Triple.Subject);
151 sb.Append('\t');
152 sb.Append(Triple.Predicate);
153 sb.Append('\t');
154 sb.Append(Triple.Object);
155 sb.Append('\t');
156 sb.AppendLine(".");
157 }
158
159 Text = sb.ToString();
160 }
161
162 byte[] Bin = Encoding.GetBytes(Text);
163 string ContentType = JsonLdContentTypes[0] + "; charset=" + Encoding.WebName;
164
165 return Task.FromResult(new ContentResponse(ContentType, Object, Bin));
166 }
167
174 public bool TryGetContentType(string FileExtension, out string ContentType)
175 {
176 if (string.Compare(FileExtension, JsonLdFileExtensions[0], true) == 0)
177 {
178 ContentType = JsonLdContentTypes[0];
179 return true;
180 }
181 else
182 {
183 ContentType = null;
184 return false;
185 }
186 }
187
194 public bool TryGetFileExtension(string ContentType, out string FileExtension)
195 {
196 if (Array.IndexOf(JsonLdContentTypes, ContentType) >= 0)
197 {
198 FileExtension = JsonLdFileExtensions[0];
199 return true;
200 }
201 else
202 {
203 FileExtension = null;
204 return false;
205 }
206 }
207 }
208}
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.
static readonly string[] JsonContentTypes
JSON content types.
Definition: JsonCodec.cs:42
Encoder and Decoder of semantic information in JSON-LD Documents.
Definition: JsonLdCodec.cs:15
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
Definition: JsonLdCodec.cs:59
const string DefaultExtension
jsonld
Definition: JsonLdCodec.cs:51
string[] FileExtensions
Supported file extensions.
Definition: JsonLdCodec.cs:36
string[] ContentTypes
Supported Internet Content Types.
Definition: JsonLdCodec.cs:26
JsonLdCodec()
Encoder and Decoder of semantic information in JSON-LD Documents.
Definition: JsonLdCodec.cs:19
async Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object
Definition: JsonLdCodec.cs:83
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
Definition: JsonLdCodec.cs:174
const string DefaultContentType
application/ld+json
Definition: JsonLdCodec.cs:46
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object
Definition: JsonLdCodec.cs:132
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Definition: JsonLdCodec.cs:98
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
Definition: JsonLdCodec.cs:194
Contains semantic information stored in a JSON-LD document.
static Task< JsonLdDocument > CreateAsync(string Text)
Contains semantic information stored in a turtle document.
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...
Interface for semantic models.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
BlankNodeIdMode
How blank node IDs are generated
Grade
Grade enumeration
Definition: Grade.cs:7