Kotlin examples

Simple model

A simple model with some scalar properties.
{
  "definitions": {
    "Student": {
      "description": "A simple student struct",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  },
  "root": "Student"
}

Student.kt

/**
 * A simple student struct
 */
open class Student {
    var firstName: String? = null
    var lastName: String? = null
    var age: Int? = null
}

Model with inheritance

A student struct which extends from a different struct.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    },
    "Student": {
      "parent": {
        "type": "reference",
        "target": "Human"
      },
      "type": "struct",
      "properties": {
        "studentId": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

Human.kt

open class Human {
    var firstName: String? = null
    var lastName: String? = null
    var age: Int? = null
}

Student.kt

open class Student : Human {
    var studentId: String? = null
}

Model with reference

A student struct which reference a faculty struct.
{
  "definitions": {
    "Student": {
      "description": "A student struct with an assigned faculty",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "faculty": {
          "type": "reference",
          "target": "Faculty"
        }
      }
    },
    "Faculty": {
      "type": "struct",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

Student.kt

/**
 * A student struct with an assigned faculty
 */
open class Student {
    var firstName: String? = null
    var lastName: String? = null
    var age: Int? = null
    var faculty: Faculty? = null
}

Faculty.kt

open class Faculty {
    var name: String? = null
}

Map with string values

A student struct which contains a map with arbitrary string values.
{
  "definitions": {
    "Student": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "properties": {
          "type": "map",
          "schema": {
            "type": "string"
          }
        }
      }
    }
  },
  "root": "Student"
}

Student.kt

open class Student {
    var firstName: String? = null
    var lastName: String? = null
    var age: Int? = null
    var properties: Map<String, String>? = null
}

Model with discriminator

A struct which uses a discriminator mapping.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "location": {
          "type": "reference",
          "target": "Location"
        }
      }
    },
    "Location": {
      "type": "struct",
      "base": true,
      "properties": {
        "type": {
          "type": "string"
        }
      },
      "discriminator": "type",
      "mapping": {
        "Web": "web",
        "World": "world"
      }
    },
    "Web": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "url": {
          "type": "string"
        }
      }
    },
    "World": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "lat": {
          "type": "string"
        },
        "long": {
          "type": "string"
        }
      }
    }
  },
  "root": "Human"
}

Human.kt

open class Human {
    var firstName: String? = null
    var lastName: String? = null
    var location: Location? = null
}

Location.kt

open abstract class Location {
    var type: String? = null
}

Web.kt

open class Web : Location {
    var url: String? = null
}

World.kt

open class World : Location {
    var lat: String? = null
    var long: String? = null
}

Model with generics

A struct which uses generics.
{
  "definitions": {
    "Student": {
      "type": "object",
      "properties": {
        "matricleNumber": {
          "type": "integer"
        }
      }
    },
    "StudentMap": {
      "type": "struct",
      "parent": {
        "type": "reference",
        "target": "Map",
        "template": {
          "T": "Student"
        }
      }
    },
    "Map": {
      "type": "object",
      "properties": {
        "totalResults": {
          "type": "integer"
        },
        "entries": {
          "type": "array",
          "schema": {
            "type": "generic",
            "name": "T"
          }
        }
      }
    }
  },
  "root": "StudentMap"
}

Student.kt

open class Student {
    var matricleNumber: Int? = null
}

Map.kt

open class Map<T> {
    var totalResults: Int? = null
    var entries: Array<T>? = null
}

StudentMap.kt

open class StudentMap : Map<Student> {
}

Import other TypeSchema specification

A struct which references an external TypeSchema.
{
  "import": {
    "my_ns": "file:///generic.json"
  },
  "definitions": {
    "Faculty": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "students": {
          "type": "array",
          "schema": {
            "type": "reference",
            "target": "my_ns:StudentMap"
          }
        }
      }
    }
  },
  "root": "Faculty"
}

Faculty.kt

open class Faculty {
    var description: String? = null
    var students: Array<StudentMap>? = null
}

Edit this page