builder.js
1.51 KB
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
56
57
58
59
var util = require('../util');
function JsonBuilder() { }
JsonBuilder.prototype.build = function(value, shape) {
return JSON.stringify(translate(value, shape));
};
function translate(value, shape) {
if (!shape || value === undefined || value === null) return undefined;
switch (shape.type) {
case 'structure': return translateStructure(value, shape);
case 'map': return translateMap(value, shape);
case 'list': return translateList(value, shape);
default: return translateScalar(value, shape);
}
}
function translateStructure(structure, shape) {
var struct = {};
util.each(structure, function(name, value) {
var memberShape = shape.members[name];
if (memberShape) {
if (memberShape.location !== 'body') return;
var locationName = memberShape.isLocationName ? memberShape.name : name;
var result = translate(value, memberShape);
if (result !== undefined) struct[locationName] = result;
}
});
return struct;
}
function translateList(list, shape) {
var out = [];
util.arrayEach(list, function(value) {
var result = translate(value, shape.member);
if (result !== undefined) out.push(result);
});
return out;
}
function translateMap(map, shape) {
var out = {};
util.each(map, function(key, value) {
var result = translate(value, shape.value);
if (result !== undefined) out[key] = result;
});
return out;
}
function translateScalar(value, shape) {
return shape.toWireFormat(value);
}
/**
* @api private
*/
module.exports = JsonBuilder;