-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
48 lines (46 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var serialize = (v, options) => {
if (!options) {
options = {
metakey: '__phpSerializedObject__',
};
}
var ret = '';
if (v === null) {
ret = 'N;';
} else if (typeof v === 'boolean') {
ret = 'b:' + (v ? '1' : '0') + ';';
} else if (typeof v === 'number' || (typeof v === 'string' && "" + (+v) === v)) {
if (v % 1 === 0) {
ret = 'i:' + v + ';';
} else if (isNaN(v)) {
ret = 'd:NAN;';
} else {
ret = 'd:' + v.toFixed(16) + ';';
}
} else if (typeof v === "string") {
ret = "s:" + Buffer.byteLength(v, 'utf8') + ':"' + v + '";';
} else if (v[options.metakey]) {
var meta = v[options.metakey];
var properties = Object.keys(v).filter(k => { return k !== options.metakey; }).map(k => {
var key = k;
if (meta.access && meta.access[k] && meta.access[k] === "protected") {
key = "\0*\0" + k;
}
else if (meta.access && meta.access[k] && meta.access[k] === "private") {
key = "\0" + meta.name + "\0" + k;
}
return serialize(key) + serialize(v[k]);
});
ret = 'O:' + meta.name.length + ':"' + meta.name + '":' + properties.length + ':{' + properties.join('') + '}';
} else if (typeof v === 'object') {
var properties = Object.keys(v).map(k => {
return serialize(k, options) + serialize(v[k], options);
});
ret = 'a:' + properties.length + ':{' + properties.join('') + '}';
}
return ret;
}
exports.serialize = serialize;
exports.unserialize = function (s, options) {
throw new Error('TODO');
};