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
| public void write(JSONSerializer serializer , Object object , Object fieldName , Type fieldType , int features , boolean unwrapped) throws IOException { SerializeWriter out = serializer.out;
if (object == null) { out.writeNull(); return; }
Map<?, ?> map = (Map<?, ?>) object; final int mapSortFieldMask = SerializerFeature.MapSortField.mask; if ((out.features & mapSortFieldMask) != 0 || (features & mapSortFieldMask) != 0) { if (map instanceof JSONObject) { map = ((JSONObject) map).getInnerMap(); }
if ((!(map instanceof SortedMap)) && !(map instanceof LinkedHashMap)) { try { map = new TreeMap(map); } catch (Exception ex) { } } }
if (serializer.containsReference(object)) { serializer.writeReference(object); return; }
SerialContext parent = serializer.context; serializer.setContext(parent, object, fieldName, 0); try { if (!unwrapped) { out.write('{'); }
serializer.incrementIndent();
Class<?> preClazz = null; ObjectSerializer preWriter = null;
boolean first = true;
if (out.isEnabled(SerializerFeature.WriteClassName)) { String typeKey = serializer.config.typeKey; Class<?> mapClass = map.getClass(); boolean containsKey = (mapClass == JSONObject.class || mapClass == HashMap.class || mapClass == LinkedHashMap.class) && map.containsKey(typeKey); if (!containsKey) { out.writeFieldName(typeKey); out.writeString(object.getClass().getName()); first = false; } }
for (Map.Entry entry : map.entrySet()) { Object value = entry.getValue();
Object entryKey = entry.getKey();
{ List<PropertyPreFilter> preFilters = serializer.propertyPreFilters; if (preFilters != null && preFilters.size() > 0) { if (entryKey == null || entryKey instanceof String) { if (!this.applyName(serializer, object, (String) entryKey)) { continue; } } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { String strKey = JSON.toJSONString(entryKey); if (!this.applyName(serializer, object, strKey)) { continue; } } } } { List<PropertyPreFilter> preFilters = this.propertyPreFilters; if (preFilters != null && preFilters.size() > 0) { if (entryKey == null || entryKey instanceof String) { if (!this.applyName(serializer, object, (String) entryKey)) { continue; } } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { String strKey = JSON.toJSONString(entryKey); if (!this.applyName(serializer, object, strKey)) { continue; } } } }
{ List<PropertyFilter> propertyFilters = serializer.propertyFilters; if (propertyFilters != null && propertyFilters.size() > 0) { if (entryKey == null || entryKey instanceof String) { if (!this.apply(serializer, object, (String) entryKey, value)) { continue; } } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { String strKey = JSON.toJSONString(entryKey); if (!this.apply(serializer, object, strKey, value)) { continue; } } } } { List<PropertyFilter> propertyFilters = this.propertyFilters; if (propertyFilters != null && propertyFilters.size() > 0) { if (entryKey == null || entryKey instanceof String) { if (!this.apply(serializer, object, (String) entryKey, value)) { continue; } } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { String strKey = JSON.toJSONString(entryKey); if (!this.apply(serializer, object, strKey, value)) { continue; } } } }
{ List<NameFilter> nameFilters = serializer.nameFilters; if (nameFilters != null && nameFilters.size() > 0) { if (entryKey == null || entryKey instanceof String) { entryKey = this.processKey(serializer, object, (String) entryKey, value); } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { String strKey = JSON.toJSONString(entryKey); entryKey = this.processKey(serializer, object, strKey, value); } } } { List<NameFilter> nameFilters = this.nameFilters; if (nameFilters != null && nameFilters.size() > 0) { if (entryKey == null || entryKey instanceof String) { entryKey = this.processKey(serializer, object, (String) entryKey, value); } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { String strKey = JSON.toJSONString(entryKey); entryKey = this.processKey(serializer, object, strKey, value); } } }
{ if (entryKey == null || entryKey instanceof String) { value = this.processValue(serializer, null, object, (String) entryKey, value); } else { boolean objectOrArray = entryKey instanceof Map || entryKey instanceof Collection; if (!objectOrArray) { String strKey = JSON.toJSONString(entryKey); value = this.processValue(serializer, null, object, strKey, value); } } }
if (value == null) { if (!out.isEnabled(SerializerFeature.WRITE_MAP_NULL_FEATURES)) { continue; } }
if (entryKey instanceof String) { String key = (String) entryKey;
if (!first) { out.write(','); }
if (out.isEnabled(SerializerFeature.PrettyFormat)) { serializer.println(); } out.writeFieldName(key, true); } else { if (!first) { out.write(','); }
if (out.isEnabled(NON_STRINGKEY_AS_STRING) && !(entryKey instanceof Enum)) { String strEntryKey = JSON.toJSONString(entryKey); serializer.write(strEntryKey); } else { serializer.write(entryKey); }
out.write(':'); }
first = false;
if (value == null) { out.writeNull(); continue; }
Class<?> clazz = value.getClass();
if (clazz != preClazz) { preClazz = clazz; preWriter = serializer.getObjectWriter(clazz); }
if (SerializerFeature.isEnabled(features, SerializerFeature.WriteClassName) && preWriter instanceof JavaBeanSerializer) { Type valueType = null; if (fieldType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) fieldType; Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (actualTypeArguments.length == 2) { valueType = actualTypeArguments[1]; } }
JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) preWriter; javaBeanSerializer.writeNoneASM(serializer, value, entryKey, valueType, features); } else { preWriter.write(serializer, value, entryKey, null, features); } } } finally { serializer.context = parent; }
serializer.decrementIdent(); if (out.isEnabled(SerializerFeature.PrettyFormat) && map.size() > 0) { serializer.println(); }
if (!unwrapped) { out.write('}'); } }
|