Features
- Type Safety
- Reference Relationship
- Referenced ArrayBuffer in ArrayBufferView
- Circular Reference
- Extensible
Supported Types
The types listed below are supported by default.
Primitive values
Value properties
Fundamental objects
Numbers & dates
Text processing
Indexed Collections
- Array
- Int8Array
- Uint8Array
- Uint8ClampedArray
- Int16Array
- Uint16Array
- Int32Array
- Uint32Array
- Float32Array
- Float64Array
- BigInt64Array
- BigUint64Array
Keyed collections
Structured data
Custom Types
const TSON=require('..');
class SuperMaxUltraBigUnlimitedNumber{
constructor(x,y){
this.x=BigInt(x);
this.y=BigInt(y);
}
};
const dumper=(value)=>value.x+'.'+value.y;
const loader=(data)=>new SuperMaxUltraBigUnlimitedNumber(...data.split('.'));
const matcher=(value)=>value instanceof SuperMaxUltraBigUnlimitedNumber;
const recursive=false;
TSON.register(SuperMaxUltraBigUnlimitedNumber, loader, dumper, matcher, recursive);
const raw=new SuperMaxUltraBigUnlimitedNumber("1234567891012345678910111","98765432109876543210222");
const tson=TSON.stringify(raw);
const result=TSON.parse(tson);
console.log('RAW',raw);
console.log('Result',result);
console.log('Text',tson);
// RAW SuperMaxUltraBigUnlimitedNumber {
// x: 1234567891012345678910111n,
// y: 98765432109876543210222n
// }
// Result SuperMaxUltraBigUnlimitedNumber {
// x: 1234567891012345678910111n,
// y: 98765432109876543210222n
// }
// Text {"type":"SuperMaxUltraBigUnlimitedNumber","data":"1234567891012345678910111.98765432109876543210222"}
Examples
const arrayBuffer=new ArrayBuffer(5);
const uint8Array=new Uint8Array(arrayBuffer,1,4);
uint8Array[0]=0x66;
uint8Array[1]=0xCC;
uint8Array[2]=0xFF;
const circle={
circle:null as any,
date:new Date(),
arrayBuffer,
uint8Array,
set:new Set([arrayBuffer,uint8Array]),
map:new Map([[arrayBuffer,uint8Array]])
}
circle.circle=circle;
const text=TSON.stringify(circle,null,2);
const result=TSON.parse(text);
console.log('Raw',circle);
console.log('Result',result);
console.log('Text',text);
- Raw
Raw <ref *1> {
circle: [Circular *1],
date: 2022-05-22T04:37:07.505Z,
arrayBuffer: ArrayBuffer { [Uint8Contents]: <00 66 cc ff 00>, byteLength: 5 },
uint8Array: Uint8Array(4) [ 102, 204, 255, 0 ],
set: Set(2) {
ArrayBuffer { [Uint8Contents]: <00 66 cc ff 00>, byteLength: 5 },
Uint8Array(4) [ 102, 204, 255, 0 ]
},
map: Map(1) {
ArrayBuffer { [Uint8Contents]: <00 66 cc ff 00>, byteLength: 5 } => Uint8Array(4) [ 102, 204, 255, 0 ]
}
}
- Result
Result <ref *1> {
circle: [Circular *1],
date: 2022-05-22T04:37:07.505Z,
arrayBuffer: ArrayBuffer { [Uint8Contents]: <00 66 cc ff 00>, byteLength: 5 },
uint8Array: Uint8Array(4) [ 102, 204, 255, 0 ],
set: Set(2) {
ArrayBuffer { [Uint8Contents]: <00 66 cc ff 00>, byteLength: 5 },
Uint8Array(4) [ 102, 204, 255, 0 ]
},
map: Map(1) {
ArrayBuffer { [Uint8Contents]: <00 66 cc ff 00>, byteLength: 5 } => Uint8Array(4) [ 102, 204, 255, 0 ]
}
}
- Text
Text {
"type": "Object",
"data": {
"circle": {
"type": "$ref",
"data": []
},
"date": {
"type": "Date",
"data": "2022-05-22T04:37:07.505Z"
},
"arrayBuffer": {
"type": "ArrayBuffer",
"data": [
0,
102,
204,
255,
0
]
},
"uint8Array": {
"type": "Uint8Array",
"data": {
"buffer": {
"type": "$ref",
"data": [
"arrayBuffer"
]
},
"offset": {
"type": "number",
"data": 1
},
"length": {
"type": "number",
"data": 4
}
}
},
"set": {
"type": "Set",
"data": [
{
"type": "$ref",
"data": [
"arrayBuffer"
]
},
{
"type": "$ref",
"data": [
"uint8Array"
]
}
]
},
"map": {
"type": "Map",
"data": [
{
"type": "Array",
"data": [
{
"type": "$ref",
"data": [
"arrayBuffer"
]
},
{
"type": "$ref",
"data": [
"uint8Array"
]
}
]
}
]
}
}
}