Protobuf 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.proto

message Student {
    optional string firstName = 1 [json_name="firstName"];
    optional string lastName = 2 [json_name="lastName"];
    optional int64 age = 3 [json_name="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.proto

message Human {
    optional string firstName = 1 [json_name="firstName"];
    optional string lastName = 2 [json_name="lastName"];
    optional int64 age = 3 [json_name="age"];
}

Student.proto

message Student {
    optional string firstName = 1 [json_name="firstName"];
    optional string lastName = 2 [json_name="lastName"];
    optional int64 age = 3 [json_name="age"];
    optional string studentId = 4 [json_name="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.proto

message Student {
    optional string firstName = 1 [json_name="firstName"];
    optional string lastName = 2 [json_name="lastName"];
    optional int64 age = 3 [json_name="age"];
    optional Faculty faculty = 4 [json_name="faculty"];
}

Faculty.proto

message Faculty {
    optional string name = 1 [json_name="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.proto

message Student {
    optional string firstName = 1 [json_name="firstName"];
    optional string lastName = 2 [json_name="lastName"];
    optional int64 age = 3 [json_name="age"];
    optional map<string, string> properties = 4 [json_name="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.proto

message Human {
    optional string firstName = 1 [json_name="firstName"];
    optional string lastName = 2 [json_name="lastName"];
    optional Location location = 3 [json_name="location"];
}

Location.proto

message Location {
    optional string type = 1 [json_name="type"];
}

Web.proto

message Web {
    optional string type = 1 [json_name="type"];
    optional string url = 2 [json_name="url"];
}

World.proto

message World {
    optional string type = 1 [json_name="type"];
    optional string lat = 2 [json_name="lat"];
    optional string long = 3 [json_name="long"];
}

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

message Student {
    optional int64 matricleNumber = 1 [json_name="matricleNumber"];
}

StudentMap.proto

message StudentMap {
    optional int64 totalResults = 1 [json_name="totalResults"];
    optional repeated Student entries = 2 [json_name="entries"];
}

Map.proto

message Map {
    optional int64 totalResults = 1 [json_name="totalResults"];
    optional repeated T entries = 2 [json_name="entries"];
}

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

message Faculty {
    optional string description = 1 [json_name="description"];
    optional repeated StudentMap students = 2 [json_name="students"];
}

Edit this page