Hello @core-libs-dev <[email protected]>, *(For minor enhancement suggestions like this, should I make an entry on JBS, or should I talk about it here? Both? Neither? Please let me know.)*
I first discovered the java.prefs module a while ago, and it has been great. It fits that perfect middle ground between a full serialization framework and just writing stuff to a file. Ideal for things like user settings for desktop applications. However, the second I need to store anything other than the common primitive data types is when things get slightly annoying. I keep finding myself creating some sort of wrapper around Preferences to just give myself a way to "serialize" or "deserialize" data. For example, a common thing I want to do is store the user's last used location. Easy enough, I just store the java.nio.file.Path via toString(). Oh wait, I need to use Path.toAbsolutePath(). Now, I need to go find all the places in my code that update it and change them. It would be nice if there was a simple class added to java.prefs -- let's call it PreferencesTransformer<T>. It would be constructed from a Preferences object and a key, serving as both a serializer and deserializer. We'd need an unimplemented public String serialize(T object) and public T deserialize(String serializedTextFormOfObject). This would be especially nice because it is entirely opt-in -- the Preferences class doesn't have to change at all. Maybe add a public <T> PreferencesTransformer<T> transformer(String key, Function<String, T> deserializer, Function<T, String> serializer) method, just for convenience sake. But otherwise, unchanged. I think this is a clean enhancement that makes code safer and easier. Please let me know if this makes good sense to you all. Thank you for your time and consideration. David Alayachew
