コード例 #1
0
    public static void Main(string[] args)
    {
        String sparql = File.ReadAllText("../query.sparql");
        //Console.WriteLine(sparql);
        var parameters = new Dictionary <string, string>();

        parameters["query"]  = sparql;
        parameters["format"] = "text/turtle";

        var                 client      = new HttpClient();
        StringContent       queryString = new StringContent(sparql);
        HttpResponseMessage resp        = client.PostAsync("http://virhp07.libris.kb.se/sparql", new FormUrlEncodedContent(parameters)).Result;

        Console.WriteLine("Hello Mono World" + resp.StatusCode);
        var turtle = resp.Content.ReadAsStringAsync().Result;

        Console.WriteLine(turtle);
        var options = new JsonLdOptions()
        {
            format = "text/turtle"
        };

        options.SetUseNativeTypes(true);
        var expanded    = JsonLdProcessor.FromRDF(turtle, options);
        var context     = File.ReadAllText("../context.jsonld");
        var compOptions = new JsonLdOptions();

        compOptions.SetEmbed(true);
        var compacted = JsonLdProcessor.Frame(expanded, context, compOptions);

        Console.WriteLine(turtle);
        Console.WriteLine(expanded);
        Console.WriteLine(compacted);
    }
コード例 #2
0
        /// <summary>
        /// Deserializes the NQuads into a typed model
        /// </summary>
        /// <typeparam name="T">destination entity model type</typeparam>
        /// <param name="nQuads">RDF data in NQuads.</param>
        public T Deserialize <T>(string nQuads)
        {
            var jsonLdObject  = JsonLdProcessor.FromRDF(nQuads);
            var jsonLdContext = _contextProvider.GetContext(typeof(T));

            if (jsonLdContext == null)
            {
                throw new ContextNotFoundException(typeof(T));
            }

            return(JsonLdProcessor.Compact(jsonLdObject, jsonLdContext, new JsonLdOptions()).ToObject <T>(_jsonSerializer));
        }
コード例 #3
0
        /// <summary>
        /// Deserializes the NQuads into a typed model
        /// </summary>
        /// <typeparam name="T">destination entity model type</typeparam>
        /// <param name="nQuads">RDF data in NQuads.</param>
        public T Deserialize <T>(string nQuads)
        {
            var jsonLd  = JsonLdProcessor.FromRDF(nQuads);
            var context = this.contextResolver.GetContext(typeof(T));
            var frame   = this.frameProvider.GetFrame(typeof(T));

            if (context == null)
            {
                throw new ContextNotFoundException(typeof(T));
            }

            return(this.Deserialize <T>(jsonLd, context, frame));
        }
コード例 #4
0
        internal static void Run()
        {
            var serialized = Sample6_ToRDF.Run();
            var opts       = new JsonLdOptions();
            var jsonld     = JsonLdProcessor.FromRDF(serialized, opts);

            Console.WriteLine(jsonld);

            /*
             *
             * Output:
             * [
             *  {
             *      "@id": "_:b0",
             *      "http://schema.org/image": [
             *          {
             *                  "@id": "http://manu.sporny.org/images/manu.png"
             *          }
             *      ],
             *      "http://schema.org/name": [
             *          {
             *              "@value": "Manu Sporny"
             *          }
             *      ],
             *      "http://schema.org/url": [
             *          {
             *              "@id": "http://manu.sporny.org/"
             *          }
             *      ],
             *      "@type": [
             *          "http://schema.org/Person"
             *      ]
             *  },
             *  {
             *      "@id": "http://example.org/ld-experts",
             *      "http://schema.org/member": [
             *          {
             *              "@id": "_:b0"
             *          }
             *      ],
             *      "http://schema.org/name": [
             *          {
             *              "@value": "LD Experts"
             *          }
             *      ]
             *  }
             * ]
             */
        }
コード例 #5
0
        public IEnumerator <object[]> GetEnumerator()
        {
            foreach (string manifest in manifests)
            {
                JToken manifestJson;

                manifestJson = GetJson(manifest);

                foreach (JObject testcase in manifestJson["sequence"])
                {
                    Func <JToken>   run;
                    ConformanceCase newCase = new ConformanceCase();

                    newCase.input   = GetJson(testcase["input"]);
                    newCase.context = GetJson(testcase["context"]);
                    newCase.frame   = GetJson(testcase["frame"]);

                    var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]);

                    var testType = (JArray)testcase["@type"];

                    if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
                    {
                        newCase.error = testcase["expect"];
                    }
                    else if (testType.Any((s) => (string)s == "jld:PositiveEvaluationTest"))
                    {
                        if (testType.Any((s) => new List <string> {
                            "jld:ToRDFTest", "jld:NormalizeTest"
                        }.Contains((string)s)))
                        {
                            newCase.output = File.ReadAllText(Path.Combine("W3C", (string)testcase["expect"]));
                        }
                        else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
                        {
                            newCase.input  = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
                            newCase.output = GetJson(testcase["expect"]);
                        }
                        else
                        {
                            newCase.output = GetJson(testcase["expect"]);
                        }
                    }
                    else
                    {
                        throw new Exception("Expecting either positive or negative evaluation test.");
                    }

                    JToken optionToken;
                    JToken value;

                    if (testcase.TryGetValue("option", out optionToken))
                    {
                        JObject optionDescription = (JObject)optionToken;

                        if (optionDescription.TryGetValue("compactArrays", out value))
                        {
                            options.SetCompactArrays((bool)value);
                        }
                        if (optionDescription.TryGetValue("base", out value))
                        {
                            options.SetBase((string)value);
                        }
                        if (optionDescription.TryGetValue("expandContext", out value))
                        {
                            newCase.context = GetJson(testcase["option"]["expandContext"]);
                            options.SetExpandContext((JObject)newCase.context);
                        }
                        if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
                        {
                            options.SetProduceGeneralizedRdf((bool)value);
                        }
                        if (optionDescription.TryGetValue("useNativeTypes", out value))
                        {
                            options.SetUseNativeTypes((bool)value);
                        }
                        if (optionDescription.TryGetValue("useRdfType", out value))
                        {
                            options.SetUseRdfType((bool)value);
                        }
                    }

                    if (testType.Any((s) => (string)s == "jld:CompactTest"))
                    {
                        run = () => JsonLdProcessor.Compact(newCase.input, newCase.context, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:ExpandTest"))
                    {
                        run = () => JsonLdProcessor.Expand(newCase.input, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:FlattenTest"))
                    {
                        run = () => JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:FrameTest"))
                    {
                        run = () => JsonLdProcessor.Frame(newCase.input, newCase.frame, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:NormalizeTest"))
                    {
                        run = () => new JValue(
                            RDFDatasetUtils.ToNQuads((RDFDataset)JsonLdProcessor.Normalize(newCase.input, options)).Replace("\n", "\r\n")
                            );
                    }
                    else if (testType.Any((s) => (string)s == "jld:ToRDFTest"))
                    {
                        options.format = "application/nquads";
                        run            = () => new JValue(
                            ((string)JsonLdProcessor.ToRDF(newCase.input, options)).Replace("\n", "\r\n")
                            );
                    }
                    else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
                    {
                        options.format = "application/nquads";
                        run            = () => JsonLdProcessor.FromRDF(newCase.input, options);
                    }
                    else
                    {
                        run = () => { throw new Exception("Couldn't find a test type, apparently."); };
                    }

                    if ((string)manifestJson["name"] == "Remote document")
                    {
                        Func <JToken> innerRun = run;
                        run = () =>
                        {
                            var remoteDoc = options.documentLoader.LoadDocument("https://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
                            newCase.input = remoteDoc.Document;
                            options.SetBase(remoteDoc.DocumentUrl);
                            options.SetExpandContext((JObject)remoteDoc.Context);
                            return(innerRun());
                        };
                    }

                    if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
                    {
                        Func <JToken> innerRun = run;
                        run = () =>
                        {
                            try
                            {
                                return(innerRun());
                            }
                            catch (JsonLdError err)
                            {
                                JObject result = new JObject();
                                result["error"] = err.Message;
                                return(result);
                            }
                        };
                    }

                    newCase.run = run;

                    yield return(new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase });
                }
            }
        }