@@ -63,19 +63,34 @@ public class SimpleSerializable implements Cloneable {
63
63
if (fields == null) {
64
64
fields = [];
65
65
}
66
+ var filter = arguments[0];
67
+ var ignoring = (filter == null || filter.ignoreDefaultFields ());
66
68
for (var i = 0; i < fields.length; i++) {
67
69
var field = fields[i];
68
70
var name = field.name;
69
- buffer[buffer.length] = String.fromCharCode (baseChar + name.length) ;
70
- buffer[buffer. length] = name;
71
+ if (filter != null && !filter.accept ( name)) continue ;
72
+ var nameStr = String.fromCharCode (baseChar + name. length) + name;
71
73
var type = field.type;
72
74
if (type == 'F' || type == 'D' || type == 'I' || type == 'L'
73
75
|| type == 'S' || type == 'B' || type == 'b') {
76
+ if (ignoring && this[name] == 0
77
+ && (type == 'F' || type == 'D' || type == 'I'
78
+ || type == 'L' || type == 'S' || type == 'B')) {
79
+ continue;
80
+ }
81
+ if (ignoring && this[name] == false && type == 'b') {
82
+ continue;
83
+ }
84
+ buffer[buffer.length] = nameStr;
74
85
buffer[buffer.length] = type;
75
86
var value = "" + this[name];
76
87
buffer[buffer.length] = String.fromCharCode (baseChar + value.length);
77
88
buffer[buffer.length] = value;
78
89
} else if (type == 'C') {
90
+ if (ignoring && this[name] == 0 || this[name] == '\0') {
91
+ continue;
92
+ }
93
+ buffer[buffer.length] = nameStr;
79
94
buffer[buffer.length] = type;
80
95
var value = "";
81
96
if (typeof this[name] == 'number') {
@@ -86,12 +101,21 @@ public class SimpleSerializable implements Cloneable {
86
101
buffer[buffer.length] = String.fromCharCode (baseChar + value.length);
87
102
buffer[buffer.length] = value;
88
103
} else if (type == 's') {
104
+ if (ignoring && this[name] == null) {
105
+ continue;
106
+ }
107
+ buffer[buffer.length] = nameStr;
89
108
this.serializeString(buffer, this[name]);
90
109
} else if (type.charAt (0) == 'A') {
91
- buffer[buffer.length] = type;
92
110
if (this[name] == null) {
111
+ if (ignoring) {
112
+ continue;
113
+ }
114
+ buffer[buffer.length] = nameStr;
93
115
buffer[buffer.length] = String.fromCharCode (baseChar - 1);
94
116
} else {
117
+ buffer[buffer.length] = nameStr;
118
+ buffer[buffer.length] = type;
95
119
var l4 = this[name].length;
96
120
if (l4 > 52) {
97
121
if (l4 > 0x4000) { // 16 * 1024
@@ -180,6 +204,7 @@ public String serialize(SimpleFilter filter) {
180
204
}
181
205
clazz = clazz .getSuperclass ();
182
206
}
207
+ boolean ignoring = (filter == null || filter .ignoreDefaultFields ());
183
208
try {
184
209
Field [] fields = (Field []) fieldSet .toArray (new Field [0 ]);
185
210
for (int i = 0 ; i < fields .length ; i ++) {
@@ -190,64 +215,83 @@ public String serialize(SimpleFilter filter) {
190
215
&& (modifiers & Modifier .STATIC ) == 0 ) {
191
216
String name = field .getName ();
192
217
if (filter != null && !filter .accept (name )) continue ;
193
- buffer .append ((char )(baseChar + name .length ()));
194
- buffer .append (name );
218
+ String nameStr = (char )(baseChar + name .length ()) + name ;
195
219
Class type = field .getType ();
196
220
if (type == float .class ) {
197
- buffer .append ('F' );
198
221
float f = field .getFloat (this );
222
+ if (f == 0.0 && ignoring ) continue ;
223
+ buffer .append (nameStr );
224
+ buffer .append ('F' );
199
225
String value = "" + f ;
200
226
buffer .append ((char ) (baseChar + value .length ()));
201
227
buffer .append (f );
202
228
} else if (type == double .class ) {
203
- buffer .append ('D' );
204
229
double d = field .getDouble (this );
230
+ if (d == 0.0d && ignoring ) continue ;
231
+ buffer .append (nameStr );
232
+ buffer .append ('D' );
205
233
String value = "" + d ;
206
234
buffer .append ((char ) (baseChar + value .length ()));
207
235
buffer .append (d );
208
236
} else if (type == int .class ) {
209
- buffer .append ('I' );
210
237
int n = field .getInt (this );
238
+ if (n == 0 && ignoring ) continue ;
239
+ buffer .append (nameStr );
240
+ buffer .append ('I' );
211
241
String value = "" + n ;
212
242
buffer .append ((char ) (baseChar + value .length ()));
213
243
buffer .append (n );
214
244
} else if (type == long .class ) {
215
- buffer .append ('L' );
216
245
long l = field .getLong (this );
246
+ if (l == 0L && ignoring ) continue ;
247
+ buffer .append (nameStr );
248
+ buffer .append ('L' );
217
249
String value = "" + l ;
218
250
buffer .append ((char ) (baseChar + value .length ()));
219
251
buffer .append (l );
220
252
} else if (type == short .class ) {
221
- buffer .append ('S' );
222
253
short s = field .getShort (this );
254
+ if (s == 0 && ignoring ) continue ;
255
+ buffer .append (nameStr );
256
+ buffer .append ('S' );
223
257
String value = "" + s ;
224
258
buffer .append ((char ) (baseChar + value .length ()));
225
259
buffer .append (s );
226
260
} else if (type == byte .class ) {
227
- buffer .append ('B' );
228
261
byte b = field .getByte (this );
262
+ if (b == 0 && ignoring ) continue ;
263
+ buffer .append (nameStr );
264
+ buffer .append ('B' );
229
265
String value = "" + b ;
230
266
buffer .append ((char ) (baseChar + value .length ()));
231
267
buffer .append (b );
232
268
} else if (type == char .class ) {
233
- buffer .append ('C' );
234
269
int c = 0 + field .getChar (this );
270
+ if (c == 0 && ignoring ) continue ;
271
+ buffer .append (nameStr );
272
+ buffer .append ('C' );
235
273
String value = "" + c ;
236
274
buffer .append ((char ) (baseChar + value .length ()));
237
275
buffer .append (c );
238
276
} else if (type == boolean .class ) {
239
- buffer .append ('b' );
240
277
boolean b = field .getBoolean (this );
278
+ if (b == false && ignoring ) continue ;
279
+ buffer .append (nameStr );
280
+ buffer .append ('b' );
241
281
String value = "" + b ;
242
282
buffer .append ((char ) (baseChar + value .length ()));
243
283
buffer .append (b );
244
284
} else if (type == String .class ) {
245
285
String s = (String ) field .get (this );
286
+ if (s == null && ignoring ) continue ;
287
+ buffer .append (nameStr );
246
288
serializeString (buffer , s );
247
289
} else { // Array ...
248
290
if (type == float [].class ) {
249
- buffer .append ("AF" );
250
291
float [] fs = (float []) field .get (this );
292
+ if (fs == null && ignoring ) continue ;
293
+ buffer .append (nameStr );
294
+ buffer .append ("AF" );
251
295
if (fs == null ) {
252
296
buffer .append ((char ) (baseChar - 1 ));
253
297
} else {
@@ -260,8 +304,10 @@ public String serialize(SimpleFilter filter) {
260
304
}
261
305
}
262
306
} else if (type == double [].class ) {
263
- buffer .append ("AD" );
264
307
double [] ds = (double []) field .get (this );
308
+ if (ds == null && ignoring ) continue ;
309
+ buffer .append (nameStr );
310
+ buffer .append ("AD" );
265
311
if (ds == null ) {
266
312
buffer .append ((char ) (baseChar - 1 ));
267
313
} else {
@@ -274,8 +320,10 @@ public String serialize(SimpleFilter filter) {
274
320
}
275
321
}
276
322
} else if (type == int [].class ) {
277
- buffer .append ("AI" );
278
323
int [] ns = (int []) field .get (this );
324
+ if (ns == null && ignoring ) continue ;
325
+ buffer .append (nameStr );
326
+ buffer .append ("AI" );
279
327
if (ns == null ) {
280
328
buffer .append ((char ) (baseChar - 1 ));
281
329
} else {
@@ -288,8 +336,10 @@ public String serialize(SimpleFilter filter) {
288
336
}
289
337
}
290
338
} else if (type == long [].class ) {
291
- buffer .append ("AL" );
292
339
long [] ls = (long []) field .get (this );
340
+ if (ls == null && ignoring ) continue ;
341
+ buffer .append (nameStr );
342
+ buffer .append ("AL" );
293
343
if (ls == null ) {
294
344
buffer .append ((char ) (baseChar - 1 ));
295
345
} else {
@@ -302,8 +352,10 @@ public String serialize(SimpleFilter filter) {
302
352
}
303
353
}
304
354
} else if (type == short [].class ) {
305
- buffer .append ("AS" );
306
355
short [] ss = (short []) field .get (this );
356
+ if (ss == null && ignoring ) continue ;
357
+ buffer .append (nameStr );
358
+ buffer .append ("AS" );
307
359
if (ss == null ) {
308
360
buffer .append ((char ) (baseChar - 1 ));
309
361
} else {
@@ -316,8 +368,10 @@ public String serialize(SimpleFilter filter) {
316
368
}
317
369
}
318
370
} else if (type == byte [].class ) {
319
- buffer .append ("AB" );
320
371
byte [] bs = (byte []) field .get (this );
372
+ if (bs == null && ignoring ) continue ;
373
+ buffer .append (nameStr );
374
+ buffer .append ("AB" );
321
375
if (bs == null ) {
322
376
buffer .append ((char ) (baseChar - 1 ));
323
377
} else {
@@ -330,8 +384,10 @@ public String serialize(SimpleFilter filter) {
330
384
}
331
385
}
332
386
} else if (type == char [].class ) {
333
- buffer .append ("AC" );
334
387
char [] cs = (char []) field .get (this );
388
+ if (cs == null && ignoring ) continue ;
389
+ buffer .append (nameStr );
390
+ buffer .append ("AC" );
335
391
if (cs == null ) {
336
392
buffer .append ((char ) (baseChar - 1 ));
337
393
} else {
@@ -344,8 +400,10 @@ public String serialize(SimpleFilter filter) {
344
400
}
345
401
}
346
402
} else if (type == boolean [].class ) {
347
- buffer .append ("Ab" );
348
403
boolean [] bs = (boolean []) field .get (this );
404
+ if (bs == null && ignoring ) continue ;
405
+ buffer .append (nameStr );
406
+ buffer .append ("Ab" );
349
407
if (bs == null ) {
350
408
buffer .append ((char ) (baseChar - 1 ));
351
409
} else {
@@ -358,8 +416,10 @@ public String serialize(SimpleFilter filter) {
358
416
}
359
417
}
360
418
} else if (type == String [].class ) {
361
- buffer .append ("AX" ); // special
362
419
String [] ss = (String []) field .get (this );
420
+ if (ss == null && ignoring ) continue ;
421
+ buffer .append (nameStr );
422
+ buffer .append ("AX" ); // special
363
423
if (ss == null ) {
364
424
buffer .append ((char ) (baseChar - 1 ));
365
425
} else {
@@ -493,8 +553,10 @@ private void serializeString(StringBuffer buffer, String s) throws UnsupportedEn
493
553
size = parseInt(sizeStr);
494
554
} catch (e) { }
495
555
}
556
+ // all fields are in their default values or no fields
557
+ if (size == 0) return true;
496
558
index++;
497
- if (size == 0 || size > length + start - index) return false;
559
+ if (size > length + start - index) return false;
498
560
}
499
561
500
562
var fieldMap = [];
@@ -654,12 +716,14 @@ public boolean deserialize(final String str, int start) {
654
716
try {
655
717
size = Integer .parseInt (sizeStr );
656
718
} catch (NumberFormatException e ) {
657
- //
719
+ return false ;
658
720
}
659
721
}
722
+ // all fields are in their default values or no fields
723
+ if (size == 0 ) return true ;
660
724
index ++;
661
725
// may be empty string or not enough string!
662
- if (size == 0 || size > length + start - index ) return false ;
726
+ if (size > length + start - index ) return false ;
663
727
}
664
728
665
729
Map fieldMap = new HashMap ();
0 commit comments