Giter Site home page Giter Site logo

jsons2xsd's Introduction

Jsons2xsd

Json schema (http://json-schema.org/) to XML schema (XSD) converter.

Features

  • Single purpose library
  • Fast
  • Minimal dependencies

Features supported

TBD

Feedback and pull requests greatly appreciated.

Build status

Build Status

Maven repository

http://ethlo.com/maven

Maven artifact

<dependency>
  <groupId>com.ethlo.schematools</groupId>
  <artifactId>jsons2xsd</artifactId>
  <version>0.2-SNAPSHOT</version>
</dependency>

Usage

final Reader jsonSchema = ...;
final String targetNameSpaceUri = "http://my.example.com/ns";
final OuterWrapping wrapping = OuterWrapping.TYPE;
final String name = "mySpecialType";
final Document xsdDocument = Jsons2Xsd.convert(jsonSchema, targetNameSpaceUri, wrapping, name);

Examples

Input Json schema:

{
    "$schema": "http://json-schema.org/draft-03/schema#",
    "description": "A representation of a person, company, organization, or place",
    "type": "object",
    "properties": {
        "fn": {
            "description": "Formatted Name",
            "type": "string"
        },
        "familyName": { "type": "string", "required": true },
        "givenName": { "type": "string", "required": true },
        "additionalName": { "type": "array", "items": { "type": "string" } },
        "honorificPrefix": { "type": "array", "minItems":1, "maxItems":10, "items": { "type": "string", "maxLength":17} },
        "honorificSuffix": { "type": "array", "items": { "type": "string" } },
        "nickname": { "type": "string" },
        "age": { "type": "integer", "required":true, "minimum":18 },
        "ssn": { "type": "string", "required":false, "pattern":"[0-9]{3}-[0-9]{2}-[0-9]{4}", "minLength":5},
        "url": { "type": "string", "format": "uri" },
        "preferred_format": {"enum":["plaintext", "html", "pdf", 123], "default":"plaintext"},
        "email": {
            "type": "object",
            "properties": {
                "type": { "type": "string" },
                "value": { "type": "string", "format": "email" }
            }
        },
        "tel": {
	    "required": true,
            "type": "object",
            "properties": {
                "type": { "type": "string" },
                "value": { "type": "string", "format": "phone" }
            }
        },
        "tz": { "type": "string" },
        "photo": { "type": "string" },
        "logo": { "type": "string" },
        "sound": { "type": "string" },
        "bday": { "type": "string", "format": "date" },
        "title": { "type": "string" },
        "role": { "type": "string" },
        "org": {
            "type": "object",
            "properties": {
                "organizationName": { "type": "string" },
                "organizationUnit": { "type": "string" }
            }
        }
    }
}

Result of conversion:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ethlo.com/schema/contacts-1.0.xsd" xmlns="http://www.w3.org/2001/XMLSchema">
  <complexType name="mySpecialType">
    <sequence>
      <element minOccurs="0" name="fn" type="string"/>
      <element name="familyName" type="string"/>
      <element name="givenName" type="string"/>
      <element minOccurs="0" name="additionalName">
        <complexType>
          <sequence>
            <element maxOccurs="unbounded" minOccurs="0" name="item" type="string"/>
          </sequence>
        </complexType>
      </element>
      <element minOccurs="0" name="honorificPrefix">
        <complexType>
          <sequence>
            <element maxOccurs="10" minOccurs="1" name="item" type="string"/>
          </sequence>
        </complexType>
      </element>
      <element minOccurs="0" name="honorificSuffix">
        <complexType>
          <sequence>
            <element maxOccurs="unbounded" minOccurs="0" name="item" type="string"/>
          </sequence>
        </complexType>
      </element>
      <element minOccurs="0" name="nickname" type="string"/>
      <element name="age">
        <simpleType>
          <restriction base="int">
            <minInclusive value="18"/>
          </restriction>
        </simpleType>
      </element>
      <element minOccurs="0" name="ssn">
        <simpleType>
          <restriction base="string">
            <minLength value="5"/>
            <pattern value="[0-9]{3}-[0-9]{2}-[0-9]{4}"/>
          </restriction>
        </simpleType>
      </element>
      <element minOccurs="0" name="url" type="anyURI"/>
      <element minOccurs="0" name="preferred_format">
        <simpleType>
          <restriction base="string">
            <enumeration value="plaintext"/>
            <enumeration value="html"/>
            <enumeration value="pdf"/>
            <enumeration value="123"/>
          </restriction>
        </simpleType>
      </element>
      <element minOccurs="0" name="email">
        <complexType>
          <sequence>
            <element minOccurs="0" name="type" type="string"/>
            <element minOccurs="0" name="value" type="string"/>
          </sequence>
        </complexType>
      </element>
      <element name="tel">
        <complexType>
          <sequence>
            <element minOccurs="0" name="type" type="string"/>
            <element minOccurs="0" name="value" type="string"/>
          </sequence>
        </complexType>
      </element>
      <element minOccurs="0" name="tz" type="string"/>
      <element minOccurs="0" name="photo" type="string"/>
      <element minOccurs="0" name="logo" type="string"/>
      <element minOccurs="0" name="sound" type="string"/>
      <element minOccurs="0" name="bday" type="date"/>
      <element minOccurs="0" name="title" type="string"/>
      <element minOccurs="0" name="role" type="string"/>
      <element minOccurs="0" name="org">
        <complexType>
          <sequence>
            <element minOccurs="0" name="organizationName" type="string"/>
            <element minOccurs="0" name="organizationUnit" type="string"/>
          </sequence>
        </complexType>
      </element>
    </sequence>
  </complexType>
</schema>

jsons2xsd's People

Contributors

dschrimpsher avatar ethlo avatar joedeveloper avatar skulasekar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.