-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.js
126 lines (115 loc) · 2.95 KB
/
serializer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var obj;
function addToJsonObject(arg) {
if (arg == null || !("type" in arg)) {
return;
}
if ("attributes" in arg) {
obj[arg.type] = arg.attributes;
} else if ("value" in arg) {
obj[arg.type] = arg.value;
}
}
function addJsonBlock(key, block) {
obj[key] = block;
}
function addNoData() {
obj["error"] = {status : "failed", reason : "NO DATA AVAILABLE"};
}
module.exports = {
startJson: function() {
obj = {};
},
addJson: function() {
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (!(arg instanceof Array)) {
addToJsonObject(arg);
} else {
if (arg.length == 0) {
addNoData();
return;
}
for (var j = 0; j < arg.length; j++) {
addToJsonObject(arg[j]);
}
}
}
},
addJsonBlock: function(block, type) {
if (block == null || !"type" in block) {
return;
}
var oneTypeSpecified = (type != null);
var tempArray = [];
var finalObj;
for (var idx in block) {
if (type == null) {
type = block[idx].type;
}
if (!oneTypeSpecified && block[idx].attributes != null) {
tempArray.push(block[idx].attributes);
} else {
tempArray.push(block[idx]);
}
if (tempArray.length <= 1) {
finalObj = {};
finalObj = tempArray[0];
} else {
finalObj = tempArray;
}
}
addJsonBlock(type, finalObj);
},
setError: function(status, message) {
obj["error"] = {status : status, reason : message};
},
endJson: function(res) {
if (obj.length == 1) {
obj = obj[0]; // don"t need it to be JSON array if its single object
}
var response = JSON.stringify(obj);
if (res != null) {
res.contentType("application/json");
res.send(response);
res.end();
} else {
return response;
}
},
currentKeys: function() {
return Object.keys(obj).length;
},
genKeyPair: function (key, value) {
if (value == null) return;
var pair = {};
if (value instanceof Object && "value" in value) {
if ("timestamp" in value) {
pair = {"type" : key, "attributes" : {"value" : value.value, "timestamp" : value.timestamp}};
} else {
pair = {"type": key, "attributes" : {"value": value.value}};
}
if ("rawTimestamp" in value) {
pair.attributes.rawTimestamp = value.rawTimestamp;
}
} else {
pair = value;
if (key != null && key != "type" && key != "value" && key != "attribute") {
pair.type = key;
}
}
return pair;
},
genKeyPairs: function(key, values) {
var array = [];
if (key != null && (values instanceof Array)) {
for (var i = 0; i < values.length; i++) {
array.push(module.exports.genKeyPair(key, values[i]));
}
} else {
for (var property in values) {
array.push(module.exports.genKeyPair(property, values[property]));
}
}
return array;
}
};