public static void WriteSerializedRepresentations(RDFSharp.Model.RDFGraph graph) { var serializationsByFormat = AvailableSerializationFormats.ToDictionary( f => (object)f, f => SerializeGraph(graph, f) ); WriteLine($"JSON-LD representation of graph '{graph.Context}'"); var contentDocument = JsonLD.Core.JsonLdProcessor.FromRDF( serializationsByFormat[RDFSharp.Model.RDFModelEnums.RDFFormats.NTriples], new JsonLD.Impl.NQuadRDFParser()); var contextDocument = JToken.Parse("{ '@context': { '@vocab': 'http://xmlns.com/foaf/0.1/' } }"); contentDocument = JsonLD.Core.JsonLdProcessor.Compact( contentDocument, contextDocument, new JsonLD.Core.JsonLdOptions()); serializationsByFormat["JSON-LD"] = contentDocument.ToString(); foreach (var kvp in serializationsByFormat) { WriteLine($"{kvp.Key} representation of graph '{graph.Context}'"); WriteLine(); WriteLine(kvp.Value); WriteLine(); } }
public static void WriteSerializedRepresentation(RDFSharp.Model.RDFGraph graph, RDFSharp.Model.RDFModelEnums.RDFFormats format) { var serialization = SerializeGraph(graph, format); WriteLine($"{format} representation of graph '{graph.Context}'"); WriteLine(); WriteLine(serialization); }
public static string SerializeGraph(RDFSharp.Model.RDFGraph graph, RDFSharp.Model.RDFModelEnums.RDFFormats format) { using (var buffer = new MemoryStream()) { graph.ToStream(format, buffer); var result = Encoding.UTF8.GetString(buffer.ToArray()).Trim(); return(result); } }
public static RDFSharp.Model.RDFGraph DeserializeGraphFromJsonLd(string data) { var result = new RDFSharp.Model.RDFGraph(); result.SetContext(new Uri("http://example.com/context/JsonLd")); var token = (JsonLD.Core.RDFDataset)JsonLD.Core.JsonLdProcessor.ToRDF(JsonLD.Util.JSONUtils.FromString(data)); foreach (var quad in token.GetQuads("@default")) { var subject = ToRDFSharpObject(quad["subject"]); var predicate = ToRDFSharpObject(quad["predicate"]); var @object = ToRDFSharpObject(quad["object"]); var triple = CreateTriple(subject, predicate, @object); result.AddTriple(triple); } return(result); }
private static RDFSharp.Model.RDFGraph ConstructGraph() { var result = new RDFSharp.Model.RDFGraph(); result.SetContext(new Uri("http://example.com/context/Local")); var exampleNamespace = new RDFSharp.Model.RDFNamespace("ex", "http://example.com/"); RDFSharp.Model.RDFNamespaceRegister.AddNamespace(exampleNamespace); result.AddTriple(new RDFSharp.Model.RDFTriple( new RDFSharp.Model.RDFResource("ex:subject/001"), RDFSharp.Model.RDFVocabulary.FOAF.FIRSTNAME, // Equivalent to RDFResource("foaf:firstName") which is equivalent to RDFResource("http://xmlns.com/foaf/0.1/firstName") new RDFSharp.Model.RDFPlainLiteral("John"))); result.AddTriple(new RDFSharp.Model.RDFTriple( new RDFSharp.Model.RDFResource("ex:subject/001"), RDFSharp.Model.RDFVocabulary.FOAF.FAMILY_NAME, new RDFSharp.Model.RDFPlainLiteral("Doe"))); return(result); }