json Labs

Convenience module intending to match the standard library json module.

Index

Classes

DeserializationError

Error deserializing document from SysML v2 JSON.

ProjectDeserializationError

Error deserializing project from SysML v2 JSON.

SerdeError

Class for exceptions from serialization and deserialization

SerdeWarning

Class for warnings from serialization and deserialization

SerializationError

Error serializing element to SysML v2 JSON.

Attributes
Functions

dumps

Serialize element to a SysML v2 JSON str.

loads


Attributes

type DeserializationReport = syside.SerdeReport[syside.Element | str | syside.DocumentSegment]
type JsonSourceInto = tuple[syside.Document, str]
type JsonSourceNew = tuple[str | syside.Url, str]
Used in

󰊕 Functions

dumps(element: syside.Element, options: syside.SerializationOptions, indent: int = 2, use_spaces: bool = True, final_new_line: bool = True, include_cross_ref_uris: bool = True) str[source]

Serialize element to a SysML v2 JSON str.

See the documentation of the SerializationOptions class for documentation of the possible options. The options object constructed with SerializationOptions.minimal instructs to produce a minimal JSON without any redundant elements that results in significantly smaller JSONs. Examples of redundant information that is avoided using minimal configuration are:

  • including fields for null values;

  • including fields whose values match the default values;

  • including redefined fields that are duplicates of redefining fields;

  • including derived fields that can be computed from minimal JSON (for example, the result value of evaluating an expression);

  • including implied relationships.

Note

Syside does not construct all derived properties yet. Therefore, setting options.include_derived to True may result in a JSON that does not satisfy the schema.

Parameters:
  • element – The SysML v2 element to be serialized to SysML v2 JSON.

  • options – The serialization options to use when serializing SysML v2 to JSON.

  • indent – How many space or tab characters to use for indenting the JSON.

  • use_spaces – Whether use spaces or tabs for indentation.

  • final_new_line – Whether to add a newline character at the end of the generated string.

  • include_cross_ref_uris – Whether to add potentially relative URIs as @uri property to references of Elements from documents other than the one owning element. Note that while such references are non-standard, they match the behaviour of XMI exports in Pilot implementation which use relative URIs for references instead of plain element IDs.

Returns:

element serialized as JSON.

loads(s: str, document: syside.Document, attributes: syside.AttributeMap | None = None) syside.DeserializedModel[source]

Deserialize a model from s into an already existing document.

Root node will be inferred as:

  1. The first Namespace (not subtype) without an owning relationship.

  2. The first Element that has no serialized owning related element or owning relationship, starting from the first element in the JSON array, and following owning elements up.

  3. The first element in the array otherwise.

Parameters:
  • s – The string contained serialized SysML model in JSON array.

  • document – The document the model will be deserialized into.

  • attributes – Attribute mapping of s. If none provided, this will attempt to infer a corresponding mapping or raise a ValueError.

Returns:

Model deserialized from JSON array. Note that references into other documents will not be resolved, users will need to resolve them by calling link on the returned model. See also IdMap.

loads(s: str, document: syside.Url | str, attributes: syside.AttributeMap | None = None) tuple[syside.DeserializedModel, syside.SharedMutex[syside.Document]][source]

Create a new document and deserialize a model from s into it.

Root node will be inferred as:

  1. The first Namespace (not subtype) without an owning relationship.

  2. The first Element that has no serialized owning related element or owning relationship, starting from the first element in the JSON array, and following owning elements up.

  3. The first element in the array otherwise.

Parameters:
  • s – The string contained serialized SysML model in JSON array.

  • document – A URI in the form of Url or a string, new document will be created with. If URI path has no extension, or the extension does not match sysml or kerml, ValueError is raised.

  • attributes – Attribute mapping of s. If none provided, this will attempt to infer a corresponding mapping or raise a ValueError.

Returns:

Model deserialized from JSON array and the newly created document. Note that references into other documents will not be resolved, users will need to resolve them by calling link on the returned model. See also IdMap.

loads(s: Iterable[syside.json.JsonSourceNew | syside.json.JsonSourceInto], /, environment: syside.Environment | None = None, resolve: Callable[[str, uuid.UUID], syside.Element | None] | None = None, attributes: syside.AttributeMap | None = None) tuple[syside.BaseModel, list[tuple[syside.DeserializedModel, syside.json.DeserializationReport]]][source]

Deserialize a project of multiple documents from s.

This is effectively calling loads(src, document, attributes) for document, src in s and performing the link step afterwards. See also other overloads of loads.

Parameters:
  • s – Projects sources to deserialize from. If providing a URL string or a Url, new documents will be created for corresponding sources, otherwise deserialization will be performed into the provided Documents.

  • environmentEnvironment this project depends on. Defaults to the bundled standard library. The environment will be used to attempt to resolve missing references in the deserialized project.

  • resolve – User-provided reference resolution callback that takes priority over environment. See DeserializedModel.link for more details.

  • attributes – Attribute mapping of s. If none provided, this will attempt to infer a corresponding mapping or raise a ValueError.

Returns:

A tuple of project deserialized from JSON sources, and deserialization results

Raises:

ProjectDeserializationError – If either the deserialization or the reference resolution had errors.