-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonCodec.js
More file actions
142 lines (116 loc) · 3.48 KB
/
jsonCodec.js
File metadata and controls
142 lines (116 loc) · 3.48 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function getJsonMessage(jsonStep) {
var txt = '';
var action = parseStepValue(jsonStep['action']);
var target = parseStepValue(jsonStep['target']);
var target_specifier = parseStepValue(jsonStep['target-specifier']);
var target_options = parseStepValue(jsonStep['target-options']);
var actuator = parseStepValue(jsonStep['actuator']);
var actuator_specifier = parseStepValue(jsonStep['actuator-specifier']);
var modifiers = parseStepValue(jsonStep['modifier']);
if (action != '') {
txt = '{\n';
if (action == 'response') {
txt += tab(1) + '"' + action + '": {\n';
txt += tab(2) + '"source": {\n';
txt += tab(3) + '"' + target + '": {\n';
txt += processDetails(target_specifier, 4) + '\n';
txt += tab(3) + '}\n';
txt += tab(2) + '},\n';
txt += processDetails(actuator, 2) + ',\n';
txt += processDetails(actuator_specifier, 2) + '\n';
txt += tab(1) + '}\n';
} else {
// action
txt += tab(1) + '"action": "' + action + '",\n';
// target
txt += tab(1) + '"target": {\n';
txt += tab(2) + '"' + target + '": {';
if (target_specifier != '') {
txt += '\n';
txt += processDetails(target_specifier, 3) + '\n';
txt += tab(2);
}
if (target_options != '') {
txt += '},\n';
txt += tab(2) + '"options": {\n';
txt += processDetails(target_options, 3) + '\n';
txt += tab(2);
}
txt += '}\n';
txt += tab(1) + '}';
if (actuator != '' || modifiers != '') {
txt += ',';
}
txt += '\n';
// actuator
if (actuator != '') {
txt += tab(1) + '"actuator": {\n';
txt += tab(2) + '"' + actuator + '": {'
if (actuator_specifier != '') {
txt += '\n';
txt += processDetails(actuator_specifier, 3) + '\n';
txt += tab(2)
}
txt += '}\n';
txt += tab(1) + '}';
if (modifiers != '') {
txt += ',';
}
txt += '\n';
}
// modifiers
if (modifiers != '') {
txt += tab(1) + '"modifiers": {\n';
if (modifiers != '') {
txt += processDetails(modifiers, 2) + '\n';
} else {
txt += tab(2) + '...\n';
}
txt += tab(1) + '}\n';
}
}
txt += '}';
}
// return
return txt;
}
function tab(numTabs) {
txt = '';
// add tabs
for (var tabNum = 0; tabNum < numTabs; tabNum++) {
txt += ' '; // four spaces
}
return txt;
}
function processDetails(specifier, numTabs) {
var txt = tab(numTabs);
var lines = specifier.split(',');
for (var lineNum = 0; lineNum < lines.length; lineNum++) {
var line = lines[lineNum].trim();
if (line.slice(0, 1) == '<') {
txt += line;
} else {
// create key-value pair with quotes
if (line.indexOf(':') > 0) {
var key = line.slice(0, line.indexOf(':')).trim();
var value = line.slice(line.indexOf(':') + 1).trim();
if (!isNumeric(value)) {
if (value.slice(0, 1) != '<') {
value = '"' + value + '"';
}
}
txt += '"' + key + '": ' + value;
} else {
txt += line;
}
}
// add comma and newline
if (lineNum + 1 < lines.length) {
txt += ',\n';
}
}
return txt;
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}