4using System.Threading.Tasks;
32 private static readonly
string[] SparqlResultSetContentTypes =
new string[]
34 "application/sparql-results+json"
42 private static readonly
string[] SparqlResultSetFileExtensions =
new string[]
55 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
77 public Task<ContentResponse>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding,
78 KeyValuePair<string, string>[] Fields, Uri BaseUri,
ICodecProgress Progress)
83 if (!(Obj is Dictionary<string, object> Doc))
84 return Task.FromResult(
new ContentResponse(
new Exception(
"Unable to decode JSON.")));
111 else if (Object is
bool &&
132 public Task<ContentResponse>
EncodeAsync(
object Object, Encoding Encoding,
ICodecProgress Progress, params
string[] AcceptedContentTypes)
134 if (Encoding is
null)
135 Encoding = Encoding.UTF8;
142 ResultObj = Encode(Result);
143 Pretty = Result.Pretty;
146 ResultObj = Encode(M);
147 else if (Object is
bool b)
148 ResultObj = Encode(b);
150 return Task.FromResult(
new ContentResponse(
new ArgumentException(
"Unable to encode object.", nameof(Object))));
153 byte[] Bin = Encoding.GetBytes(Text);
154 string ContentType = SparqlResultSetContentTypes[0] +
"; charset=" + Encoding.WebName;
159 private static Dictionary<string, object> Encode(
SparqlResultSet Result)
161 Dictionary<string, object> Head =
new Dictionary<string, object>();
162 Dictionary<string, object> ResultObj =
new Dictionary<string, object>()
170 if (!(Result.
Links is
null))
174 foreach (Uri Link
in Result.
Links)
175 Links.
Add(Link.ToString());
177 Head[
"link"] = Links.
ToArray();
184 Dictionary<string, object> Results =
new Dictionary<string, object>();
185 ResultObj[
"results"] = Results;
193 Dictionary<string, object> Binding =
new Dictionary<string, object>();
196 Binding[Item.
Name] = OutputValue(Item.
Value);
198 Bindings.
Add(Binding);
201 Results[
"bindings"] = Bindings.
ToArray();
208 private static Dictionary<string, object> Encode(
ObjectMatrix Result)
210 Dictionary<string, object> Head =
new Dictionary<string, object>();
211 Dictionary<string, object> ResultObj =
new Dictionary<string, object>()
219 Dictionary<string, object> Results =
new Dictionary<string, object>();
220 ResultObj[
"results"] = Results;
225 int NrRows = Result.
Rows;
226 int NrColumns = Result.
Columns;
228 for (y = 0; y < NrRows; y++)
230 Dictionary<string, object> Binding =
new Dictionary<string, object>();
232 for (x = 0; x < NrColumns; x++)
235 Bindings.
Add(Binding);
238 Results[
"bindings"] = Bindings.
ToArray();
243 private static Dictionary<string, object> Encode(
bool Result)
245 Dictionary<string, object> Head =
new Dictionary<string, object>();
246 Dictionary<string, object> ResultObj =
new Dictionary<string, object>()
249 {
"boolean", Result }
255 private static Dictionary<string, object> OutputValue(
object Value)
257 Dictionary<string, object> Result;
263 Result =
new Dictionary<string, object>()
265 {
"type",
"literal" },
266 {
"value", Literal.Value }
269 if (!
string.IsNullOrEmpty(Literal.StringType))
270 Result[
"datatype"] = Literal.StringType;
285 Result =
new Dictionary<string, object>()
288 {
"value", N.Uri.ToString() }
293 Result =
new Dictionary<string, object>()
296 {
"value", N2.NodeId }
301 Dictionary<string, object> Triple =
new Dictionary<string, object>()
303 {
"subject", OutputValue(T.Subject) },
304 {
"predicate", OutputValue(T.Predicate) },
305 {
"object", OutputValue(T.Object) }
307 Result =
new Dictionary<string, object>()
309 {
"type",
"triple" },
315 Result =
new Dictionary<string, object>()
317 {
"type",
"literal" },
318 {
"value", Value?.ToString() }
324 Result =
new Dictionary<string, object>()
326 {
"type",
"literal" },
327 {
"value", Value?.ToString() }
342 if (
string.Compare(FileExtension, SparqlResultSetFileExtensions[0],
true) == 0)
344 ContentType = SparqlResultSetContentTypes[0];
362 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
364 FileExtension = SparqlResultSetFileExtensions[0];
369 FileExtension =
null;
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.
static object Parse(string Json)
Parses a JSON string.
static string Encode(string s)
Encodes a string for inclusion in JSON.
Represents a custom literal.
string Language
Language of string.
Represents a string literal.
string Language
Language of string.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
bool? BooleanResult
Any Boolean result returned.
string[] Variables
Names of variables in result set.
ISparqlResultRecord[] Records
Records in result set.
Uri[] Links
Links to additional metadata about result set.
Encoder and Decoder of semantic information from SPARQL queries using JSON. https://www....
string[] FileExtensions
Supported file extensions.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object
string[] ContentTypes
Supported Internet Content Types.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object
SparqlResultSetJsonCodec()
Encoder and Decoder of semantic information from SPARQL queries using JSON.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
A chunked list is a linked list of chunks of objects of type T .
void Add(T Item)
Adds an item to the collection.
T[] ToArray()
Returns an array containing all elements of the collection.
Static class managing binary representations of strings.
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.
IElement GetElement(int Index)
Gets an element of the vector.
bool HasColumnNames
If the matrix has column names defined.
int Columns
Number of columns.
string[] ColumnNames
Contains optional column names.
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 nodes.
Interface for semantic literals.
Interface for semantic triples.
Interface for items in a record from the results of a SPARQL query.
string Name
Name of item in record.
ISemanticElement Value
Value of item in record.
Interface for result records of a SPARQL query.