GithubHelp home page GithubHelp logo

commandertvis / json-schema-serialization Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ricky12awesome/json-schema-serialization

0.0 0.0 0.0 105 KB

Adds support for Json Schema using Kotlin.serialization

License: MIT License

Kotlin 100.00%

json-schema-serialization's Introduction

Download Jitpack Build Status

json-schema-serialization (jss)

Adds support for Json Schema using kotlinx.serialization

Currently, Json Schema DSL is very experimental, expect a lot of changes.

Dependency

You would need kotlinx.serialization setup to use this dependency

Gradle / Gradle Kotlin DSL
repositories {
  jcenter()  
  // or maven("https://dl.bintray.com/ricky12awesome/github")
}

dependencies {
  implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6")
}

Usage

Code

@Serializable
data class Config(
  @Description(["Name for this project."])
  val name: String = "",
  @Description(["Theme for this project."])
  val theme: Theme = Theme()
)

@Serializable
sealed class ThemeColor {
  @Serializable @SerialName("HEX")
  data class HEX(
    @Pattern("#[0-9a-fA-F]{2,6}") val hex: String
  ) : ThemeColor()

  @Serializable @SerialName("RGB")
  data class RGB(
    @IntRange(0, 255) val r: Int,
    @IntRange(0, 255) val g: Int,
    @IntRange(0, 255) val b: Int
  ) : ThemeColor()

  @Serializable @SerialName("HSV")
  data class HSV(
    @IntRange(1, 360) val h: Int,
    @FloatRange(0.0, 1.0) val s: Double,
    @FloatRange(0.0, 1.0) val v: Double
  ) : ThemeColor()

  @Serializable @SerialName("HSL")
  data class HSL(
    @IntRange(1, 360) val h: Int,
    @FloatRange(0.0, 1.0) val s: Double,
    @FloatRange(0.0, 1.0) val l: Double
  ) : ThemeColor()
}

@Serializable
data class Theme(
  @Description(["Primary color for this theme."]) @Definition("ThemeColor")
  val primary: ThemeColor = ThemeColor.RGB(128, 128, 128),
  @Description(["Secondary color for this theme."]) @Definition("ThemeColor")
  val secondary: ThemeColor = ThemeColor.HSV(0, 0.0, 0.3),
  @Description(["Accent color for this theme."]) @Definition("ThemeColor")
  val accent: ThemeColor = ThemeColor.HSL(0, 0.0, 0.8),
  @Description(["Background color for this theme."]) @Definition("ThemeColor")
  val background: ThemeColor = ThemeColor.HEX("#242424")
)

Schema

globalJson.encodeToSchema(Config.serializer(), generateDefinitions = false)
{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "Name for this project."
    },
    "theme": {
      "type": "object",
      "properties": {
        "primary": {
          "description": "Primary color for this theme.",
          "$ref": "#/definitions/ThemeColor"
        },
        "secondary": {
          "description": "Secondary color for this theme.",
          "$ref": "#/definitions/ThemeColor"
        },
        "accent": {
          "description": "Accent color for this theme.",
          "$ref": "#/definitions/ThemeColor"
        },
        "background": {
          "description": "Background color for this theme.",
          "$ref": "#/definitions/ThemeColor"
        }
      }
    }
  },
  "definitions": {
    "ThemeColor": {
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "HEX",
            "RGB",
            "HSV",
            "HSL"
          ]
        }
      },
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "HEX"
            },
            "hex": {
              "type": "string",
              "pattern": "#[0-9a-fA-F]{2,6}"
            }
          },
          "required": [
            "hex"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "RGB"
            },
            "r": {
              "type": "number",
              "minimum": 0,
              "maximum": 255
            },
            "g": {
              "type": "number",
              "minimum": 0,
              "maximum": 255
            },
            "b": {
              "type": "number",
              "minimum": 0,
              "maximum": 255
            }
          },
          "required": [
            "r",
            "g",
            "b"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "HSV"
            },
            "h": {
              "type": "number",
              "minimum": 1,
              "maximum": 360
            },
            "s": {
              "type": "number",
              "minimum": 0.0,
              "maximum": 1.0
            },
            "v": {
              "type": "number",
              "minimum": 0.0,
              "maximum": 1.0
            }
          },
          "required": [
            "h",
            "s",
            "v"
          ]
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "HSL"
            },
            "h": {
              "type": "number",
              "minimum": 1,
              "maximum": 360
            },
            "s": {
              "type": "number",
              "minimum": 0.0,
              "maximum": 1.0
            },
            "l": {
              "type": "number",
              "minimum": 0.0,
              "maximum": 1.0
            }
          },
          "required": [
            "h",
            "s",
            "l"
          ]
        }
      ],
      "required": [
        "type"
      ]
    }
  }
}

Some Features I'm thinking about adding

  • Json Schema DSL
  • Json Schema Data Object instead of using JsonObject

json-schema-serialization's People

Contributors

commandertvis avatar nikkyai avatar ricky12awesome 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.