0
votes

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"]
}
1
You want to generate a class at runtime, based on a Json?aiqency
Yes as the method I mentioned above accepts class as a parameter.Pranay Kumar

1 Answers

1
votes

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/LIBRARY.md