Here is the minimum full code I tried :
import java.util.Map;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
public class LoadObservable {
public static class MapStorage {
private ObservableMap<String, String> map = FXCollections.observableHashMap();
public void setMap(Map<String, String> newmap) {
map.clear();
map.putAll(newmap);
}
/**
* fails
*/
public ObservableMap<String, String> getMap() {
return map;
}
/**
* pass
*/
public Map<String, String> getMap() {
return map;
}
}
@Test
public void load() {
MapStorage storage = new MapStorage();
storage.map.put("nice", "dog");
String dumped = new Yaml().dump(storage);
MapStorage loaded = new Yaml().loadAs(dumped, MapStorage.class);
Assert.assertEquals(loaded.map.get("nice"), "dog", "dumped is " + dumped);
}
}
At first I tried with only a public field, but then snakeyaml writes it as a LinkedHashMap and can't cast it to an ObservableMap. Then I remembered snakeyaml uses getters/setters and tried to used them.
The code that pass presents the ObservableMap as a Map in the getter, and uses a Map in the setters to set the values of the ObservableMap. However since it does not present the ObservableMap to the user, it is useless to keep an observableMap. I sure could add another getter that returns the real type, however since I have tens of fields in my real cases this would be very bad for the user.
What I would like to, is to use the code that at the moment fails. Any idea ?