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 = 5347166 [json_name="firstName"];
    optional string lastName = 115423417 [json_name="lastName"];
    optional int64 age = 162946261 [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 = 5347166 [json_name="firstName"];
    optional string lastName = 115423417 [json_name="lastName"];
    optional int64 age = 162946261 [json_name="age"];
}

Student.proto

message Student {
    optional string firstName = 5347166 [json_name="firstName"];
    optional string lastName = 115423417 [json_name="lastName"];
    optional int64 age = 162946261 [json_name="age"];
    optional string studentId = 48953698 [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 = 5347166 [json_name="firstName"];
    optional string lastName = 115423417 [json_name="lastName"];
    optional int64 age = 162946261 [json_name="age"];
    optional Faculty faculty = 46264005 [json_name="faculty"];
}

Faculty.proto

message Faculty {
    optional string name = 93953702 [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 = 5347166 [json_name="firstName"];
    optional string lastName = 115423417 [json_name="lastName"];
    optional int64 age = 162946261 [json_name="age"];
    optional map<string, string> properties = 149505037 [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 = 5347166 [json_name="firstName"];
    optional string lastName = 115423417 [json_name="lastName"];
    optional Location location = 147494470 [json_name="location"];
}

Location.proto

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

Web.proto

message Web {
    optional string type = 259930637 [json_name="type"];
    optional string url = 87439128 [json_name="url"];
}

World.proto

message World {
    optional string type = 259930637 [json_name="type"];
    optional string lat = 210819871 [json_name="lat"];
    optional string long = 259130331 [json_name="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.proto

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

StudentMap.proto

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

Map.proto

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


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

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


Edit this page