SerializeWriter成员函数
序列化字段名称
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
| public void writeFieldName(String key, boolean checkSpecial) { if (key == null) { write("null:"); return; }
if (useSingleQuotes) { if (quoteFieldNames) { writeStringWithSingleQuote(key); write(':'); } else { writeKeyWithSingleQuoteIfHasSpecial(key); } } else { if (quoteFieldNames) { writeStringWithDoubleQuote(key, ':'); } else { boolean hashSpecial = key.length() == 0; for (int i = 0; i < key.length(); ++i) { char ch = key.charAt(i); boolean special = (ch < 64 && (sepcialBits & (1L << ch)) != 0) || ch == '\\'; if (special) { hashSpecial = true; break; } } if (hashSpecial) { writeStringWithDoubleQuote(key, ':'); } else { write(key); write(':'); } } } }
|
序列化字段名称方法writeFieldName主要的任务:
- 完成字段特殊字符的转译
- 添加字段的引号
处理输出key的特殊字符方法writeStringWithDoubleQuote
前面已经分析过了,序列化字段名称是否需要添加引号和特殊字符处理参考writeKeyWithSingleQuoteIfHasSpecial
:
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
| private void writeKeyWithSingleQuoteIfHasSpecial(String text) { final byte[] specicalFlags_singleQuotes = IOUtils.specicalFlags_singleQuotes;
int len = text.length(); int newcount = count + len + 1; if (newcount > buf.length) { if (writer != null) { if (len == 0) { write('\''); write('\''); write(':'); return; }
boolean hasSpecial = false; for (int i = 0; i < len; ++i) { char ch = text.charAt(i); if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch] != 0) { hasSpecial = true; break; } }
if (hasSpecial) { write('\''); } for (int i = 0; i < len; ++i) { char ch = text.charAt(i); if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch] != 0) { write('\\'); write(replaceChars[(int) ch]); } else { write(ch); } }
if (hasSpecial) { write('\''); } write(':'); return; } expandCapacity(newcount); }
if (len == 0) { int newCount = count + 3; if (newCount > buf.length) { expandCapacity(count + 3); } buf[count++] = '\''; buf[count++] = '\''; buf[count++] = ':'; return; }
int start = count; int end = start + len;
text.getChars(0, len, buf, start); count = newcount;
boolean hasSpecial = false;
for (int i = start; i < end; ++i) { char ch = buf[i]; if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch] != 0) { if (!hasSpecial) { newcount += 3; if (newcount > buf.length) { expandCapacity(newcount); } count = newcount;
System.arraycopy(buf, i + 1, buf, i + 3, end - i - 1); System.arraycopy(buf, 0, buf, 1, i); buf[start] = '\''; buf[++i] = '\\'; buf[++i] = replaceChars[(int) ch]; end += 2; buf[count - 2] = '\'';
hasSpecial = true; } else { newcount++; if (newcount > buf.length) { expandCapacity(newcount); } count = newcount;
System.arraycopy(buf, i + 1, buf, i + 2, end - i); buf[i] = '\\'; buf[++i] = replaceChars[(int) ch]; end++; } } }
buf[newcount - 1] = ':'; }
|
序列化Boolean类型字段键值对
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
| public void writeFieldValue(char seperator, String name, boolean value) { if (!quoteFieldNames) { write(seperator); writeFieldName(name); write(value); return; } int intSize = value ? 4 : 5;
int nameLen = name.length(); int newcount = count + nameLen + 4 + intSize; if (newcount > buf.length) { if (writer != null) { write(seperator); writeString(name); write(':'); write(value); return; } expandCapacity(newcount); }
int start = count; count = newcount;
buf[start] = seperator;
int nameEnd = start + nameLen + 1;
buf[start + 1] = keySeperator;
name.getChars(0, nameLen, buf, start + 2);
buf[nameEnd + 1] = keySeperator;
if (value) { System.arraycopy(":true".toCharArray(), 0, buf, nameEnd + 2, 5); } else { System.arraycopy(":false".toCharArray(), 0, buf, nameEnd + 2, 6); } }
|
序列化boolean类型的键值对属性,因为不涉及特殊字符,主要就是把原型序列化为字面量值。
序列化Int类型字段键值对
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
| public void writeFieldValue(char seperator, String name, int value) { if (value == Integer.MIN_VALUE || !quoteFieldNames) { write(seperator); writeFieldName(name); writeInt(value); return; }
int intSize = (value < 0) ? IOUtils.stringSize(-value) + 1 : IOUtils.stringSize(value);
int nameLen = name.length(); int newcount = count + nameLen + 4 + intSize; if (newcount > buf.length) { if (writer != null) { write(seperator); writeFieldName(name); writeInt(value); return; } expandCapacity(newcount); }
int start = count; count = newcount;
buf[start] = seperator;
int nameEnd = start + nameLen + 1;
buf[start + 1] = keySeperator;
name.getChars(0, nameLen, buf, start + 2);
buf[nameEnd + 1] = keySeperator; buf[nameEnd + 2] = ':';
IOUtils.getChars(value, count, buf); }
|
序列化int类型的键值对属性,因为不涉及特殊字符,主要就是把原型序列化为字面量值。截止到现在,已经把核心SerializWriter
类讲完了,剩余字段键值对极其类似writeFieldValue
boolean和int等,因此无需冗余分析。因为序列化真正开始之前,这个类极其基础并且非常重要,因此花的时间较多。