Skip to main content
Version: 1.0.0

Serialization & validation

Everything you build in code round-trips to and from OntoUML JSON — the exact format the visual editor reads and writes. That is what makes the library interoperable: a model authored in a script can be opened on a canvas, and a diagram drawn by hand can be loaded, queried, and transformed in code.

Theory: one format, two tools

OntoUML JSON is specified by a public JSON Schema published at https://w3id.org/ontouml/schema. A project serializes to a single object with a type: "Project" discriminator, its metadata, and a flat elements array — every class, relation, property, generalization, package, diagram, and view, each tagged with its own type. References between elements (a relation's ends, a generalization's general and specific, a package's contents) are stored by id, and the parser resolves them back into live object references when it reads the file.

Serializing​

Every element implements toJSON(), so plain JSON.stringify already produces schema-conformant output. Prefer serializationUtils.serialize, which does the same but validates the result against the schema before returning it — failing fast if the model ever produces a non-conformant serialization. Pass a spacing argument for a human-readable file.

import { class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
,
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const project: Projectproject.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.add(value: string, language?: string): void
Adds a text value in the given language, replacing any existing value for that language.
@paramvalue - the text to add.@paramlanguage - language tag of `value`; defaults to defaultLanguage.@throwsan error if `language` is not a valid language tag.
add
('School enrollment');
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Model').PackageBuilder.build(): Package
Builds an instance of Package with the parameters passed to the builder. When no methods are evoked, the created package has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `contents: []`
@throwsan error if the builder has already been used.
build
();
const const person: Classperson = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const school: Classschool = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('School').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const model: Packagemodel .Package.binaryRelationBuilder(): BinaryRelationBuilder
Creates a builder for a BinaryRelation contained in this package.
binaryRelationBuilder
()
.BinaryRelationBuilder.material(): BinaryRelationBuilder
Sets the stereotype of the relation to «material», which decorates relations between endurants that are founded on relators, such as being married to or being enrolled at. Also applies the following defaults: - `isDerived = true`
@returnsthis builder, for method chaining.
material
()
.NamedElementBuilder<BinaryRelationBuilder>.name(value: string, language?: string): BinaryRelationBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('studies at')
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the source end of the relation.
@returnsthis builder, for method chaining.
source
(const person: Classperson)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the target end of the relation.
@returnsthis builder, for method chaining.
target
(const school: Classschool)
.BinaryRelationBuilder.build(): BinaryRelation
Builds an instance of BinaryRelation with the parameters passed to the builder. Both the source and the target classifiers are required before `build()` is evoked. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created relation has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,`
@throwsan error if the builder has already been used.
build
();
// Compact — for storage or transport. Validated against the schema. const const json: stringjson =
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.serialize: (project: Project, space?: string | number) => string
Serializes a Project into a JSON string conformant to the OntoUML JSON Schema (https://w3id.org/ontouml/schema), validating the result against the schema before returning it.
@paramproject - the project to serialize.@paramspace - optional indentation passed to `JSON.stringify` for a human-readable serialization.@throwsan error listing the schema violations if the serialization is invalid.
serialize
(const project: Projectproject);
// Pretty-printed — for a file a human will read. const const pretty: stringpretty =
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.serialize: (project: Project, space?: string | number) => string
Serializes a Project into a JSON string conformant to the OntoUML JSON Schema (https://w3id.org/ontouml/schema), validating the result against the schema before returning it.
@paramproject - the project to serialize.@paramspace - optional indentation passed to `JSON.stringify` for a human-readable serialization.@throwsan error listing the schema violations if the serialization is invalid.
serialize
(const project: Projectproject, 2);
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const json: stringjson.String.slice(start?: number, end?: number): string
Returns a section of a string.
@paramstart The index to the beginning of the specified portion of stringObj.@paramend The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end. If this value is not specified, the substring continues to the end of stringObj.
slice
(0, 24)); // {"type":"Project","id":…

Write pretty to a .ontouml file (or any .json) and it will open in the visual tools; the top-level type: "Project" is what identifies it.

Deserializing​

To go the other way, hand the JSON string to serializationUtils.parse. It rebuilds a live Project — with real object references, not ids — so the query and navigation methods work immediately on the result.

import { class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
,
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Model').PackageBuilder.build(): Package
Builds an instance of Package with the parameters passed to the builder. When no methods are evoked, the created package has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `contents: []`
@throwsan error if the builder has already been used.
build
();
const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const json: stringjson = var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON
.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
stringify
(const project: Projectproject);
// …later, or in another program: const const restored: Projectrestored =
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.parse: (serializedProject: string) => Project
Parses a Project serialized into a string, as defined by the OntoUML JSON Schema (https://w3id.org/ontouml/schema), and returns the corresponding instance of Project . The input is validated against the schema before any element is deserialized. Model elements, diagrams, views, and shapes are fully supported. When the shapes referenced by a view are absent from the serialization, the view keeps the serialized shape identifiers but its shapes are recreated with default dimensions and positions.
@paramserializedProject - the JSON string to parse.@throwsan error if the string is not a schema-valid serialized Project or if references between elements cannot be resolved.
parse
(const json: stringjson);
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const restored: Projectrestored instanceof class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
); // true
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const restored: Projectrestored.Project.classes: Class[]
The classes contained in this project.
classes
.Array<Class>.map<string>(callbackfn: (value: Class, index: number, array: Class[]) => string, thisArg?: any): string[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map
(c: Classc => c: Classc.NamedElement.getNameOrId(language?: string): string
Returns the name of this element in the given language, falling back to its id when no suitable name is available.
@paramlanguage - language tag of the desired name (e.g., `"en"`); when omitted, the name is selected according to LanguageString.languagePreference.
getNameOrId
())); // [ "Person" ]

parse throws a descriptive error rather than returning a broken model when the input is not a serialized project, when it violates the JSON Schema, or when a reference cannot be resolved — for example, a relation whose end points at an id that is absent from elements. The schema check runs before any element is deserialized, so malformed input from an external source fails fast with a list of violations instead of surfacing as a confusing error halfway through parsing.

Diagrams round-trip, geometry included

Model elements, diagrams, views, and shapes (the concrete rectangles and paths carrying pixel geometry) are all serialized as project elements and fully preserved by parse. When a serialization omits the shapes referenced by its views, the parsed views keep the shape ids but get default positions and sizes. See Diagrams for details.

Round-trip fidelity​

Parsing a serialization and serializing it again yields byte-identical JSON: the parser registers elements in the order they appear in the file on purpose. That property is what lets you treat OntoUML JSON as a stable interchange format — a model edited in code and one edited on the canvas diff cleanly.

import { class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
,
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const project: Projectproject.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.add(value: string, language?: string): void
Adds a text value in the given language, replacing any existing value for that language.
@paramvalue - the text to add.@paramlanguage - language tag of `value`; defaults to defaultLanguage.@throwsan error if `language` is not a valid language tag.
add
('Roundtrip');
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Model').PackageBuilder.build(): Package
Builds an instance of Package with the parameters passed to the builder. When no methods are evoked, the created package has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `contents: []`
@throwsan error if the builder has already been used.
build
();
const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const first: stringfirst = var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON
.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
stringify
(const project: Projectproject);
const const second: stringsecond = var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON
.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
stringify
(
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.parse: (serializedProject: string) => Project
Parses a Project serialized into a string, as defined by the OntoUML JSON Schema (https://w3id.org/ontouml/schema), and returns the corresponding instance of Project . The input is validated against the schema before any element is deserialized. Model elements, diagrams, views, and shapes are fully supported. When the shapes referenced by a view are absent from the serialization, the view keeps the serialized shape identifiers but its shapes are recreated with default dimensions and positions.
@paramserializedProject - the JSON string to parse.@throwsan error if the string is not a schema-valid serialized Project or if references between elements cannot be resolved.
parse
(const first: stringfirst));
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const first: stringfirst === const second: stringsecond); // true

Validating against the schema​

Both serialize and parse validate automatically, so most code never calls the validator by hand. When you want the check on its own — say, to vet a file before deciding what to do with it — use serializationUtils.validate. It accepts either a JSON string or an already-parsed object, returns silently on success, and throws an error listing every schema violation:

import { class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
,
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Model').PackageBuilder.build(): Package
Builds an instance of Package with the parameters passed to the builder. When no methods are evoked, the created package has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `contents: []`
@throwsan error if the builder has already been used.
build
();
// Passes silently: the library produces valid OntoUML JSON.
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.validate: (serializedProject: object | string) => void
Validates a serialized Project against the OntoUML JSON Schema (https://w3id.org/ontouml/schema).
@paramserializedProject - the serialization to validate, either as a JSON string or as the object resulting from parsing one.@throwsan error listing the schema violations if the serialization is invalid, or a `SyntaxError` if a string input is not valid JSON.
validate
(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON
.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
stringify
(const project: Projectproject));
// Throws, listing the violations found by the schema. try {
const serializationUtils: {
    parse: (serializedProject: string) => Project;
    serialize: (project: Project, space?: string | number) => string;
    validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and validation of Project objects according to the OntoUML JSON Schema (https://w3id.org/ontouml/schema). Both serialize and parse validate the serialization against the schema, throwing on invalid input.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.validate: (serializedProject: object | string) => void
Validates a serialized Project against the OntoUML JSON Schema (https://w3id.org/ontouml/schema).
@paramserializedProject - the serialization to validate, either as a JSON string or as the object resulting from parsing one.@throwsan error listing the schema violations if the serialization is invalid, or a `SyntaxError` if a string input is not valid JSON.
validate
('{ "type": "Project" }');
} catch (var e: anye) { var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
((var e: anye as Error).Error.message: stringmessage.String.split(separator: string | RegExp, limit?: number): string[] (+1 overload)
Split a string into substrings using the specified separator and return them as an array.
@paramseparator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.@paramlimit A value used to limit the number of elements returned in the array.
split
(':')[0]);
// Invalid serialization according to the OntoUML JSON Schema }

Under the hood, the library ships the schema (published as the ontouml-schema npm package and at https://w3id.org/ontouml/schema) and compiles it with Ajv on first use. If you need to validate in another stack, the schema targets JSON Schema draft 2020-12 and uses string formats, so any compliant validator works.

Structural vs. ontological validity

Schema validation only checks structure — that ids resolve, types are known, cardinalities are well-formed. It does not check whether the model is ontologically sound (a role with no identity provider, a mediation on a non-relator). Those are OntoUML anti-patterns; the query methods in Querying give you the raw material to write such checks yourself.

Now you try it​

The editor below builds a two-class model, serializes it, parses it back, and should report true for a clean round-trip. Add a third class of your own and confirm it survives the trip.

Loading playground…

Key takeaways​

  • Serialize with serializationUtils.serialize(project) — the output is validated against the OntoUML JSON Schema before it is returned. (JSON.stringify(project) produces the same JSON, without the check.)
  • Deserialize with serializationUtils.parse(json), which validates the input against the schema, then rebuilds a live Project and resolves all references — throwing on broken input at either step.
  • The round-trip is byte-identical, making OntoUML JSON a stable interchange format between code and the visual editor.
  • Shapes are serialized along with views, so diagram geometry round-trips.
  • Use serializationUtils.validate(input) to run the schema check on its own; structural validity is not ontological validity.

That closes the tour. From here, revisit Querying to turn models into analyses, or the visual editor to see what you built on a canvas.