Пример #1
0
        private static void Example1()
        {
            var g = new Graph();

            g.Assert("http://orders.com#order123", "http://schema.orders.com#address", "http://orders.com#address123");
            g.Assert("http://orders.com#order123", "http://schema.orders.com#payment", "http://orders.com#payment123");
            g.Assert("http://orders.com#address123", "http://schema.orders.com#street", GraphObject.FromData("101 108th"));
            g.Assert("http://orders.com#payment123", "http://schema.orders.com#name", GraphObject.FromData("Arthur Dexter Bradley"));
            g.Assert("http://orders.com#payment123", "http://schema.orders.com#number", GraphObject.FromData("1234-1234-1234-1234"));
            g.Assert("http://orders.com#address123", "http://schema.orders.com#city", GraphObject.FromData("Bellevue"));
            g.Assert("http://orders.com#address123", "http://schema.orders.com#state", GraphObject.FromData("WA"));

            var g2 = new Graph();

            g2.Assert("http://orders.com#payment123", "http://schema.orders.com#ccv", GraphObject.FromData("123"));
            g2.Assert("http://orders.com#payment123", "http://schema.orders.com#expiry", GraphObject.FromData("20/05"));
            g2.Assert("http://orders.com#address123", "http://schema.orders.com#zip", GraphObject.FromData("98004"));

            g.Merge(g2);

            var context = new Context
            {
                Base  = "http://schema.orders.com#",
                Terms =
                {
                    { "o", new Context.TermDefinition {
                          Id = "http://orders.com#"
                      } }
                }
            };

            var obj = JsonLdProcessor.Frame(context, g, "http://orders.com#order123");

            Console.WriteLine(obj);

            var graph = JsonLdProcessor.CreateGraph(obj);

            foreach (var triple in graph.GetTriples())
            {
                Console.WriteLine($"<{triple.Subject}> <{triple.Predicate}> {triple.Object} .");
            }
        }
Пример #2
0
        private static void ExampleSchemaNet()
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore,
                TypeNameHandling  = TypeNameHandling.Auto
            };

            var context = new Context
            {
                Base  = "http://schema.org",
                Terms = { { "o", new Context.TermDefinition {
                                Id = "http://orders.com"
                            } } }
            };

            var graph = new Graph();
            var joe   = new Person()
            {
                Id         = new Uri("http://schema.org/Person#1234567890"),
                Name       = "Joe",
                FamilyName = "Smith",
                Gender     = GenderType.Female
            };

            // add Joe to graph
            graph.Merge(JsonLdProcessor.CreateGraph(joe));

            var sarah = new Person()
            {
                Id         = new Uri("http://schema.org/Person#0987654321"),
                Name       = "Sarah",
                FamilyName = "Smith",
                Gender     = GenderType.Female
            };

            // add Sarah to graph
            graph.Merge(JsonLdProcessor.CreateGraph(sarah));

            // add spouse relationships between the 2 Person objects
            // NOTE: This uses the JsonLd.Id and context to create Uris and validates that Spouse is actual property on the source object
            graph.Assert(joe, "Spouse", sarah);
            graph.Assert(sarah, "Spouse", joe);

            // dump triples
            Console.WriteLine("---triples---- ");
            foreach (var triple in graph.GetTriples())
            {
                Console.WriteLine($"  {triple}");
            }

            // dump joe nodes as JsObject
            var jsJoe = JsonLdProcessor.Frame(context, graph, "http://schema.org/Person#1234567890");

            Console.WriteLine("---JSObject ---- ");
            Console.WriteLine(jsJoe);

            // get joe as Person object
            var joe2 = JsonLdProcessor.AsObject <Person>(context, graph, "http://schema.org/Person#1234567890");

            Console.WriteLine("---POCO object---- ");
            Console.WriteLine(JsonConvert.SerializeObject(joe2, Formatting.Indented));

            // Get joe as Person object
            var sarah2 = JsonLdProcessor.AsObject <Person>(context, graph, "http://schema.org/Person#0987654321");

            Console.WriteLine("---POCO object---- ");
            Console.WriteLine(JsonConvert.SerializeObject(sarah2, Formatting.Indented));
        }