Example #1
0
        public static VocabTerm <OpenApiDocument> Create()
        {
            var openApiTerm = new VocabTerm <OpenApiDocument>();

            openApiTerm.MapProperty <string>("swagger", (s, o) => s.Version = o);

            openApiTerm.MapProperty <string>("schemes", (s, o) =>
            {
                if (s.Schemes == null)
                {
                    s.Schemes = new List <String>();
                }
                s.Schemes.Add(o);
            });

            var infoTerm = new VocabTerm <Info>("info");

            infoTerm.MapProperty <string>("description", (s, o) => s.Description       = o);
            infoTerm.MapProperty <string>("termsOfService", (s, o) => s.TermsOfService = o);
            infoTerm.MapProperty <string>("title", (s, o) => s.Title = o);

            openApiTerm.MapObject <Info>(infoTerm, (s) =>
            {
                s.Info = new Info();
                return(s.Info);
            });

            var contactTerm = new VocabTerm <Contact>("contact");

            infoTerm.MapObject <Contact>(contactTerm, (s) =>
            {
                s.Contact = new Contact();
                return(s.Contact);
            });

            var opsTerm = new VocabTerm <Operation>();

            opsTerm.MapProperty <string>("operationId", (s, o) => s.Id = o);
            //opsTerm.MapProperty<string>("x-controller", (s, o) => s. = o);

            var pathTerm = new VocabTerm <Path>();

            pathTerm.MapAnyObject <Operation>(opsTerm, (s, p) => {
                return(s.AddOperation(p, Guid.NewGuid().ToString()));
            });

            var pathsTerm = new VocabTerm <OpenApiDocument>("paths");

            pathsTerm.MapAnyObject <Path>(pathTerm, (s, p) => {
                return(s.AddPath(p));
            });

            openApiTerm.MapObject <OpenApiDocument>(pathsTerm, (s) =>
            {
                return(s);
            });


            return(openApiTerm);
        }
Example #2
0
        public VocabTerm <NS> Clone <NS>(string newterm)
        {
            var term = new VocabTerm <NS>(newterm);

            foreach (var ct in _ChildTerms)
            {
                term.AddChild(ct.Key, ct.Value);
            }
            return(term);
        }
Example #3
0
 public void AddChild(string term, VocabTerm childTerm)
 {
     _ChildTerms.Add(term, childTerm);
 }
Example #4
0
 public Context(object subject, VocabTerm term)
 {
     Subject = subject;
     TermMap = term;
 }
Example #5
0
        public static void ParseStream(Stream stream, object rootSubject, VocabTerm rootTerm)
        {
            using (var reader = new JsonTextReader(new StreamReader(stream)))
            {
                var ostack = new Stack <Context>();

                Context currentContext = null;
                Func <Context, object, Context> currentParser = null;

                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonToken.StartObject:

                        if (currentContext == null)
                        {
                            currentContext = new Context(rootSubject, rootTerm);
                            currentParser  = rootTerm.Parser;
                        }
                        else
                        {
                            // Save current context before creating new context
                            ostack.Push(currentContext);

                            if (currentParser != null)     // If we have a handler to create a new context object
                            {
                                // Update to new Context unless we are walking an Array of objects
                                var newContext = currentParser(currentContext, currentContext.LastProperty);
                                if (newContext != null)
                                {
                                    currentContext = newContext;
                                }
                            }
                            else
                            {
                                currentContext = new Context(null, null);      // Unknown object
                            }
                        }

                        DiagSource?.Write("Hapikit.JsonStreamingParser.NewObject", currentContext);
                        break;

                    case JsonToken.StartArray:
                        DiagSource?.Write("Hapikit.JsonStreamingParser.StartArray", currentContext);
                        break;

                    case JsonToken.EndArray:
                        DiagSource?.Write("Hapikit.JsonStreamingParser.EndArray", currentContext);
                        break;

                    case JsonToken.EndObject:
                        // Update context object
                        DiagSource?.Write("Hapikit.JsonStreamingParser.EndObject", currentContext);
                        if (ostack.Count > 0)
                        {
                            currentParser  = currentContext.TermMap?.Parser;
                            currentContext = ostack.Pop();
                        }
                        break;

                    case JsonToken.PropertyName:
                        // Determine new VocabTerm based on new Property
                        currentContext.LastProperty = reader.Value.ToString();
                        currentParser = currentContext.TermMap?.FindParser(currentContext.LastProperty);
                        DiagSource?.Write("Hapikit.JsonStreamingParser.PropertyName", currentContext.TermMap);
                        break;

                    case JsonToken.Integer:
                    case JsonToken.Boolean:
                    case JsonToken.String:
                        // Use VocabTerm Parser to Update Subject based on the current Term.
                        if (currentParser != null)
                        {
                            DiagSource?.Write("Hapikit.JsonStreamingParser.PropertyValue", reader.Value);
                            currentParser(currentContext, reader.Value);
                        }
                        else
                        {
                            DiagSource?.Write("Hapikit.JsonStreamingParser.MissingTermHandler", new { Context = currentContext, Value = reader.Value });
                        }
                        break;
                    }
                }
            }
        }