Changeset 8344 in josm for trunk/src/org/openstreetmap/josm/data/Preferences.java
- Timestamp:
- 2015-05-10T13:27:54+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r8339 r8344 14 14 import java.io.PrintWriter; 15 15 import java.io.Reader; 16 import java.io.StringReader; 17 import java.io.StringWriter; 16 18 import java.lang.annotation.Retention; 17 19 import java.lang.annotation.RetentionPolicy; … … 22 24 import java.util.Collection; 23 25 import java.util.Collections; 26 import java.util.HashMap; 24 27 import java.util.HashSet; 25 28 import java.util.Iterator; … … 38 41 import java.util.regex.Pattern; 39 42 43 import javax.json.Json; 44 import javax.json.JsonObject; 45 import javax.json.JsonObjectBuilder; 46 import javax.json.JsonReader; 47 import javax.json.JsonValue; 48 import javax.json.JsonWriter; 40 49 import javax.swing.JOptionPane; 41 50 import javax.swing.UIManager; … … 1266 1275 } 1267 1276 1277 @SuppressWarnings("rawtypes") 1278 private static String mapToJson(Map map) { 1279 StringWriter stringWriter = new StringWriter(); 1280 try (JsonWriter writer = Json.createWriter(stringWriter)) { 1281 JsonObjectBuilder object = Json.createObjectBuilder(); 1282 for(Object o: map.entrySet()) { 1283 Entry e = (Entry) o; 1284 object.add(e.getKey().toString(), e.getValue().toString()); 1285 } 1286 writer.writeObject(object.build()); 1287 } 1288 return stringWriter.toString(); 1289 } 1290 1291 @SuppressWarnings({ "rawtypes", "unchecked" }) 1292 private static Map mapFromJson(String s) { 1293 Map ret = null; 1294 try (JsonReader reader = Json.createReader(new StringReader(s))) { 1295 JsonObject object = reader.readObject(); 1296 ret = new HashMap(object.size()); 1297 for (Entry<String, JsonValue> e: object.entrySet()) { 1298 ret.put(e.getKey(), e.getValue().toString()); 1299 } 1300 } 1301 return ret; 1302 } 1303 1268 1304 public static <T> Map<String,String> serializeStruct(T struct, Class<T> klass) { 1269 1305 T structPrototype; … … 1285 1321 if (fieldValue != null) { 1286 1322 if (f.getAnnotation(writeExplicitly.class) != null || !Objects.equals(fieldValue, defaultFieldValue)) { 1287 hash.put(f.getName().replace("_", "-"), fieldValue.toString()); 1323 String key = f.getName().replace("_", "-"); 1324 if (fieldValue instanceof Map) { 1325 hash.put(key, mapToJson((Map) fieldValue)); 1326 } else { 1327 hash.put(key, fieldValue.toString()); 1328 } 1288 1329 } 1289 1330 } … … 1332 1373 } else if (f.getType() == String.class) { 1333 1374 value = key_value.getValue(); 1334 } else 1375 } else if (f.getType().isAssignableFrom(Map.class)) { 1376 value = mapFromJson(key_value.getValue()); 1377 } 1378 else 1335 1379 throw new RuntimeException("unsupported preference primitive type"); 1336 1380
Note:
See TracChangeset
for help on using the changeset viewer.