Java 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.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

/**
 * A simple student struct
 */
public class Student {
    private String firstName;
    private String lastName;
    private Integer age;

    @JsonSetter("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonGetter("firstName")
    public String getFirstName() {
        return this.firstName;
    }

    @JsonSetter("lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonGetter("lastName")
    public String getLastName() {
        return this.lastName;
    }

    @JsonSetter("age")
    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonGetter("age")
    public Integer getAge() {
        return this.age;
    }
}


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.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Human {
    private String firstName;
    private String lastName;
    private Integer age;

    @JsonSetter("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonGetter("firstName")
    public String getFirstName() {
        return this.firstName;
    }

    @JsonSetter("lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonGetter("lastName")
    public String getLastName() {
        return this.lastName;
    }

    @JsonSetter("age")
    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonGetter("age")
    public Integer getAge() {
        return this.age;
    }
}

Student.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Student extends Human {
    private String studentId;

    @JsonSetter("studentId")
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    @JsonGetter("studentId")
    public String getStudentId() {
        return this.studentId;
    }
}


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.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

/**
 * A student struct with an assigned faculty
 */
public class Student {
    private String firstName;
    private String lastName;
    private Integer age;
    private Faculty faculty;

    @JsonSetter("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonGetter("firstName")
    public String getFirstName() {
        return this.firstName;
    }

    @JsonSetter("lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonGetter("lastName")
    public String getLastName() {
        return this.lastName;
    }

    @JsonSetter("age")
    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonGetter("age")
    public Integer getAge() {
        return this.age;
    }

    @JsonSetter("faculty")
    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }

    @JsonGetter("faculty")
    public Faculty getFaculty() {
        return this.faculty;
    }
}

Faculty.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Faculty {
    private String name;

    @JsonSetter("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonGetter("name")
    public String getName() {
        return this.name;
    }
}


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.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Student {
    private String firstName;
    private String lastName;
    private Integer age;
    private java.util.Map<String, String> properties;

    @JsonSetter("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonGetter("firstName")
    public String getFirstName() {
        return this.firstName;
    }

    @JsonSetter("lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonGetter("lastName")
    public String getLastName() {
        return this.lastName;
    }

    @JsonSetter("age")
    public void setAge(Integer age) {
        this.age = age;
    }

    @JsonGetter("age")
    public Integer getAge() {
        return this.age;
    }

    @JsonSetter("properties")
    public void setProperties(java.util.Map<String, String> properties) {
        this.properties = properties;
    }

    @JsonGetter("properties")
    public java.util.Map<String, String> getProperties() {
        return this.properties;
    }
}


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.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Human {
    private String firstName;
    private String lastName;
    private Location location;

    @JsonSetter("firstName")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonGetter("firstName")
    public String getFirstName() {
        return this.firstName;
    }

    @JsonSetter("lastName")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @JsonGetter("lastName")
    public String getLastName() {
        return this.lastName;
    }

    @JsonSetter("location")
    public void setLocation(Location location) {
        this.location = location;
    }

    @JsonGetter("location")
    public Location getLocation() {
        return this.location;
    }
}

Location.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Web.class, name = "web"),
    @JsonSubTypes.Type(value = World.class, name = "world"),
})
public abstract class Location {
    private String type;

    @JsonSetter("type")
    public void setType(String type) {
        this.type = type;
    }

    @JsonGetter("type")
    public String getType() {
        return this.type;
    }
}

Web.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Web extends Location {
    private String url;

    @JsonSetter("url")
    public void setUrl(String url) {
        this.url = url;
    }

    @JsonGetter("url")
    public String getUrl() {
        return this.url;
    }
}

World.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class World extends Location {
    private String lat;
    private String _long;

    @JsonSetter("lat")
    public void setLat(String lat) {
        this.lat = lat;
    }

    @JsonGetter("lat")
    public String getLat() {
        return this.lat;
    }

    @JsonSetter("long")
    public void setLong(String _long) {
        this._long = _long;
    }

    @JsonGetter("long")
    public String getLong() {
        return this._long;
    }
}


Model with generics

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

Student.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Student {
    private Integer matricleNumber;

    @JsonSetter("matricleNumber")
    public void setMatricleNumber(Integer matricleNumber) {
        this.matricleNumber = matricleNumber;
    }

    @JsonGetter("matricleNumber")
    public Integer getMatricleNumber() {
        return this.matricleNumber;
    }
}

Map.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Map<T> {
    private Integer totalResults;
    private java.util.List<T> entries;

    @JsonSetter("totalResults")
    public void setTotalResults(Integer totalResults) {
        this.totalResults = totalResults;
    }

    @JsonGetter("totalResults")
    public Integer getTotalResults() {
        return this.totalResults;
    }

    @JsonSetter("entries")
    public void setEntries(java.util.List<T> entries) {
        this.entries = entries;
    }

    @JsonGetter("entries")
    public java.util.List<T> getEntries() {
        return this.entries;
    }
}

StudentMap.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class StudentMap extends Map<Student> {
}


Import other TypeSchema specification

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

Faculty.java

package org.typeschema.dto;

import com.fasterxml.jackson.annotation.*;

public class Faculty {
    private String description;
    private java.util.List<StudentMap> students;

    @JsonSetter("description")
    public void setDescription(String description) {
        this.description = description;
    }

    @JsonGetter("description")
    public String getDescription() {
        return this.description;
    }

    @JsonSetter("students")
    public void setStudents(java.util.List<StudentMap> students) {
        this.students = students;
    }

    @JsonGetter("students")
    public java.util.List<StudentMap> getStudents() {
        return this.students;
    }
}


Edit this page