예제 #1
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));
        }