jsonschema-gen-0.4.1.0: JSON Schema generator from Algebraic data type

Copyright(c) 2015 Shohei Murayama
LicenseBSD3
MaintainerShohei Murayama <shohei.murayama@gmail.com>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Data.JSON.Schema.Generator

Contents

Description

A generator for JSON Schemas from ADT.

Synopsis

How to use this library

Example:

{-# LANGUAGE DeriveGeneric #-}

import qualified Data.ByteString.Lazy.Char8 as BL
import Data.JSON.Schema.Generator
import Data.Proxy
import GHC.Generics

data User = User
    { name :: String
    , age  :: Int
    , email :: Maybe String
    } deriving Generic

instance JSONSchemaGen User

main :: IO ()
main = BL.putStrLn $ generate (Proxy :: Proxy User)

Let's run the above script, we can get on stdout (the following json is formatted with jq):

 {
  "required": [
    "name",
    "age",
    "email"
  ],
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "Main.User",
  "title": "Main.User",
  "type": "object",
  "properties": {
    "email": {
      "type": [
        "string",
        "null"
      ]
    },
    "age": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    }
  }
}

Genenerating JSON Schema

data Options Source #

Options that specify how to generate schema definition automatically from your datatype.

Constructors

Options 

Fields

Instances

data FieldType Source #

Constructors

JSONSchemaPrim a => FieldType (Proxy a) 

defaultOptions :: Options Source #

Default geerating Options:

Options
{ baseUri        = ""
, schemaIdSuffix = ""
, refSchemaMap   = Map.empty
}

generate Source #

Arguments

:: JSONSchemaGen a 
=> Proxy a

A proxy value of the type from which a schema will be generated.

-> ByteString 

Generate a JSON Schema from a proxy value of a type. This uses the default options to generate schema in json format.

generate' Source #

Arguments

:: JSONSchemaGen a 
=> Options

Schema generation Options.

-> Options

Encoding Options of aeson.

-> Proxy a

A proxy value of the type from which a schema will be generated.

-> ByteString 

Generate a JSON Schema from a proxy vaulue of a type. This uses the specified options to generate schema in json format.

Type conversion

class JSONSchemaPrim a where Source #

Minimal complete definition

toSchemaPrim

Generic Schema class

class GJSONSchemaGen f where Source #

Minimal complete definition

gToSchema

Methods

gToSchema :: Options -> Proxy (f a) -> Schema Source #