项目作者: ethlo

项目描述 :
Convert Json schema (jsons) to XML schema (XSD)
高级语言: Java
项目地址: git://github.com/ethlo/jsons2xsd.git
创建时间: 2013-06-25T04:23:03Z
项目社区:https://github.com/ethlo/jsons2xsd

开源协议:MIT License

下载


jsons2xsd

Maven Central
License: MIT
Coverage Status
Codacy Badge
Build Status
GitHub issues open

JSON-schema to XML schema converter written in Java.

Dependency

  1. <dependency>
  2. <groupId>com.ethlo.jsons2xsd</groupId>
  3. <artifactId>jsons2xsd</artifactId>
  4. <version>2.3.0</version>
  5. </dependency>

Snapshots

  1. <repositories>
  2. <repository>
  3. <id>sonatype-nexus-snapshots</id>
  4. <snapshots>
  5. <enabled>true</enabled>
  6. </snapshots>
  7. <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  8. </repository>
  9. </repositories>

Usage

  1. try (final Reader r = ...)
  2. {
  3. final Config cfg = new Config.Builder()
  4. .targetNamespace("http://example.com/myschema.xsd")
  5. .name("array")
  6. .build();
  7. final Document doc = Jsons2Xsd.convert(r, cfg);
  8. }

Example input

  1. {
  2. "type":"array",
  3. "items":{
  4. "type":"object",
  5. "properties":{
  6. "price":{
  7. "type":"number",
  8. "minimum":0
  9. },
  10. "name":{
  11. "type":"string",
  12. "minLength":5,
  13. "maxLength":32
  14. },
  15. "isExpired":{
  16. "default":false,
  17. "type":"boolean"
  18. },
  19. "manufactured":{
  20. "type":"string",
  21. "format":"date-time"
  22. }
  23. },
  24. "required":[
  25. "price",
  26. "name",
  27. "manufactured"
  28. ]
  29. }
  30. }

Example output

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:x="http://example.com/myschema.xsd" elementFormDefault="qualified" targetNamespace="http://example.com/myschema.xsd">
  3. <complexType name="array">
  4. <sequence>
  5. <element name="price">
  6. <simpleType>
  7. <restriction base="decimal">
  8. <minInclusive value="0" ></minInclusive>
  9. </restriction>
  10. </simpleType>
  11. </element>
  12. <element name="name">
  13. <simpleType>
  14. <restriction base="string">
  15. <minLength value="5" ></minLength>
  16. <maxLength value="32" ></maxLength>
  17. </restriction>
  18. </simpleType>
  19. </element>
  20. <element minOccurs="0" name="isExpired" type="boolean" ></element>
  21. <element name="manufactured" type="dateTime" ></element>
  22. </sequence>
  23. </complexType>
  24. </schema>

Support for non-standard types and formats

Ignore unknown JSON formats

  1. final Config cfg = new Config.Builder()
  2. .ignoreUnknownFormats(true)
  3. ...
  4. .build();

Register custom JSON formats

  1. final Config cfg = new Config.Builder()
  2. .customTypeMapping(JsonSimpleType.INTEGER, "int64", XsdSimpleType.LONG)
  3. .customTypeMapping(JsonSimpleType.INTEGER, "int32", XsdSimpleType.INT)
  4. .customTypeMapping(JsonSimpleType.STRING, "ext-ref", XsdSimpleType.STRING)
  5. ...
  6. .build();

Register non-JSON types

  1. final Config cfg = new Config.Builder()
  2. .nonJsonTypeMapping("date-time", XsdSimpleType.DATE_TIME)
  3. .nonJsonTypeMapping("int", XsdSimpleType.INT)
  4. ...
  5. .build();