Skip to content

Commit e792e97

Browse files
committed
Add section on enums
1 parent 09f1626 commit e792e97

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

docs/BHoM_oM/JSONSchema.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,91 @@ The initial part of requiring `"_t"` to exist and match one of the types guarant
149149
}]
150150
}
151151
```
152+
### Enums
153+
154+
[C# enums](https://learn.microsoft.com/en-us/dotnet/api/system.enum?view=netstandard-2.0) are generally serialised into strings using the BHoM Serialiser_Engine. This means they can be validated using the [JSON schema enum](https://json-schema.org/understanding-json-schema/reference/enum) keyword.
155+
156+
The exception to this rule is when either the enum is serialised as a top level object, or when it is stored in json under a property of a different type, for example [System.Object](https://learn.microsoft.com/en-us/dotnet/api/system.object?view=netstandard-2.0). For this case, the enum is serialised as an object aware of the type, and with the value stored as a property.
157+
158+
To handle both these cases BHoM enums are turned into JSON schemas wrapped in an [anyOf](https://json-schema.org/understanding-json-schema/reference/combining#anyOf) keyword.
159+
160+
!!! example
161+
Example below shows the JSON schema for the [Offset](https://bhom.xyz/api/oM/Physical/Physical/Enums/Offset/) enum. The highlighted portions in the tabs below correspond to the two different options for the handling of the enums. The anyOf pattern on line 3 makes it so that any of the two options will validate as correct.
162+
163+
=== "Simple - as known property"
164+
165+
``` json linenums="1" hl_lines="4-5"
166+
{
167+
"$id" : "https://raw.githubusercontent.com/BHoM/BHoM_JSONSchema/develop/Physical_oM/Elements/Offset.json",
168+
"anyOf" : [{
169+
"type" : ["string", "null"],
170+
"enum" : ["Undefined", "Centre", "InnerEdge", "OuterEdge"]
171+
}, {
172+
"type" : ["object", "null"],
173+
"properties" : {
174+
"_t" : {
175+
"type" : ["string", "null"],
176+
"const" : "System.Enum"
177+
},
178+
"TypeName" : {
179+
"type" : ["object", "null"],
180+
"properties" : {
181+
"_t" : {
182+
"type" : ["string", "null"],
183+
"const" : "System.Type"
184+
},
185+
"Name" : {
186+
"type" : ["string", "null"],
187+
"const" : "BH.oM.Physical.Elements.Offset"
188+
}
189+
}
190+
},
191+
"Value" : {
192+
"type" : ["string", "null"],
193+
"enum" : ["Undefined", "Centre", "InnerEdge", "OuterEdge"]
194+
}
195+
}
196+
}]
197+
}
198+
```
199+
200+
=== "As top level object"
201+
202+
``` Json linenums="1" hl_lines="7-29"
203+
{
204+
"$id" : "https://raw.githubusercontent.com/BHoM/BHoM_JSONSchema/develop/Physical_oM/Elements/Offset.json",
205+
"anyOf" : [{
206+
"type" : ["string", "null"],
207+
"enum" : ["Undefined", "Centre", "InnerEdge", "OuterEdge"]
208+
}, {
209+
"type" : ["object", "null"],
210+
"properties" : {
211+
"_t" : {
212+
"type" : ["string", "null"],
213+
"const" : "System.Enum"
214+
},
215+
"TypeName" : {
216+
"type" : ["object", "null"],
217+
"properties" : {
218+
"_t" : {
219+
"type" : ["string", "null"],
220+
"const" : "System.Type"
221+
},
222+
"Name" : {
223+
"type" : ["string", "null"],
224+
"const" : "BH.oM.Physical.Elements.Offset"
225+
}
226+
}
227+
},
228+
"Value" : {
229+
"type" : ["string", "null"],
230+
"enum" : ["Undefined", "Centre", "InnerEdge", "OuterEdge"]
231+
}
232+
}
233+
}]
234+
}
235+
```
236+
152237

153238
## Releases
154239

0 commit comments

Comments
 (0)