0

I have a json schema in draft 04 format (http://json-schema.org/draft-04/schema#) which is used to define a configuration. These schemas are dynamically created based on use input.

I need to pass this json schema to a method which only accepts a kotlin class as parameter. Is there a way to convert this json schema to a kotlin data class?

Basically what I want is something similar to what www.jsonschema2pojo.org this does, but I want to create the class dynamically in the method call.

Sample schema:

{
    "title": "Schema 1",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        }
    },
    "required": ["firstName", "age"]
}
Pranay Kumar
  • 400
  • 1
  • 3
  • 12

1 Answers1

2

You can look at how this IntelliJ Idea plugin does it and use their library in your app: https://github.com/wuseal/JsonToKotlinClass/blob/master/doc/LIBRARY.md

Sebas LG
  • 1,715
  • 1
  • 18
  • 31
  • The blob ref may not work. Here's the shortened link to the repo [JsonToKotlinClass](https://github.com/wuseal/JsonToKotlinClass) – bvj Mar 19 '23 at 20:43