-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserialization.js
55 lines (48 loc) · 1.33 KB
/
serialization.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
49
50
51
52
53
54
55
var _ = require('underscore');
var converter = require('aws-sdk/lib/dynamodb/converter');
module.exports.serialize = function(item) {
function replacer(key, value) {
if (Buffer.isBuffer(this[key])) return this[key].toString('base64');
if (this[key].BS &&
Array.isArray(this[key].BS) &&
_(this[key].BS).every(function(buf) {
return Buffer.isBuffer(buf);
}))
{
return {
BS: this[key].BS.map(function(buf) {
return buf.toString('base64');
})
};
}
return value;
}
return JSON.stringify(Object.keys(item).reduce(function(obj, key) {
obj[key] = converter.input(item[key]);
return obj;
}, {}), replacer);
};
module.exports.deserialize = function(str) {
function reviver(key, value) {
if (typeof value === 'object' && value.B && typeof value.B === 'string') {
return { B: new Buffer.from(value.B, 'base64') };
}
if (typeof value === 'object' &&
value.BS &&
Array.isArray(value.BS))
{
return {
BS: value.BS.map(function(s) {
return new Buffer.from(s, 'base64');
})
};
}
return value;
}
var obj = JSON.parse(str, reviver);
return Object.keys(obj).reduce(function(item, key) {
var value = converter.output(obj[key]);
item[key] = value;
return item;
}, {});
};