Convert Json schema (jsons) to XML schema (XSD)
Branch: master
Clone or download
Latest commit 04d429a Aug 18, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Cleanup Aug 14, 2018
.gitignore Initial, major cleanup before 2.0 work Dec 8, 2017
.travis.yml Add code coverage run Dec 9, 2017
LICENSE Update license to reflect correct copyright owner Oct 10, 2015
README.md Update README.md Aug 18, 2018
pom.xml Preparing next dev cycle Aug 18, 2018

README.md

jsons2xsd

Maven Central License: MIT Coverage Status Codacy Badge Build Status

JSON-schema to XML schema converter written in Java.

Dependency

<dependency>
  <groupId>com.ethlo.jsons2xsd</groupId>
  <artifactId>jsons2xsd</artifactId>
  <version>2.0</version>
</dependency>

Snapshots

<repositories>
  <repository>
    <id>sonatype-nexus-snapshots</id>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  </repository>
</repositories>

Usage

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

Example input

{
  "type":"array",
  "items":{
    "type":"object",
    "properties":{
      "price":{
        "type":"number",
        "minimum":0
      },
      "name":{
        "type":"string",
        "minLength":5,
        "maxLength":32
      },
      "isExpired":{
        "default":false,
        "type":"boolean"
      },
      "manufactured":{
        "type":"string",
        "format":"date-time"
      }
    },
    "required":[
      "price",
      "name",
      "manufactured"
    ]
  }
}

Example output

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:x="http://example.com/myschema.xsd" elementFormDefault="qualified" targetNamespace="http://example.com/myschema.xsd">
  <complexType name="array">
    <sequence>
      <element name="price">
        <simpleType>
          <restriction base="decimal">
            <minInclusive value="0" />
          </restriction>
        </simpleType>
      </element>
      <element name="name">
        <simpleType>
          <restriction base="string">
            <minLength value="5" />
            <maxLength value="32" />
          </restriction>
        </simpleType>
      </element>
      <element minOccurs="0" name="isExpired" type="boolean" />
      <element name="manufactured" type="dateTime" />
    </sequence>
  </complexType>
</schema>

Support for non-standard types and formats

Ignore unknown JSON formats

final Config cfg = new Config.Builder()
    .ignoreUnknownFormats(true)
    ...
    .build();

Register custom JSON formats

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

Register non-JSON types

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