JSON Export
This example illustrates exporting the root namespace loaded from a file to the SysML v2
JSON serialization format. The main function of interest is syside.json.dumps
, which
takes an Element
and generates a string representation of a SysML v2 JSON
serialization. The serialization includes the element and (recursively) all its owned
members. Typically, as in this example, you would serialize the root namespace of a
loaded document.
Note
Before running this example, make sure you have activated the SysIDE license by
running syside-license check
according to the instructions in the
License Activation section.
Example Model
package 'JSON Export Example' {
part def Electrical;
part def Mechanical;
part Automobile {
part 'Drive Train' : Electrical;
part Chassis : Mechanical;
}
}
Example Script
import pathlib
import syside
EXAMPLE_DIR = pathlib.Path(__file__).parent
MODEL_FILE_PATH = EXAMPLE_DIR / "example_model.sysml"
def main() -> None:
(model, diagnostics) = syside.try_load_model([MODEL_FILE_PATH])
# Only errors cause an exception. SysIDE may also report warnings and
# informational messages, but not for this example.
assert not diagnostics.contains_errors(warnings_as_errors=True)
# Export the model to JSON
assert len(model.user_docs) == 1
with model.user_docs[0].lock() as locked:
print(
syside.json.dumps(
locked.root_node, syside.SerializationOptions.minimal()
)
)
if __name__ == "__main__":
main()
Output
[
{
"@type": "Namespace",
"@id": "04aa74c8-7a96-45b7-8282-363ddc190395",
"elementId": "04aa74c8-7a96-45b7-8282-363ddc190395",
"ownedRelationship": [
{ "@id": "8ed6b3ea-81cb-4b11-bb95-363ce78c0f3f" }
]
},
{
"@type": "OwningMembership",
"@id": "8ed6b3ea-81cb-4b11-bb95-363ce78c0f3f",
"elementId": "8ed6b3ea-81cb-4b11-bb95-363ce78c0f3f",
"owningRelatedElement": { "@id": "04aa74c8-7a96-45b7-8282-363ddc190395" },
"memberElement": { "@id": "ed343e69-5970-4908-b15e-6c3ed0206cea" },
"memberName": "JSON Export Example",
"ownedRelatedElement": [
{ "@id": "ed343e69-5970-4908-b15e-6c3ed0206cea" }
]
},
{
"@type": "Package",
"@id": "ed343e69-5970-4908-b15e-6c3ed0206cea",
"elementId": "ed343e69-5970-4908-b15e-6c3ed0206cea",
"declaredName": "JSON Export Example",
"owningRelationship": { "@id": "8ed6b3ea-81cb-4b11-bb95-363ce78c0f3f" },
"ownedRelationship": [
{ "@id": "7083e387-8cd2-40c2-bc21-f730ca524bb3" },
{ "@id": "0326379d-2c10-4d54-a984-bd701373dd01" },
{ "@id": "71d7d67a-ccf6-4936-b6a8-f1a553c2cb12" }
]
},
{
"@type": "OwningMembership",
"@id": "7083e387-8cd2-40c2-bc21-f730ca524bb3",
"elementId": "7083e387-8cd2-40c2-bc21-f730ca524bb3",
"owningRelatedElement": { "@id": "ed343e69-5970-4908-b15e-6c3ed0206cea" },
"memberElement": { "@id": "400ad2fc-1388-4e93-9a16-7923048f59f3" },
"memberName": "Electrical",
"ownedRelatedElement": [
{ "@id": "400ad2fc-1388-4e93-9a16-7923048f59f3" }
]
},
{
"@type": "PartDefinition",
"@id": "400ad2fc-1388-4e93-9a16-7923048f59f3",
"elementId": "400ad2fc-1388-4e93-9a16-7923048f59f3",
"declaredName": "Electrical",
"owningRelationship": { "@id": "7083e387-8cd2-40c2-bc21-f730ca524bb3" },
"isVariation": false
},
{
"@type": "OwningMembership",
"@id": "0326379d-2c10-4d54-a984-bd701373dd01",
"elementId": "0326379d-2c10-4d54-a984-bd701373dd01",
"owningRelatedElement": { "@id": "ed343e69-5970-4908-b15e-6c3ed0206cea" },
"memberElement": { "@id": "a20f82bd-bfa7-4583-8273-6f86da886597" },
"memberName": "Mechanical",
"ownedRelatedElement": [
{ "@id": "a20f82bd-bfa7-4583-8273-6f86da886597" }
]
},
{
"@type": "PartDefinition",
"@id": "a20f82bd-bfa7-4583-8273-6f86da886597",
"elementId": "a20f82bd-bfa7-4583-8273-6f86da886597",
"declaredName": "Mechanical",
"owningRelationship": { "@id": "0326379d-2c10-4d54-a984-bd701373dd01" },
"isVariation": false
},
{
"@type": "OwningMembership",
"@id": "71d7d67a-ccf6-4936-b6a8-f1a553c2cb12",
"elementId": "71d7d67a-ccf6-4936-b6a8-f1a553c2cb12",
"owningRelatedElement": { "@id": "ed343e69-5970-4908-b15e-6c3ed0206cea" },
"memberElement": { "@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050" },
"memberName": "Automobile",
"ownedRelatedElement": [
{ "@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050" }
]
},
{
"@type": "PartUsage",
"@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050",
"elementId": "eeb4c4ca-26af-4840-90a3-6dd78dcea050",
"declaredName": "Automobile",
"owningRelationship": { "@id": "71d7d67a-ccf6-4936-b6a8-f1a553c2cb12" },
"isVariation": false,
"ownedRelationship": [
{ "@id": "928924dc-e8d8-41ac-bab0-043ed90c8f7e" },
{ "@id": "1936841f-a35b-4caa-aa3a-071f5d9e7af9" }
]
},
{
"@type": "FeatureMembership",
"@id": "928924dc-e8d8-41ac-bab0-043ed90c8f7e",
"elementId": "928924dc-e8d8-41ac-bab0-043ed90c8f7e",
"owningRelatedElement": { "@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050" },
"memberElement": { "@id": "891d7d40-e36d-46ee-8557-41f86d47240d" },
"memberName": "Drive Train",
"feature": { "@id": "891d7d40-e36d-46ee-8557-41f86d47240d" },
"type": { "@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050" },
"ownedRelatedElement": [
{ "@id": "891d7d40-e36d-46ee-8557-41f86d47240d" }
]
},
{
"@type": "PartUsage",
"@id": "891d7d40-e36d-46ee-8557-41f86d47240d",
"elementId": "891d7d40-e36d-46ee-8557-41f86d47240d",
"declaredName": "Drive Train",
"owningRelationship": { "@id": "928924dc-e8d8-41ac-bab0-043ed90c8f7e" },
"isComposite": true,
"isVariation": false,
"ownedRelationship": [
{ "@id": "608387d2-b884-41f1-b41f-7ccbb9c3c085" }
]
},
{
"@type": "FeatureTyping",
"@id": "608387d2-b884-41f1-b41f-7ccbb9c3c085",
"elementId": "608387d2-b884-41f1-b41f-7ccbb9c3c085",
"owningRelatedElement": { "@id": "891d7d40-e36d-46ee-8557-41f86d47240d" },
"general": { "@id": "400ad2fc-1388-4e93-9a16-7923048f59f3" },
"specific": { "@id": "891d7d40-e36d-46ee-8557-41f86d47240d" },
"type": { "@id": "400ad2fc-1388-4e93-9a16-7923048f59f3" },
"typedFeature": { "@id": "891d7d40-e36d-46ee-8557-41f86d47240d" }
},
{
"@type": "FeatureMembership",
"@id": "1936841f-a35b-4caa-aa3a-071f5d9e7af9",
"elementId": "1936841f-a35b-4caa-aa3a-071f5d9e7af9",
"owningRelatedElement": { "@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050" },
"memberElement": { "@id": "9edaadc0-ff3d-4667-b792-d72ed4826582" },
"memberName": "Chassis",
"feature": { "@id": "9edaadc0-ff3d-4667-b792-d72ed4826582" },
"type": { "@id": "eeb4c4ca-26af-4840-90a3-6dd78dcea050" },
"ownedRelatedElement": [
{ "@id": "9edaadc0-ff3d-4667-b792-d72ed4826582" }
]
},
{
"@type": "PartUsage",
"@id": "9edaadc0-ff3d-4667-b792-d72ed4826582",
"elementId": "9edaadc0-ff3d-4667-b792-d72ed4826582",
"declaredName": "Chassis",
"owningRelationship": { "@id": "1936841f-a35b-4caa-aa3a-071f5d9e7af9" },
"isComposite": true,
"isVariation": false,
"ownedRelationship": [
{ "@id": "514dbe0b-f1bc-4311-a98a-6f96418cd217" }
]
},
{
"@type": "FeatureTyping",
"@id": "514dbe0b-f1bc-4311-a98a-6f96418cd217",
"elementId": "514dbe0b-f1bc-4311-a98a-6f96418cd217",
"owningRelatedElement": { "@id": "9edaadc0-ff3d-4667-b792-d72ed4826582" },
"general": { "@id": "a20f82bd-bfa7-4583-8273-6f86da886597" },
"specific": { "@id": "9edaadc0-ff3d-4667-b792-d72ed4826582" },
"type": { "@id": "a20f82bd-bfa7-4583-8273-6f86da886597" },
"typedFeature": { "@id": "9edaadc0-ff3d-4667-b792-d72ed4826582" }
}
]
Download
Download this example here.