jsog-typescript is a TypeScript module that implements the JavaScript Object Graph (JSOG) format, enabling the serialization and deserialization of JavaScript objects while preserving object references to prevent cycles and duplicates. Its key differentiator is the ability to instantiate actual TypeScript class instances during deserialization, leveraging decorators for type mapping. This allows developers to work with rich object models that retain their methods and `typeof` information after being processed. The package is currently at version 1.0.0-1 and appears to be actively maintained, offering specific integration patterns for frameworks like Angular 4 and AngularJS. It builds upon the core JSOG specification and incorporates ideas from `json-typescript-mapper` to provide this type-aware deserialization.
npm install jsog-typescriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining TypeScript classes with `@JsonProperty` decorators, serializing an object graph including circular references, and then deserializing it back into full TypeScript class instances, preserving methods and `instanceof` checks.
Add `"experimentalDecorators": true, "emitDecoratorMetadata": true` to the `compilerOptions` section of your `tsconfig.json`.
Install `reflect-metadata` (`npm install reflect-metadata`) and add `import 'reflect-metadata';` at the very top of your application's main entry point (e.g., `main.ts` or `index.ts`).
For properties that are a single custom object, use `@JsonProperty(MyClass)`. For properties that are arrays of custom objects, use `@JsonProperty(MyClass)` on the array property itself.
If your class requires constructor arguments, consider providing a default constructor or ensuring your class design allows for no-argument instantiation when deserialized by `jsog-typescript`.
Run `npm install reflect-metadata` and add `import 'reflect-metadata';` as the very first line in your main application entry file (e.g., `main.ts`).
Verify that `experimentalDecorators` and `emitDecoratorMetadata` are enabled in `tsconfig.json` and that all properties intended for deserialization into custom types have the `@JsonProperty()` decorator, with type arguments where necessary (e.g., `@JsonProperty(MyClass)`).
Ensure you are using `jsog.deserializeObject(data, MyClass)` or `jsog.deserializeArray(data, MyClass)` to explicitly tell `jsog-typescript` to instantiate objects as `MyClass` instances, and that `reflect-metadata` is properly imported.