AMF0-TS
Fast AMF0 library written in pure Typescript with 0 dependencies, supporting TS, Node and the browser.
Installation
npm i amf0-ts
Type compatibility table
Type |
Serialize |
Deserialize |
Reason for exclusion |
Null |
✔️ |
✔️ |
- |
Undefined |
✔️ |
✔️ |
- |
Boolean |
✔️ |
✔️ |
- |
Number |
✔️ |
✔️ |
- |
String |
✔️ |
✔️ |
- |
Long string |
✔️ |
✔️ |
- |
Object |
✔️ |
✔️ |
- |
Object end |
✔️ |
✔️ |
- |
Reference |
✔️ |
✔️ |
- |
ECMA array |
✔️ |
✔️ |
- |
Date |
✔️ |
✔️ |
- |
Map |
✔️ |
✔️ |
- |
Set |
✔️ |
✔️ |
- |
Unsupported |
✔️ |
✔️ |
- |
Typed object |
✔️ |
✔️ |
- |
XML Document |
❌ |
❌ |
Who uses XML? |
Strict array |
❌ |
❌ |
No remoting support |
AVMPlus |
❌ |
❌ |
AMF0 only |
Extra compatibility table
Name |
Supported |
Class alias |
✔️ |
Dynamic property writer |
✔️ |
Usage
Below you'll find simple usage examples.
Basic types
Working with basic types is very self explanatory.
Typescript
import { AMF0, ECMAArray } from 'amf0-ts';
const obj: object = { id: 1 };
const ref: object = { ref1: obj, ref2: obj };
const mixed: ECMAArray = Object.assign([], [1,2,3], { 'Key': 'Value' });
const arr: ECMAArray = [1,2,3];
AMF0.parse(AMF0.stringify(obj));
AMF0.parse(AMF0.stringify(ref));
AMF0.parse(AMF0.stringify(mixed));
AMF0.parse(AMF0.stringify(arr));
Node
const { AMF0 } = require('amf0-ts');
const obj = { id: 1 };
const ref = { ref1: obj, ref2: obj };
const mixed = Object.assign([], [1,2,3], { 'Key': 'Value' })
const arr = [1,2,3];
AMF0.parse(AMF0.stringify(obj));
AMF0.parse(AMF0.stringify(ref));
AMF0.parse(AMF0.stringify(mixed));
AMF0.parse(AMF0.stringify(arr));
Browser
<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
<script>
const obj = { id: 1 };
const ref = { ref1: obj, ref2: obj };
const mixed = Object.assign([], [1,2,3], { 'Key': 'Value' })
const arr = [1,2,3];
AMF0.parse(AMF0.stringify(obj));
AMF0.parse(AMF0.stringify(ref));
AMF0.parse(AMF0.stringify(mixed));
AMF0.parse(AMF0.stringify(arr));
</script>
Changes
This library has a few changes:
- Movieclip has been changed to
Map
- Recordset has been changed to
Set
Typescript
import { AMF0 } from 'amf0-ts';
const map: Map<string, any> = new Map([[1, 'Value']]);
const set: Set<any> = new Set([1, 'A', 2, 'B', 3, 'C']);
AMF0.parse(AMF0.stringify(map));
AMF0.parse(AMF0.stringify(set));
Node
const { AMF0 } = require('amf0-ts');
const map = new Map([['Key', 'Value']]);
const set = new Set([1, 'A', 2, 'B', 3, 'C']);
AMF0.parse(AMF0.stringify(map));
AMF0.parse(AMF0.stringify(set));
Browser
<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
<script>
const map = new Map([['Key', 'Value']]);
const set = new Set([1, 'A', 2, 'B', 3, 'C']);
AMF0.parse(AMF0.stringify(map));
AMF0.parse(AMF0.stringify(set));
</script>
Typed class
This library also supports typed classes.
Typescript
import { AMF0 } from 'amf0-ts';
class Person {
public name:string;
public age:number;
constructor(name:string, age:number) {
this.name = name;
this.age = age;
}
}
AMF0.registerClassAlias('com.person', Person);
AMF0.isRegisteredClassAlias(Person);
const person: Person = new Person('Daan', 18);
AMF0.parse(AMF0.stringify(person));
AMF0.deregisterClassAlias(Person);
Node
const { AMF0 } = require('amf0-ts');
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
AMF0.registerClassAlias('com.person', Person);
AMF0.isRegisteredClassAlias(Person);
const person = new Person('Daan', 18);
AMF0.parse(AMF0.stringify(person));
AMF0.deregisterClassAlias(Person);
Browser
<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
<script>
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
AMF0.registerClassAlias('com.person', Person);
AMF0.isRegisteredClassAlias(Person);
const person = new Person('Daan', 18);
AMF0.parse(AMF0.stringify(person));
AMF0.deregisterClassAlias(Person);
</script>
Dynamic property writer
This library also supports Dynamic property writers.
Typescript
import { AMF0, IDynamicPropertyWriter, IDynamicPropertyOutput } from 'amf0-ts';
class HideSensitive implements IDynamicPropertyWriter {
constructor() { }
public writeDynamicProperties(obj: object, output: IDynamicPropertyOutput): void {
for (const prop in obj) {
if (prop.toLowerCase() !== 'password') {
output.writeDynamicProperty(prop, obj[prop]);
}
}
}
}
AMF0.setDynamicPropertyWriter(new HideSensitive());
AMF0.hasDynamicPropertyWriter();
AMF0.parse(AMF0.stringify({ id: 1, username: 'Zaseth', password: '123123' }));
AMF0.setDynamicPropertyWriter(null);
AMF0.hasDynamicPropertyWriter();
Node
const { AMF0 } = require('amf0-ts');
class HideSensitive {
constructor() { }
writeDynamicProperties(obj, output) {
for (const prop in obj) {
if (prop.toLowerCase() !== 'password') {
output.writeDynamicProperty(prop, obj[prop]);
}
}
}
}
AMF0.setDynamicPropertyWriter(new HideSensitive());
AMF0.hasDynamicPropertyWriter();
AMF0.parse(AMF0.stringify({ id: 1, username: 'Zaseth', password: '123123' }));
AMF0.setDynamicPropertyWriter(null);
AMF0.hasDynamicPropertyWriter();
Browser
<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
<script>
class HideSensitive {
constructor() { }
writeDynamicProperties(obj, output) {
for (const prop in obj) {
if (prop.toLowerCase() !== 'password') {
output.writeDynamicProperty(prop, obj[prop]);
}
}
}
}
AMF0.setDynamicPropertyWriter(new HideSensitive());
AMF0.hasDynamicPropertyWriter();
AMF0.parse(AMF0.stringify({ id: 1, username: 'Zaseth', password: '123123' }));
AMF0.setDynamicPropertyWriter(null);
AMF0.hasDynamicPropertyWriter();
</script>