Skip to content

Commit e714922

Browse files
author
Clemens Vasters
committed
Add multiple inheritance example demonstrating \ arrays
1 parent d2c9870 commit e714922

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Multiple Inheritance Example
2+
3+
This example demonstrates multiple inheritance using the `$extends` keyword with an array of JSON Pointers, as specified in [JSON Structure Core PR 20](https://github.com/json-structure/json-structure/pull/20).
4+
5+
## Key Concepts
6+
7+
### Multiple Inheritance with $extends Array
8+
9+
The `$extends` keyword can accept either:
10+
- A single JSON Pointer string (e.g., `"#/definitions/Vehicle"`)
11+
- An array of JSON Pointer strings (e.g., `["#/definitions/Car", "#/definitions/Aircraft"]`)
12+
13+
When an array is provided, the type inherits from all base types. Properties from earlier types in the array take precedence over later ones (first-wins semantics).
14+
15+
### Schema Structure
16+
17+
```
18+
Vehicle (abstract)
19+
├── make, model, year
20+
21+
├── Car ($extends Vehicle)
22+
│ └── numDoors, fuelType
23+
24+
Aircraft
25+
└── wingspan, maxAltitude
26+
27+
FlyingCar ($extends [Car, Aircraft])
28+
└── flightMode, transitionTime
29+
└── Inherits: make, model, year, numDoors, fuelType, wingspan, maxAltitude
30+
```
31+
32+
### Merged Properties
33+
34+
A `FlyingCar` instance must include:
35+
- From `Car` (and transitively from `Vehicle`): `make`, `model`, `year`, `numDoors`, `fuelType`
36+
- From `Aircraft`: `wingspan`, `maxAltitude`
37+
- Its own: `flightMode`, `transitionTime` (optional)
38+
39+
## Files
40+
41+
- `schema.struct.json` - The JSON Structure schema demonstrating multiple inheritance
42+
- `example.json` - A valid instance of a FlyingCar
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"make": "Terrafugia",
3+
"model": "Transition",
4+
"year": 2024,
5+
"numDoors": 2,
6+
"fuelType": "hybrid",
7+
"wingspan": 8.0,
8+
"maxAltitude": 10000,
9+
"flightMode": "ground",
10+
"transitionTime": 60
11+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"$schema": "https://json-structure.org/meta/core/v0/",
3+
"name": "MultipleInheritanceExample",
4+
"version": "1.0",
5+
"description": "Demonstrates multiple inheritance using $extends with an array of JSON Pointers",
6+
"type": "object",
7+
"$ref": "#/definitions/FlyingCar",
8+
"definitions": {
9+
"Vehicle": {
10+
"name": "Vehicle",
11+
"abstract": true,
12+
"type": "object",
13+
"properties": {
14+
"make": { "type": "string" },
15+
"model": { "type": "string" },
16+
"year": { "type": "int16" }
17+
},
18+
"required": ["make", "model", "year"]
19+
},
20+
"Car": {
21+
"name": "Car",
22+
"$extends": "#/definitions/Vehicle",
23+
"type": "object",
24+
"properties": {
25+
"numDoors": { "type": "uint8" },
26+
"fuelType": {
27+
"type": "string",
28+
"enum": ["gasoline", "diesel", "electric", "hybrid"]
29+
}
30+
},
31+
"required": ["numDoors", "fuelType"]
32+
},
33+
"Aircraft": {
34+
"name": "Aircraft",
35+
"type": "object",
36+
"properties": {
37+
"wingspan": { "type": "double", "description": "Wingspan in meters" },
38+
"maxAltitude": { "type": "uint32", "description": "Maximum altitude in feet" }
39+
},
40+
"required": ["wingspan", "maxAltitude"]
41+
},
42+
"FlyingCar": {
43+
"name": "FlyingCar",
44+
"description": "A car that can also fly - demonstrates multiple inheritance",
45+
"$extends": ["#/definitions/Car", "#/definitions/Aircraft"],
46+
"type": "object",
47+
"properties": {
48+
"flightMode": {
49+
"type": "string",
50+
"enum": ["ground", "air", "transition"]
51+
},
52+
"transitionTime": {
53+
"type": "uint16",
54+
"description": "Time in seconds to transition between ground and air mode"
55+
}
56+
},
57+
"required": ["flightMode"]
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)