2using System.Collections.Generic;
4using System.Threading.Tasks;
30 private static readonly
string[] SparqlResultSetContentTypes =
new string[]
32 "application/sparql-results+json"
40 private static readonly
string[] SparqlResultSetFileExtensions =
new string[]
53 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
74 public Task<object>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
79 if (!(Obj is Dictionary<string, object> Doc))
80 throw new Exception(
"Unable to decode JSON.");
83 return Task.FromResult<
object>(Parsed);
107 else if (Object is
bool &&
127 public Task<KeyValuePair<byte[], string>>
EncodeAsync(
object Object, Encoding Encoding, params
string[] AcceptedContentTypes)
129 if (Encoding is
null)
130 Encoding = Encoding.UTF8;
138 Pretty = Result.Pretty;
142 else if (Object is
bool b)
145 throw new ArgumentException(
"Unable to encode object.", nameof(Object));
148 byte[] Bin = Encoding.GetBytes(Text);
149 string ContentType = SparqlResultSetContentTypes[0] +
"; charset=" + Encoding.WebName;
151 return Task.FromResult(
new KeyValuePair<
byte[],
string>(Bin, ContentType));
156 Dictionary<string, object> Head =
new Dictionary<string, object>();
157 Dictionary<string, object> ResultObj =
new Dictionary<string, object>()
165 if (!(Result.
Links is
null))
167 List<string> Links =
new List<string>();
169 foreach (Uri Link
in Result.
Links)
170 Links.Add(Link.ToString());
172 Head[
"link"] = Links.ToArray();
179 Dictionary<string, object> Results =
new Dictionary<string, object>();
180 ResultObj[
"results"] = Results;
184 List<Dictionary<string, object>> Bindings =
new List<Dictionary<string, object>>();
188 Dictionary<string, object> Binding =
new Dictionary<string, object>();
191 Binding[Item.
Name] = OutputValue(Item.
Value);
193 Bindings.Add(Binding);
196 Results[
"bindings"] = Bindings.ToArray();
205 Dictionary<string, object> Head =
new Dictionary<string, object>();
206 Dictionary<string, object> ResultObj =
new Dictionary<string, object>()
214 Dictionary<string, object> Results =
new Dictionary<string, object>();
215 ResultObj[
"results"] = Results;
217 List<Dictionary<string, object>> Bindings =
new List<Dictionary<string, object>>();
220 int NrRows = Result.
Rows;
221 int NrColumns = Result.
Columns;
223 for (y = 0; y < NrRows; y++)
225 Dictionary<string, object> Binding =
new Dictionary<string, object>();
227 for (x = 0; x < NrColumns; x++)
230 Bindings.Add(Binding);
233 Results[
"bindings"] = Bindings.ToArray();
238 private Dictionary<string, object>
EncodeAsync(
bool Result)
240 Dictionary<string, object> Head =
new Dictionary<string, object>();
241 Dictionary<string, object> ResultObj =
new Dictionary<string, object>()
244 {
"boolean", Result }
250 private static Dictionary<string, object> OutputValue(
object Value)
252 Dictionary<string, object> Result;
258 Result =
new Dictionary<string, object>()
260 {
"type",
"literal" },
261 {
"value", Literal.Value }
264 if (!
string.IsNullOrEmpty(Literal.StringType))
265 Result[
"datatype"] = Literal.StringType;
280 Result =
new Dictionary<string, object>()
283 {
"value", N.Uri.ToString() }
288 Result =
new Dictionary<string, object>()
291 {
"value", N2.NodeId }
296 Dictionary<string, object> Triple =
new Dictionary<string, object>()
298 {
"subject", OutputValue(T.Subject) },
299 {
"predicate", OutputValue(T.Predicate) },
300 {
"object", OutputValue(T.Object) }
302 Result =
new Dictionary<string, object>()
304 {
"type",
"triple" },
310 Result =
new Dictionary<string, object>()
312 {
"type",
"literal" },
313 {
"value", Value?.ToString() }
319 Result =
new Dictionary<string, object>()
321 {
"type",
"literal" },
322 {
"value", Value?.ToString() }
337 if (
string.Compare(FileExtension, SparqlResultSetFileExtensions[0],
true) == 0)
339 ContentType = SparqlResultSetContentTypes[0];
357 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
359 FileExtension = SparqlResultSetFileExtensions[0];
364 FileExtension =
null;
Helps with parsing of commong data types.
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.
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< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object
string[] ContentTypes
Supported Internet Content Types.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, 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.
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.
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.