Class JsonModel
Represents the JSON configuration model for a single MOSAIC pipeline component (block).
Implements
Inherited Members
Namespace: MOSAIC.Components.Basics
Assembly: MOSAIC.dll
Syntax
public sealed record JsonModel : IEquatable<JsonModel>
Remarks
A MOSAIC configuration typically consists of a dictionary where each entry maps a block name (key) to a JsonModel instance (value). The dictionary key is treated as the canonical block identifier in the pipeline graph (e.g., used to connect inputs).
Serialization: Properties are annotated with JsonPropertyNameAttribute to define the JSON contract. Optional properties are omitted from JSON output when they are null.
Validation: This type supports lightweight validation via Validate(). Some fields also include System.ComponentModel.DataAnnotations attributes (e.g., MinLengthAttribute and RangeAttribute) that can be used by external validation frameworks.
Parameters: The Params collection is intentionally untyped (object) to support flexible block-specific arguments. Block factories should interpret and validate parameters based on Type.
Examples
{
"Rectifier": {
"Type": "Blocks.Filters.Rectifier",
"Inputs": [ "RawEmg" ],
"Params": [ 0.02, "double", true ],
"DesiredRate": 200,
"Path": null
}
}
Properties
DesiredRate
Gets the desired update/output rate in Hertz.
Declaration
[JsonPropertyName("DesiredRate")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Range(0, 1.7976931348623157E+308)]
public double? DesiredRate { get; init; }
Property Value
| Type | Description |
|---|---|
| double? |
Remarks
This value is optional. When present, it is typically used as a reference for runtime monitoring and status classification (e.g., idle/lagging detection).
Inputs
Gets the list of input block names/ids, if any.
Declaration
[JsonPropertyName("Inputs")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IReadOnlyList<string>? Inputs { get; init; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<string> |
Remarks
Each entry should correspond to the key/name of another configured block that publishes values consumed by this component.
Name
Gets or sets the human-readable name of the component for logging or UI display.
Declaration
[JsonPropertyName("Name")]
[MinLength(1)]
public string? Name { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
In many configurations, the dictionary key acts as the canonical name. Some loaders may copy that key into this property to keep the model self-contained.
Params
Gets the block-specific parameter list.
Declaration
[JsonPropertyName("Params")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonConverter(typeof(ObjectListConverter))]
public IReadOnlyList<object>? Params { get; init; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<object> |
Remarks
The semantics of this collection depend on Type. Values are stored as object to support heterogeneous parameter sets.
When deserialized, values will be JsonElement instances. Use the static helper methods (GetString(object?, string?), GetInt(object?, int), GetDouble(object?, double), GetBool(object?, bool)) or check ValueKind to extract the actual values.
Path
Gets an optional file path or URI associated with the component.
Declaration
[JsonPropertyName("Path")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Path { get; init; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
Used for blocks that require external resources (e.g., calibration data, models, or media files).
SchemaVersion
Gets the schema version for this JSON contract.
Declaration
[JsonPropertyName("schemaVersion")]
public int SchemaVersion { get; init; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Can be used to support migrations as the configuration format evolves.
Type
Gets the component type identifier used to construct the block (e.g., "Blocks.Timers.ScheduledTimer").
Declaration
[JsonPropertyName("Type")]
[JsonRequired]
[MinLength(1)]
public required string Type { get; init; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
This value is required and is typically resolved to a concrete runtime type by a factory/DI container.
Methods
GetBool(object?, bool)
Extracts a bool from a parameter object, handling JsonElement transparently.
Declaration
public static bool GetBool(object? obj, bool defaultValue = false)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The parameter value, typically a JsonElement from Params. |
| bool | defaultValue | Value returned when |
Returns
| Type | Description |
|---|---|
| bool | The extracted boolean, or |
Remarks
Numeric values are treated as truthy when non-zero. String values are parsed via TryParse(string, out bool).
GetDouble(object?, double)
Extracts a double from a parameter object, handling JsonElement transparently.
Declaration
public static double GetDouble(object? obj, double defaultValue = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The parameter value, typically a JsonElement from Params. |
| double | defaultValue | Value returned when |
Returns
| Type | Description |
|---|---|
| double | The extracted double, or |
Remarks
Integer and float values are widened to double. String values use invariant JSON number formatting first, then the current culture as a fallback for user-entered values.
GetInt(object?, int)
Extracts an int from a parameter object, handling JsonElement transparently.
Declaration
public static int GetInt(object? obj, int defaultValue = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The parameter value, typically a JsonElement from Params. |
| int | defaultValue | Value returned when |
Returns
| Type | Description |
|---|---|
| int | The extracted integer, or |
Remarks
Floating-point values are truncated (cast to int). String values are parsed via TryParse(string, out int).
GetString(object?, string?)
Extracts a string from a parameter object, handling JsonElement transparently.
Declaration
public static string? GetString(object? obj, string? defaultValue = null)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The parameter value, typically a JsonElement from Params. |
| string | defaultValue | Value returned when |
Returns
| Type | Description |
|---|---|
| string | The extracted string, or |
Remarks
Numbers and booleans stored as JsonElement are converted to their string representation.
TryParseDouble(string?, out double)
Declaration
private static bool TryParseDouble(string? value, out double result)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | |
| double | result |
Returns
| Type | Description |
|---|---|
| bool |
Validate()
Performs lightweight runtime validation after deserialization.
Declaration
public void Validate()
Exceptions
| Type | Condition |
|---|---|
| ValidationException | Thrown if Type is null/empty or if DesiredRate is negative. |