private XmlSchemaElement ExtractElementSequence(string propertyName, JsonSchema propertyType, JsonSchema parentSchema)
        {
            XmlSchemaElement element = new XmlSchemaElement
            {
                Name = propertyName,
            };

            AddAnnotations(element, propertyType);

            List<JsonSchema> items = GetterExtensions.Items(propertyType);
            if (items != null && items.Count > 0)
            {
                double? minItems = GetterExtensions.MinItems(propertyType);
                double? maxItems = GetterExtensions.MaxItems(propertyType);

                if (minItems != null)
                {
                    element.MinOccurs = (decimal)minItems;
                }

                if (maxItems != null)
                {
                    element.MaxOccurs = (decimal)maxItems;
                }
                else
                {
                    element.MaxOccursString = "unbounded";
                }

                foreach (JsonSchema schema in items)
                {
                    string typeRef = GetterExtensions.Ref(schema);
                    element.SchemaTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(typeRef));
                }
            }

            XmlQualifiedName typeName = GetTypeName(propertyType);
            if (typeName != null)
            {
                element.SchemaTypeName = typeName;
            }

            List<string> requiredFields = GetterExtensions.Required(parentSchema);
            if (requiredFields == null || !requiredFields.Contains(propertyName))
            {
                element.MinOccurs = 0;
                element.IsNillable = true;
            }

            string reference = GetterExtensions.Ref(propertyType);
            if (reference != null)
            {
                element.SchemaTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(reference));
            }

            return element;
        }
Example #2
0
        private XmlQualifiedName GetTypeName(JsonSchema jSchema)
        {
            string referencedType = GetterExtensions.Ref(jSchema);

            if (!string.IsNullOrEmpty(referencedType))
            {
                return(new XmlQualifiedName(ExtractTypeFromDefinitionReference(referencedType)));
            }

            TypeKeyword type = jSchema.Get <TypeKeyword>();

            if (type != null)
            {
                switch (type.Value)
                {
                case JsonSchemaType.String:
                {
                    string format = GetterExtensions.Format(jSchema);
                    if (format != null)
                    {
                        return(ExtractBaseTypeNameFromFormat(format));
                    }

                    return(new XmlQualifiedName("string", XML_SCHEMA_NS));
                }

                case JsonSchemaType.Integer:
                    return(new XmlQualifiedName("integer", XML_SCHEMA_NS));

                case JsonSchemaType.Number:
                    return(new XmlQualifiedName("decimal", XML_SCHEMA_NS));

                case JsonSchemaType.Boolean:
                    return(new XmlQualifiedName("boolean", XML_SCHEMA_NS));

                case JsonSchemaType.Array:
                {
                    List <JsonSchema> itemsSchemas = GetterExtensions.Items(jSchema);
                    JsonSchema        itemSchema   = itemsSchemas.ToArray()[0];

                    string itemsReferencedType = GetterExtensions.Ref(itemSchema);
                    if (!string.IsNullOrEmpty(itemsReferencedType))
                    {
                        return(new XmlQualifiedName(ExtractTypeFromDefinitionReference(itemsReferencedType)));
                    }

                    return(null);
                }
                }

                return(null);
            }

            return(null);
        }
        private XmlSchemaSimpleType ExtractSimpleType(string name, JsonSchema jSchema)
        {
            XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType
            {
                Name = name,
            };

            AddAnnotations(simpleType, jSchema);

            string reference = GetterExtensions.Ref(jSchema);
            if (reference != null)
            {
                XmlQualifiedName baseTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(reference));
                XmlSchemaSimpleTypeRestriction simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction
                {
                    BaseTypeName = baseTypeName,
                };

                XmlSchemaSimpleTypeRestriction stringFacets = ExtractStringFacets(jSchema);
                if (stringFacets.Facets.Count > 0)
                {
                    foreach (XmlSchemaObject facet in stringFacets.Facets)
                    {
                        simpleTypeRestriction.Facets.Add(facet);
                    }
                }

                XmlSchemaSimpleTypeRestriction numberFacets = ExtractNumberAndIntegerFacets(jSchema, new TypeKeyword(JsonSchemaType.Number));
                if (numberFacets.Facets.Count > 0)
                {
                    foreach (XmlSchemaObject facet in numberFacets.Facets)
                    {
                        simpleTypeRestriction.Facets.Add(facet);
                    }
                }

                simpleType.Content = simpleTypeRestriction;

                return simpleType;
            }

            TypeKeyword type = jSchema.Get<TypeKeyword>();

            if (type == null)
            {
                // assume string type if type is missing
                XmlSchemaSimpleTypeRestriction noTypeSimpleType = ExtractStringFacets(jSchema);
                noTypeSimpleType.BaseTypeName = null;

                simpleType.Content = noTypeSimpleType;

                return simpleType;
            }

            if (type.Value == JsonSchemaType.String)
            {
                simpleType.Content = ExtractStringFacets(jSchema);
            }
            else if (type.Value == JsonSchemaType.Number || type.Value == JsonSchemaType.Integer)
            {
                simpleType.Content = ExtractNumberAndIntegerFacets(jSchema, type);
            }
            else if (type.Value == JsonSchemaType.Boolean)
            {
                XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction
                {
                    BaseTypeName = new XmlQualifiedName("boolean", XML_SCHEMA_NS),
                };
                simpleType.Content = content;
            }
            else if (type.Value == JsonSchemaType.Array)
            {
                string xlist = jSchema.OtherData.TryGetString("@xsdType");
                if (xlist.Equals("XmlList"))
                {
                    XmlSchemaSimpleTypeList theList = new XmlSchemaSimpleTypeList();
                    List<JsonSchema> items = GetterExtensions.Items(jSchema);
                    string typeRef = GetterExtensions.Ref(items[0]);
                    theList.ItemTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(typeRef));

                    simpleType.Content = theList;
                }
            }

            return simpleType;
        }