-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.js
245 lines (191 loc) · 6.92 KB
/
sample.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//==============================================================================
// Simple nodejs usage description.
//==============================================================================
// Define the prototypes to retain ---------------------------------------------
function Base(ts_) { this.ts = ts_; };
Base.prototype = {
__PROTO__: 'Base' /* indicator */
, ts: null
, f: function() {
return "ts=" + this.ts.toLocaleString();
}
}
function Derived(s) {
Base.call(this, 0);
this.s = s;
this.i = Infinity;
this.j = -Infinity;
this.x = NaN;
this.re = new RegExp(s, 'gim');
};
Derived.prototype = new Base;
Derived.prototype.__PROTO__ = 'Derived'; /* indicator */
Derived.prototype.g = function() { return "s=" + this.s; };
Derived.prototype.h = function() { return this.f() + "," + this.g(); };
function Transfer(id_) {
this.id = id_;
this.b = new Base(new Date);
this.sub = {
d1: new Derived("d1")
, d2: new Derived("d2")
};
this.a = [ new Derived("da[0]")
, new Base(123456789)
, new Derived("da[2]")
, new Base(987654321)
];
}
Transfer.prototype = {
__PROTO__: 'Transfer'
, id: -1
, getId: function() { return this.id; }
};
Transfer.prototype.__PROTO__ = 'Transfer';
var sent = new Transfer(123);
// Indicators for Date+RegExp encoding.
Date.prototype.__PROTO__ = 'Date';
RegExp.prototype.__PROTO__ = 'RegExp';
//==============================================================================
// nodejs load
if (typeof jsonPrototype !== 'object') {
var jsonPrototype = require('../jsonPrototype');
}
//==============================================================================
// Output to console and/or browser.
var log, show_ = function() { }
if (typeof window === 'object' && typeof window.document === 'object') {
show_ = function(msg) {
var doc = window.document
, aPre = doc.getElementsByTagName('pre')
, oPre = aPre.length ? aPre[aPre.length - 1] : null
;
oPre && oPre.appendChild(doc.createTextNode(msg + "\n"));
}
}
if (typeof console === 'object') {
log = function(msg, good) {
if (typeof good !== 'boolean') {
console.log(msg);
show_(msg);
return;
}
if (good) {
msg += ": OK";
console.log(msg);
show_(msg);
return true;
}
msg += ": FAIL";
show_(msg);
console.error(msg);
return false;
}
}
else { // Rhino
log = function(msg, good) {
msg += (typeof good === 'boolean'
? ": " + (good ? "OK" : "FAIL") : "");
print(msg);
show_(msg);
return good;
}
}
// -----------------------------------------------------------------------------
// Checks
function check(sent, flagDate_, received, flagOuter_) {
var r, good = true;
if (flagOuter_) {
r = (received.getId() === sent.getId());
log("CHECK getId(): " + received.getId(), r);
}
else {
r = (received.id === sent.getId());
log("CHECK sent.getId():" + sent.getId() +
" == received.id" + received.id, r);
}
if (flagDate_) {
r = (received.b.ts.toLocaleString() === sent.b.ts.toLocaleString());
log("CHECK Date: R/S " +
(typeof received.b.ts) + " " + received.b.ts +
" / " + (typeof sent.b.ts) + "" + sent.b.ts, r);
good = good && r;
}
else {
r = ((new Date(received.b.ts)).valueOf() === sent.b.ts.valueOf());
log("CHECK Date string: " + received.b.ts, r);
good = good && r;
}
r = (received.sub.d1.re.source === sent.sub.d1.re.source &&
received.sub.d1.re.toString() === sent.sub.d1.re.toString());
log("CHECK RegExp: " + received.sub.d1.re, r);
if (flagDate_) {
r = (received.b.f() === sent.b.f());
log("CHECK b.f(): " + received.b.f(), r);
}
r = (received.sub.d2.h() === sent.sub.d2.h());
log("CHECK sub.d2.h(): " + received.sub.d2.h(), r);
// Note: === will not do until sent has been unpacked.
r = (received.sub.d1.i == sent.sub.d1.i);
log("CHECK sub.d1 Infinity: " + received.sub.d1.i, r);
// Note: === will not do until sent has been unpacked.
r = (received.sub.d1.j == sent.sub.d1.j);
log("CHECK sub.d1 -Infinity: " + received.sub.d1.j, r);
r = (isNaN(received.sub.d1.x) === isNaN(sent.sub.d1.x));
log("CHECK sub.d1 NaN: " + received.sub.d1.x, r);
r = (received.a[2].h() === sent.a[2].h());
log("CHECK sub.a[2].h(): " + received.a[2].h(), r);
return good;
}
//==============================================================================
// Samples and Tests
var constructorsHash = { Base: Base, Derived: Derived, Transfer: Transfer };
// -----------------------------------------------------------------------------
// Direct Serialization
var jsonDirect = JSON.stringify(sent, jsonPrototype.pack);
log("jsonDirect = " + jsonDirect);
log("sent");
log(sent);
// Deserialization
var received = JSON.parse(
jsonDirect,
jsonPrototype.unpack.bind(constructorsHash)
);
log("direct receive");
log(received);
var r, good = true;
r = check(sent, false /*direct serialization cannot handle Date*/,
received, false /*direct deserialization cannot handle outer*/);
good = good && r;
// -----------------------------------------------------------------------------
// Indirect Serialization
jsonPrototype.packObject(sent);
// Caution! Object 'sent' has been altered!
var jsonIndirect = JSON.stringify(sent);
log("jsonIndirect = " + jsonIndirect);
// Restore sent object back to original state.
// jsonPrototype.unpackObject(sent, constructorsHash);
// Deserialization
received = JSON.parse(jsonIndirect);
received = jsonPrototype.unpackObject(received, constructorsHash);
log("indirect receive");
log(received);
r = check(sent, true /*indirect serialization handles Date*/,
received, true /*indirect deserialization handles outer*/);
good = good && r;
// -----------------------------------------------------------------------------
// Indirect Deserialization from Direct Serialization
received = JSON.parse(jsonDirect);
received = jsonPrototype.unpackObject(received, constructorsHash);
r = check(sent, false /*direct serialization cannot handle Date*/,
received, true /*indirect deserialization handles outer*/);
good = good && r;
// -----------------------------------------------------------------------------
// Direct Deserialization from Indirect Serialization
var received = JSON.parse(
jsonIndirect, jsonPrototype.unpack.bind(constructorsHash));
r = check(sent, true /*indirect serialization handles Date*/,
received, false /*direct deserialization cannot handle outer*/);
good = good && r;
log("Test Result", good);
//==============================================================================
// EOF