JSON Schema Generator

class besser.generators.json.json_schema_generator.JSONSchemaGenerator(model: DomainModel, output_dir: str = None, mode: str = 'regular')[source]

Bases: GeneratorInterface

JSONSchemaGenerator is a class that implements the GeneratorInterface and is responsible for generating JSONSchema code based on B-UML models.

Parameters:
  • model (DomainModel) – An instance of the DomainModel class representing the B-UML model.

  • output_dir (str, optional) – The output directory where the generated code will be saved. Defaults to None.

  • mode (str, optional) – The generation mode, either ‘regular’ or ‘smart_data’. Defaults to ‘regular’.

_abc_impl = <_abc._abc_data object>
_get_example_value_for_attribute(attr)[source]

Get example value for a specific attribute.

_get_property_description(attr, prop_type)[source]

Generate proper description format for Smart Data Models.

Parameters:
  • attr – The attribute object

  • prop_type – The JSON schema type

Returns:

Formatted description

Return type:

str

_get_property_type(property_type)[source]

Maps B-UML types to JSON schema types and returns format when needed.

Parameters:

property_type – The B-UML type to map.

Returns:

(type, format) where format is None if not applicable

Return type:

tuple

_prepare_smart_data_schema_for_class(class_def)[source]

Prepares schema data for Smart Data format for a specific class.

Parameters:

class_def – The class to prepare the schema for.

Returns:

A dictionary containing the schema data.

Return type:

dict

generate()[source]

Generates JSONSchema code based on the provided B-UML model and saves it to the specified output directory. If the output directory was not specified, the code generated will be stored in the <current directory>/output folder.

Returns:

None, but store the generated code as a file

generate_base_example(class_def)[source]

Generate a base example in key-value format.

generate_example(schema_str)[source]

Generates an example JSON based on the rendered schema string.

generate_jsonld_example(base_example)[source]

Generate JSON-LD example.

generate_normalized_example(base_example)[source]

Generate NGSI v2 normalized example.

generate_normalized_jsonld_example(base_example)[source]

Generate NGSI-LD normalized example.

static tojson_ordered(value, indent=4, base_indent=0)[source]

Serializes ordered JSON with additional indentation only for internal lines, keeping the first line without extra indentation.

Parameters:
  • value (dict or list) – The JSON structure to serialize.

  • indent (int) – Number of spaces to indent per level.

  • base_indent (int) – Extra spaces added to all lines except the first.

Returns:

JSON string with properly formatted indentation.

Return type:

str

besser.generators.json.json_schema_generator.extract_example_and_type(prop, details)[source]

Infer NGSI type and value based on JSON schema, handling anyOf, array, and formats.

besser.generators.json.json_schema_generator.get_example_value(prop, details)[source]

Returns a mock example value based on the schema property definition.

besser.generators.json.json_schema_generator.get_ngsi_type(details)[source]

Determines the NGSI type label based on JSON Schema field definition.