-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializerTest.d
462 lines (350 loc) · 10.6 KB
/
SerializerTest.d
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
module test.SerializerTest;
import hunt.xml;
import hunt.serialization.Common;
import hunt.logging.ConsoleLogger;
import std.conv;
import std.datetime;
import std.format;
struct Test {
}
class SerializerTest {
void testAll() {
// testBasic();
testGetAsClass01();
// testXmlSerializable();
}
void testBasic() {
// QimenEntryOrderConfirmMessage m = new QimenEntryOrderConfirmMessage();
// //
// Document xv = XmlSerializer.toDocument(m);
// trace(xv.toPrettyString());
}
@Test void testGetAsClass01() {
auto creationTime = Clock.currTime;
long currentStdTime = Clock.currStdTime;
Greeting gt = new Greeting();
gt.initialization();
gt.setPrivateMember("private member");
gt.id = 123;
gt.content = "Hello, world!";
gt.creationTime = creationTime;
gt.currentTime = currentStdTime;
gt.setColor("Red");
gt.setContent("Hello");
gt.currentGuest.name = "Jerry";
gt.currentGuest.age = 30;
//
Document xv = XmlSerializer.toDocument(gt);
trace(xv.toPrettyString());
//
Greeting gt1 = XmlSerializer.toObject!(Greeting)(xv);
assert(gt1 !is null);
// trace("gt====>", gt, "====");
// trace("gt1====>", gt1, "====");
// trace(gt1.getContent());
// warning(gt1.getColor());
assert(gt.getPrivateMember == gt1.getPrivateMember);
assert(gt.id == gt1.id);
assert(gt.content == gt1.content);
assert(gt.creationTime == gt1.creationTime);
assert(gt.currentTime != gt1.currentTime);
assert(0 == gt1.currentTime);
assert(gt1.settings !is null);
assert(gt.getColor() == gt1.getColor());
assert(gt1.getColor() == "Red");
assert(gt.getContent() == gt1.getContent());
assert(gt1.getContent() == "Hello");
// array
string[] members = gt1.members;
assert(members.length >=2);
assert(members[0] == "Alice");
string[string] languages = gt1.languages;
assert(languages.length >=2);
assert(languages["en-us"] == "English");
Guest[] guests = gt1.guests;
assert(guests.length >=1);
assert(guests[0].name == gt.guests[0].name);
assert(guests[0].age == gt.guests[0].age);
Guest currentGuest = gt1.currentGuest;
warning(currentGuest.name);
warning(currentGuest.age);
assert(currentGuest.name == gt.currentGuest.name);
}
void testXmlSerializable() {
Element rootNode;
Element element;
Attribute attribute;
GreetingSettings settings = new GreetingSettings();
settings.name = "hunt";
/* ---------------------------------- class --------------------------------- */
Document xml_class = XmlSerializer.toDocument(settings);
// info(xml_class.toPrettyString());
rootNode = xml_class.firstNode();
attribute = rootNode.firstAttribute(MetaTypeName);
assert(attribute is null);
element = rootNode.firstNode("Color");
assert(element !is null);
/* -------------------------------- interface ------------------------------- */
ISettings isettings = settings;
Document xml_interface = XmlSerializer.toDocument(isettings);
// info(xml_interface.toPrettyString());
rootNode = xml_interface.firstNode();
attribute = rootNode.firstAttribute(MetaTypeName);
assert(attribute !is null);
// trace(attribute.toString());
element = rootNode.firstNode("Color");
assert(element !is null);
// trace(element.toString());
/* ------------------------------- base class ------------------------------- */
GreetingSettingsBase settingBase = settings;
Document xml_base = XmlSerializer.toDocument(settingBase);
info(xml_base.toPrettyString());
rootNode = xml_interface.firstNode();
attribute = rootNode.firstAttribute(MetaTypeName);
assert(attribute !is null);
// trace(attribute.toString());
element = rootNode.firstNode("Color");
assert(element !is null);
// trace(element.toString());
}
}
/**
*
*/
interface ISettings : XmlSerializable {
string color();
void color(string c);
}
/**
*
*/
@XmlRootElement("Greeting-SettingsBase")
abstract class GreetingSettingsBase : ISettings {
string city;
string name = "name in base";
// abstract Element xmlSerialize();
Element xmlSerialize() {
Element result = new Element(typeof(this).stringof);
result.appendNode(XmlSerializer.toXmlElement("_city", city));
return result;
}
void xmlDeserialize(Element value) {
// do nothing
warning("TODO: ", value.toString());
}
}
@XmlRootElement("Greeting-Settings")
class GreetingSettings : GreetingSettingsBase {
@XmlAttribute("ID")
int id = 1001;
@XmlElement("Color")
string _color;
string name;
this() {
_color = "black";
}
this(string color) {
_color = color;
}
string color() {
return _color;
}
void color(string c) {
this._color = c;
}
override Element xmlSerialize() {
// Method 1
// return XmlSerializer.serializeObject!(SerializationOptions.Default.traverseBase(false))(this);
return XmlSerializer.serializeObject!(SerializationOptions.Default)(this);
// // Method 2
// Element result = super.xmlSerialize();
// result.setName(typeof(this).stringof);
// // result.appendNode(XmlSerializer.toXmlElement("_city", city));
// import std.traits;
// alias xmlAttributeUDAs = getUDAs!(_color, XmlElement);
// static if(xmlAttributeUDAs.length > 0) {
// enum ColorName = xmlAttributeUDAs[0].name;
// } else {
// enum ColorName = "_color";
// }
// result.appendNode(XmlSerializer.toXmlElement(ColorName, _color));
// return result;
}
override void xmlDeserialize(Element value) {
// _color = value["_color"].str;
info("Using XmlSerializable's interface for ", value.toString());
XmlSerializer.deserializeObject!(typeof(this), TraverseBase.yes)(this, value);
// It's not necessary to call super's xmlDeserialize;
}
}
/**
*
*/
class GreetingBase {
@XmlAttribute("ID")
int id;
private string content;
this() {
}
this(int id, string content) {
this.id = id;
this.content = content;
}
void setContent(string content) {
this.content = content;
}
string getContent() {
return this.content;
}
override string toString() {
return "id=" ~ to!string(id) ~ ", content=" ~ content;
}
}
/**
*
*/
class Greeting : GreetingBase {
private string privateMember;
private ISettings settings;
Object.Monitor skippedMember;
alias TestHandler = void delegate(string);
// FIXME: Needing refactor or cleanup -@zxp at 6/16/2019, 12:33:02 PM
//
string content; // test for the same field
SysTime creationTime;
SysTime[] nullTimes;
SysTime[] times;
@XmlIgnore
long currentTime;
Guest currentGuest;
byte[] bytes;
string[] members;
Guest[] guests;
// @XmlAttribute()
string[string] languages;
this() {
super();
}
this(int id, string content) {
super(id, content);
this.content = ">>> " ~ content ~ " <<<";
initialization();
}
void initialization() {
settings = new GreetingSettings();
times = new SysTime[2];
times[0] = Clock.currTime;
times[1] = Clock.currTime;
members = new string[2];
members[0] = "Alice";
members[1] = "Bob";
guests = new Guest[1];
guests[0] = new Guest();
guests[0].name = "guest01";
guests[0].age = 25;
languages["zh-cn"] = "中文";
languages["en-us"] = "English";
currentGuest = new Guest();
currentGuest.name = "Current Guest";
currentGuest.age = 13;
}
void addGuest(string name, int age) {
Guest g = new Guest();
g.name = name;
g.age = age;
guests ~= g;
}
void setColor(string color) {
settings.color = color;
}
string getColor() {
return settings.color();
}
void voidReturnMethod() {
}
void setPrivateMember(string value) {
this.privateMember = value;
}
string getPrivateMember() {
return this.privateMember;
}
override string toString() {
string s = format("content=%s, creationTime=%s, currentTime=%s",
content, creationTime, currentTime);
return s;
}
}
class Guest {
string name;
int age;
}
// @XmlRootElement("request")
// class QimenEntryOrderConfirmMessage
// {
// EntryOrder entryOrder;
// OrderLine[] orderLines;
// this() {
// entryOrder = new EntryOrder();
// orderLines ~= new OrderLine();
// }
// string getMethod()
// {
// return "taobao.qimen.entryorder.confirm";
// }
// }
// class EntryOrder
// {
// string orderCode;
// // string orderId;
// // string orderType;
// // string warehouseName;
// // int totalOrderLines;
// // string entryOrderCode;
// // string ownerCode;
// // string purchaseOrderCode;
// // string warehouseCode;
// // string entryOrderId;
// // string entryOrderType;
// // string outBizCode;
// // int confirmType;
// // string status;
// // string operateTime;
// // string remark;
// // string freight;
// // string subOrderType;
// // string responsibleDepartment;
// // string shopNick;
// // string shopCode;
// }
// class OrderLine
// {
// this() {
// batchs ~= new Batch();
// }
// string orderLineNo;
// // string sourceOrderCode;
// // string subSourceOrderCode;
// // string ownerCode;
// // string itemCode;
// // string itemId;
// // string inventoryType;
// // int planQty;
// // int actualQty;
// // string batchCode;
// // string productDate;
// // string expireDate;
// // string produceCode;
// Batch[] batchs;
// // string remark;
// // string unit;
// // SnList snList;
// }
// class Batch
// {
// string batchCode = "abc";
// // string productDate;
// // string expireDate;
// // string produceCode;
// // string inventoryType;
// // int actualQty;
// }