Ejemplo n.º 1
0
        /// <summary>
        ///       Creates a XmlSchema object from a JSON Schema object
        /// </summary>
        /// <param name="jSchema">The Json Schema to convert</param>
        /// <returns>The converted XmlSchema object</returns>
        public XmlSchema CreateXsd(JsonSchema jSchema)
        {
            if (jSchema == null)
            {
                throw new Exception("Cannot create XSD from empty (null) JsonSchema");
            }

            XmlSchema xsdSchema = new XmlSchema
            {
                ElementFormDefault   = XmlSchemaForm.Qualified,
                AttributeFormDefault = XmlSchemaForm.Unqualified,
            };

            xsdSchema.Namespaces.Add("brreg", BRREG_NS);

            AddInfo(xsdSchema, jSchema);

            string title       = GetterExtensions.Title(jSchema);
            string description = GetterExtensions.Description(jSchema);

            if (!string.IsNullOrEmpty(title) || !string.IsNullOrEmpty(description))
            {
                XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();
                AddTitleAndDescriptionAnnotations(jSchema, annotation);

                xsdSchema.Items.Add(annotation);
            }

            // Handle global element declarations
            Dictionary <string, JsonSchema> globalProperties = jSchema.Properties();

            if (globalProperties != null)
            {
                foreach (KeyValuePair <string, JsonSchema> property in globalProperties)
                {
                    XmlSchemaElement rootElement = new XmlSchemaElement
                    {
                        Name           = property.Key,
                        SchemaTypeName = GetTypeName(property.Value),
                    };
                    AddAnnotations(rootElement, property.Value);
                    xsdSchema.Items.Add(rootElement);
                }
            }

            // Handle all definitions
            Dictionary <string, JsonSchema> definitions = GetterExtensions.Definitions(jSchema);

            if (definitions != null)
            {
                foreach (KeyValuePair <string, JsonSchema> def in definitions)
                {
                    ExtractProperties(xsdSchema, def.Key, def.Value);
                }
            }

            return(xsdSchema);
        }
Ejemplo n.º 2
0
        private void AddTitleAndDescriptionAnnotations(JsonSchema jSchema, XmlSchemaAnnotation annotation)
        {
            string description = GetterExtensions.Description(jSchema);
            if (description != null)
            {
                annotation.Items.Add(CreateSimpleDocumentation("description", description));
            }

            string title = GetterExtensions.Title(jSchema);
            if (title != null)
            {
                annotation.Items.Add(CreateSimpleDocumentation("title", title));
            }
        }