private void AddCustomNameSpaces(XmlSchema xsdSchema, JsonSchema jsonSchema) { Dictionary <string, JsonSchema> definitions = GetterExtensions.Definitions(jsonSchema); if (definitions != null) { foreach (KeyValuePair <string, JsonSchema> def in definitions) { JsonSchema jSchema = def.Value; if (jSchema != JsonSchema.Empty && jSchema.Properties() != null) { Dictionary <string, JsonSchema> props = jSchema.Properties(); foreach (KeyValuePair <string, JsonSchema> property in props) { JsonSchema schemaProp = property.Value; string nameProp = property.Key; if (nameProp.Equals("dataFormatProvider") && schemaProp.Const().String.Equals("SERES")) { xsdSchema.Namespaces.Add("seres", SERES_NS); } } } } } }
/// <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); }
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; }
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 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)); } }
private XmlSchemaSequence ExtractAttributesAndElements(JsonSchema jSchema, XmlSchemaComplexType complexType) { if (jSchema == null || jSchema.Properties() == null) { return(null); } List <string> requiredProperties = GetterExtensions.Required(jSchema); XmlSchemaSequence sequence = new XmlSchemaSequence(); foreach (KeyValuePair <string, JsonSchema> property in jSchema.Properties()) { string propertyName = property.Key; JsonSchema propertyType = property.Value; string xsdType = propertyType.OtherData.TryGetString("@xsdType"); if (xsdType != null && xsdType.Equals("XmlAttribute")) { XmlSchemaAttribute attribute = ExtractAttribute(propertyName, propertyType); if (requiredProperties != null && requiredProperties.Contains(propertyName)) { attribute.Use = XmlSchemaUse.Required; } complexType.Attributes.Add(attribute); } else { sequence.Items.Add(ExtractElementSequence(propertyName, propertyType, jSchema)); } } bool?xsdAnyAttribute = jSchema.OtherData.TryGetBoolean("@xsdAnyAttribute"); if (xsdAnyAttribute == true) { XmlSchemaAnyAttribute anyAttribute = new XmlSchemaAnyAttribute(); anyAttribute.Namespace = "##targetNamespace"; complexType.AnyAttribute = anyAttribute; } return(sequence); }
private XmlSchemaComplexType ExtractComplexTypeSimpleContent(string name, JsonSchema jSchema) { List <string> requiredProperties = GetterExtensions.Required(jSchema); XmlSchemaComplexType complexType = new XmlSchemaComplexType { Name = name, }; AddAnnotations(complexType, jSchema); XmlSchemaSimpleContent simpleContent = new XmlSchemaSimpleContent(); XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension(); simpleContent.Content = extension; foreach (KeyValuePair <string, JsonSchema> item in jSchema.Properties()) { if (item.Key.Equals("value") || HasSimpleContentAnnotation(item.Value)) { extension.BaseTypeName = new XmlQualifiedName( ExtractTypeFromDefinitionReference(GetterExtensions.Ref(item.Value))); } else { XmlSchemaAttribute attributeDefinition = ExtractAttribute(item.Key, item.Value); if (requiredProperties.Contains(item.Key)) { attributeDefinition.Use = XmlSchemaUse.Required; } extension.Attributes.Add(attributeDefinition); } } complexType.ContentModel = simpleContent; AddUnhandledAttributes(jSchema, complexType); return(complexType); }
private static XmlSchemaSimpleTypeRestriction ExtractNumberAndIntegerFacets(JsonSchema jSchema, TypeKeyword type) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction(); double? minValue = GetterExtensions.Minimum(jSchema); double? minExclusiveValue = GetterExtensions.ExclusiveMinimum(jSchema); if (type.Value == JsonSchemaType.Number) { content.BaseTypeName = new XmlQualifiedName("decimal", XML_SCHEMA_NS); } else if (type.Value == JsonSchemaType.Integer) { if (minValue != null && minValue == 0.0) { content.BaseTypeName = new XmlQualifiedName("positiveInteger", XML_SCHEMA_NS); } else { content.BaseTypeName = new XmlQualifiedName("integer", XML_SCHEMA_NS); } } if (minValue != null || minExclusiveValue != null) { if (minValue != null) { XmlSchemaMinInclusiveFacet facet = new XmlSchemaMinInclusiveFacet { Value = FormatDouble((double)minValue), }; content.Facets.Add(facet); } else { XmlSchemaMinExclusiveFacet facet = new XmlSchemaMinExclusiveFacet { Value = FormatDouble((double)minExclusiveValue), }; content.Facets.Add(facet); } } double? maxValue = GetterExtensions.Maximum(jSchema); double? maxExclusiveValue = GetterExtensions.ExclusiveMaximum(jSchema); if (maxValue != null || maxExclusiveValue != null) { if (maxValue != null) { XmlSchemaMaxInclusiveFacet maxInclusiveFacet = new XmlSchemaMaxInclusiveFacet { Value = FormatDouble((double)maxValue), }; content.Facets.Add(maxInclusiveFacet); } else { XmlSchemaMaxExclusiveFacet maxExclusiveFacet = new XmlSchemaMaxExclusiveFacet { Value = FormatDouble((double)maxExclusiveValue), }; content.Facets.Add(maxExclusiveFacet); } } return content; }
private XmlSchemaSimpleTypeRestriction ExtractStringFacets(JsonSchema jSchema) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("string", XML_SCHEMA_NS), }; EnumKeyword enumKeyword = jSchema.Get<EnumKeyword>(); if (enumKeyword != null) { foreach (JsonValue enumValue in GetterExtensions.Enum(jSchema)) { XmlSchemaEnumerationFacet enumFacet = new XmlSchemaEnumerationFacet { Value = enumValue.String, }; content.Facets.Add(enumFacet); } } MinLengthKeyword minLength = jSchema.Get<MinLengthKeyword>(); MaxLengthKeyword maxLength = jSchema.Get<MaxLengthKeyword>(); if (minLength != null && maxLength != null && minLength.Value == maxLength.Value) { // special rule that maps equal min and max lengths to xsd length facet XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet { Value = minLength.Value.ToString(), }; content.Facets.Add(lengthFacet); } else { if (minLength != null) { XmlSchemaMinLengthFacet minLengthFacet = new XmlSchemaMinLengthFacet { Value = minLength.Value.ToString(), }; content.Facets.Add(minLengthFacet); } if (maxLength != null) { XmlSchemaMaxLengthFacet maxLengthFacet = new XmlSchemaMaxLengthFacet { Value = maxLength.Value.ToString(), }; content.Facets.Add(maxLengthFacet); } } PatternKeyword pattern = jSchema.Get<PatternKeyword>(); if (pattern != null) { XmlSchemaPatternFacet patternFacet = new XmlSchemaPatternFacet { Value = pattern.Value.ToString(), }; content.Facets.Add(patternFacet); } FormatKeyword format = jSchema.Get<FormatKeyword>(); if (format != null && format.Value != null && !string.IsNullOrEmpty(format.Value.Key)) { content.BaseTypeName = ExtractBaseTypeNameFromFormat(format.Value.Key); } return content; }
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; }
private XmlSchemaComplexType ExtractComplexType(string name, JsonSchema type) { XmlSchemaComplexType complexType = new XmlSchemaComplexType { Name = name, }; AddAnnotations(complexType, type); string abstractType = type.OtherData.TryGetString("@xsdTag"); if (abstractType != null && abstractType.Equals("abstract")) { complexType.IsAbstract = true; } List<JsonSchema> allOf = GetterExtensions.AllOf(type); if (allOf != null && allOf.Count > 0) { XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension(); foreach (JsonSchema schema in allOf) { string reference = GetterExtensions.Ref(schema); if (reference != null) { extension.BaseTypeName = new XmlQualifiedName(ExtractTypeFromDefinitionReference(reference)); XmlSchemaComplexContent content = new XmlSchemaComplexContent { Content = extension, }; complexType.ContentModel = content; } else if (schema.Properties() != null) { extension.Particle = ExtractAttributesAndElements(schema, complexType); } } } else { XmlSchemaSequence sequence = new XmlSchemaSequence(); List<JsonSchema> oneOf = GetterExtensions.OneOf(type); if (oneOf != null && oneOf.Count > 0) { XmlSchemaChoice choice = new XmlSchemaChoice(); foreach (JsonSchema choiceType in oneOf) { XmlSchemaSequence propSequence = ExtractAttributesAndElements(choiceType, complexType); foreach (XmlSchemaObject item in propSequence.Items) { choice.Items.Add(item); } } sequence.Items.Add(choice); } // handle properties XmlSchemaSequence propertySequence = ExtractAttributesAndElements(type, complexType); if (propertySequence != null && propertySequence.Items.Count > 0) { foreach (XmlSchemaObject item in propertySequence.Items) { sequence.Items.Add(item); } } if (sequence != null && sequence.Items.Count > 0) { complexType.Particle = sequence; } } return complexType; }
private static XmlSchemaSimpleTypeRestriction ExtractNumberAndIntegerFacets(JsonSchema jSchema, TypeKeyword type) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction(); double?minValue = GetterExtensions.Minimum(jSchema); double?minExclusiveValue = GetterExtensions.ExclusiveMinimum(jSchema); if (type.Value == JsonSchemaType.Number) { content.BaseTypeName = new XmlQualifiedName("decimal", XML_SCHEMA_NS); } else if (type.Value == JsonSchemaType.Integer) { if (minValue != null && minValue == 1) { content.BaseTypeName = new XmlQualifiedName("positiveInteger", XML_SCHEMA_NS); } else { content.BaseTypeName = new XmlQualifiedName("integer", XML_SCHEMA_NS); } } double?maxValue = GetterExtensions.Maximum(jSchema); double?maxExclusiveValue = GetterExtensions.ExclusiveMaximum(jSchema); Regex regex = new Regex("^[9]+$"); if ((minValue != null && maxValue != null) && Math.Abs((double)minValue).Equals(maxValue) && regex.IsMatch(maxValue.ToString())) { XmlSchemaTotalDigitsFacet facet = new XmlSchemaTotalDigitsFacet { Value = FormatDouble(maxValue.ToString().Length), }; content.Facets.Add(facet); } else { if (minValue != null || minExclusiveValue != null) { if (minValue != null) { XmlSchemaMinInclusiveFacet facet = new XmlSchemaMinInclusiveFacet { Value = FormatDouble((double)minValue), }; content.Facets.Add(facet); } else { XmlSchemaMinExclusiveFacet facet = new XmlSchemaMinExclusiveFacet { Value = FormatDouble((double)minExclusiveValue), }; content.Facets.Add(facet); } } if (maxValue != null || maxExclusiveValue != null) { if (maxValue != null) { XmlSchemaMaxInclusiveFacet maxInclusiveFacet = new XmlSchemaMaxInclusiveFacet { Value = FormatDouble((double)maxValue), }; content.Facets.Add(maxInclusiveFacet); } else { XmlSchemaMaxExclusiveFacet maxExclusiveFacet = new XmlSchemaMaxExclusiveFacet { Value = FormatDouble((double)maxExclusiveValue), }; content.Facets.Add(maxExclusiveFacet); } } } return(content); }