typed-serializer v2.1.14
Typed-Serializer
Typed-serializer like JSON.stringify() and JSON.parse() for typed objects (class instance).
Typed-serializer Parser.parse() method return typed objects, which class definition in modules.
Use browserify framework for browsers.
Install:
npm i typed-serializer
Table of Contents
Features
- save referenced objects types;
- resolve circular reference;
- resolve reference to the same objects.
Example
const Serializer = require('typed-serializer').Serializer;
const Stringifier = require('typed-serializer').Stringifier;
const Parser = require('typed-serializer').Parser;
const includedClasses = require('./includedClasses');
const includedClasses2 = require('./includedClasses2');
const excludedClasses = require('./excludedClasses');
const { CustomContent } = require('../classes/includedClasses');
//array of modules which classes including to stringify and parse process
var modules = [includedClasses, includedClasses2];
//object to stringify
var customContent = new CustomContent()
//stringify object to json
var stringifier = new Stringifier(modules);
var json = stringifier.stringify(customContent, true);
//parse object from json
var parser = new Parser(modules);
resultCustomContent = parser.parse(json);
//use referenced objects Set
var arr = Array.from(parser.referencedObjectsSet);
assert.ok(arr[0] instanceof Unit);
assert.ok(arr[1] instanceof Unit);
assert.ok(arr[2] instanceof CustomUnit);
assert.ok(arr[3] instanceof CustomContent);
assert.ok(arr.length === 4);
For example we define classes in three modules:
- includedclasses - classes including to stringify and parse process;
- includedclasses2 - classes including to stringify and parse process;
- excludedclasses - classes excluded from stringify and parse process.
Module: includedClasses
import { Layout, Hint } from "./excludedClasses";
import { CustomUnit, Unit } from "./includedClasses2";
export class Content {
contentName;
constructor() {
this.contentName = 'Content';
}
}
export class CustomContent extends Content {
customContentName;
units: Array<Unit> = new Array<Unit>();
layouts: Array<Layout> = new Array<Layout>();
hint: Hint;
customUnit: CustomUnit;
constructor() {
super();
this.customUnit = new CustomUnit(this);
this.customContentName = 'CustomContent';
this.units.push(new Unit());
var unit = new Unit();
this.units.push(unit);
this.units.push(unit);
this.layouts.push(new Layout());
this.layouts.push(new Layout());
this.layouts.push(new Layout());
this.hint = new Hint();
}
}
Module: includedClasses2
import { Content, CustomContent } from "./includedClasses";
export class Unit {
unitName;
constructor() {
this.unitName = "Unit";
}
}
export class CustomUnit extends Unit {
customUnitName: string;
content: Content;
constructor(content: Content) {
super();
this.customUnitName = "CustomUnitUnit";
this.content = content;
}
}
Module: excludedClasses
export class Layout {
layoutName;
hint;
constructor() {
this.layoutName = "Layout";
this.hint = new Hint();
}
}
export class Hint {
hintName;
constructor() {
this.hintName = 'Hint';
}
}
Result: resultCustomContent (see "Example")
Result of parsing json to typed object:
Fig. 1. Result object "resultCustomContent"
Object resultCustomContent has type "CustomContent" from module "includedClasses". Reference object "customUnit" has type "CustomUnit" from module "includedClasses2". References objects "units" have type "Unit" from module "includedClasses2". 1. "customUnit" has circular reference "content" to parent object. Return value "resultCustomContent". 2. "hint" and "layouts" are referencing to objects which classes defined in module "excludedClasses". This module missing in array of modules including in stringify and parse process. Return value null. 3. "units1" and "units2" are referencing to the same object.
Result: referencedObjectsSet (see "Example")
All objects which type parsed added to Set "referencedObjectsSet".
Fig. 2. Result object "resultCustomContent" - objects added to "referencedObjectsSet"
Compare figure 2 objects types and example code (units1 and units2 - the same objects):
...
var arr = Array.from(parser.referencedObjectsSet);
assert.ok(arr[0] instanceof Unit);
assert.ok(arr[1] instanceof Unit);
assert.ok(arr[2] instanceof CustomUnit);
assert.ok(arr[3] instanceof CustomContent);
assert.ok(arr.length === 4);
Service attributes
All referenced objects after parsing have service attributes (see figure 3):
- typedSerializer_id,
- typedSerializer_nativeClassName. To remove this attrs from parsed objects run parser.removeTheExcess() after parsing json
Fig. 3. Referenced object - service attributes
References
Fig. 4. Serializer class diagramm
Serializer
constructor()
@param context - module or module array to finds object types
constructor(context: any)
getGlobal()
Static method. Return global context.
@param namespace - namespace in global context (optional)
static getGlobal(namespace: string): any
setGlobal()
Static method. Set modules exports into global context.
@param context - module or module array
@param namespace - namespace in global context (optional)
static setGlobal(context: any, namespace: string): void
clearGlobal()
Static method. Remove from global context all modules exports.
@param context - module or module array
@param namespace - namespace in global context (optional)
static clearGlobal(context: any, namespace: string): void
Stringifier
constructor()
@param context - module or module array to finds object types
constructor(context: any)
stringify()
Stringify obj like JSON.stringify().
Saves objects types in property 'nativeClassName'. Objects types looks in modules or global context.
If ignoreNotFound == true and object types not found in modules or global context - return undefined.
@param obj - object to stringify
@param ignoreNotFound - if true - ignore object
stringify(obj: any, ignoreNotFound: boolean = false): string
Parser
referencedObjectsSet
Set of referenced objects. Your can call Parser.parse() several times. That adds referenced objects in Set. After parsing your can call some method for each object in set.
constructor()
@param context - module or module array to finds object types
constructor(context: any)
parse()
Parse json string like JSON.parse(). Include objects types. Objects types looks in modules or global context.
@param json - json string
parse(json: string): Object
removeTheExcess()
Removes attrs "typedSerializer_id" and "typedSerializer_nativeClassName" from parsed objects.
removeTheExcess(): void
Using global context example
Using global
...
//array of modules which classes including to stringify and parse process
var modules = [includedClasses, includedClasses2];
//Set modules exports into global context
Serializer.setGlobal(modules);
//object to stringify
var customContent = new CustomContent()
//stringify object to json
var stringifier = new Stringifier();
var json = stringifier.stringify(customContent, true);
//parse object from json
var parser = new Parser();
resultCustomContent = parser.parse(json);
//Remove modules exports from global context
Serializer.clearGlobal(modules);
Using global with namespace
...
//array of modules which classes including to stringify and parse process
var modules = [includedClasses, includedClasses2];
//Set modules exports into global context
Serializer.setGlobal(modules, 'testParseNameSpace');
var globalNameSpace = Serializer.getGlobal('testParseNameSpace');
//object to stringify
var customContent = new CustomContent()
//stringify object to json
var stringifier = new Stringifier(globalNameSpace);
var json = stringifier.stringify(customContent, true);
//parse object from json
var parser = new Parser(globalNameSpace);
resultCustomContent = parser.parse(json);
//Remove modules exports from global context
Serializer.clearGlobal(modules, 'testParseNameSpace');
9 months ago
12 months ago
12 months ago
11 months ago
12 months ago
12 months ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago