@tripsnek/tmf
Version:
TypeScript Modeling Framework - A TypeScript port of the Eclipse Modeling Framework (EMF)
165 lines • 5.78 kB
TypeScript
import { EObject } from '../metamodel/api/eobject.js';
import { EClass } from '../metamodel/api/eclass.js';
import { EPackage } from '../metamodel/api/epackage.js';
import { EStructuralFeature } from '../metamodel/api/estructural-feature.js';
import { EAttribute } from '../metamodel/api/eattribute.js';
/**
* Utilities for converting between EObjects and JSON. Usage:
*
* (1) TJson.makeJson(EObject) - converts an EObject to JSON.
* (2) TJson.makeEObject(json) - deserializes EObject encoded with (1).
* (3) TJson.makeJsonArray(EObject[]) - converts an array of EObjects to a JSON Array.
* (4) TJson.makeEObjectArray(json) - converts a JSON Array to an array of EObjects.
*
* You may also configure which EClasses are eligible for conversion with
* 'addPackages(EPackage[])' and 'setPackages(EPackage[])'.
*
*/
export declare class TJson {
static JSON_FIELD_TYPESCRIPT_TYPE: string;
static packages: EPackage[];
static setPackages(packages: EPackage[]): void;
static addPackages(packages: EPackage[]): void;
static getPackages(): EPackage[];
private static warnIfNotInitialized;
/**
* Converts a TMF EObject to JSON.
*
* @param obj
*/
static makeJson(obj: EObject): any;
/**
* Converts an object in JSON Object into a TMF EObject.
* Creates proxy objects for unresolved non-containment references.
*
* @param json
* @return
*/
static makeEObject(jsonObj: any): EObject | undefined;
/**
* Creates a proxy object for an unresolved reference and sets it on the source object.
*
* @param ref The unresolved serialized reference
* @param context The deserialization context
*/
private static createAndSetProxy;
/**
* Creates a proxy EObject for an unresolved reference.
*
* @param fullId The full ID of the target object (format: "ClassName_actualId")
* @param reference The reference feature to determine the target EClass
* @returns A proxy EObject or undefined if creation failed
*/
private static createProxy;
/**
* Converts a TMF EObject array to JSON array.
*
* @param obj
*/
static makeJsonArray(objs: EObject[]): any[];
/**
* Converts an object in JSON Array into am array of TMF EObjects.
*
* @param json
* @return
*/
static makeEObjectArray(jsonArray: any[]): EObject[];
private static jsonToEObject;
private static deserializeReferencedObjects;
private static deserializeSingleValuedReference;
private static deserializeManyValuedReference;
protected static eObjectToJsonAux(obj: EObject, serializedSoFar: Map<EObject, any>, attributesOnly: boolean, context: SerializationContext): any;
private static referencesToJson;
private static manyValuedReferenceToJson;
private static singleValuedRefToJson;
private static attributesToJson;
/**
* Converts a primitive value (or a Date) for use inside JSON.
*
* @param val
* @return
*/
static primitiveValueToJson(attr: EAttribute, val: any): any;
static getJsonFieldName(feature: EStructuralFeature): string;
static eClassByNameCaseInsensitive(objectType: string, pkg: EPackage): EClass;
/**
* Sets all primitive (EAttribute) values on the eobject, given a
* JSONObject with the same fields.
*
* @param propsObj
* @param eObj
*/
static setPrimitiveValuesOnJson(propsObj: any, eObj: EObject): void;
}
export declare class SerializedReference {
fromId: string;
toId: string;
refName: string;
constructor(from: string, to: string, ref: string);
static create(fromDId: string, toDId: string, fromRef: string): SerializedReference;
/**
* Swizzles the object to JSON.
*/
serialize(): any;
/**
* Restores the swizzled reference.
*
* @param context The deserialization context
* @returns true if the reference was successfully resolved, false if target object not found
*/
deserialize(context: DeserializationContext): boolean;
}
/**
* Context object for managing state during serialization
*/
declare class SerializationContext {
private tempIdCounter;
private objectToTempId;
private readonly root;
constructor(root: EObject);
/**
* Gets or creates a stable identifier for an object.
* Uses the object's ID if available, otherwise creates a path-based identifier.
*/
getStableId(obj: EObject): string;
/**
* Creates a path-based identifier for an object based on its containment path.
* Format: "@path:ClassName:/feature1[index]/feature2[index]/..."
*/
private createPathBasedId;
}
/**
* Context object for managing state during deserialization
*/
declare class DeserializationContext {
private pathToObject;
private readonly root;
private idsToObjs;
constructor(root: EObject);
/**
* Builds an index of all contained objects using both real IDs and path-based IDs
*/
private buildIndex;
/**
* Gets the stable ID for an object (for indexing purposes during deserialization)
*/
getStableId(obj: EObject): string;
/**
* Creates a path-based identifier (same logic as SerializationContext)
*/
private createPathBasedId;
/**
* Resolves a path-based identifier to an object within the containment hierarchy.
*/
resolvePathBasedId(pathId: string): EObject | undefined;
/**
* Gets the index map for looking up objects by ID
*/
getIndex(): Map<string, EObject>;
/**
* Resolves an ID to an object, handling both regular and path-based IDs
*/
resolveId(id: string): EObject | undefined;
}
export {};
//# sourceMappingURL=tjson.d.ts.map