For a while now I have been toying with the idea of using source code as a file storage format.

*My question: How to support format version changes?*

An example would be a settings file.

JSON:

```
{
 "version": 1,
 "email": "[email protected]"
}
```

Then there is typically a manual or automatic parsing/deserialization/binding step that maps the textual information to a typed model in memory, e.g. an object of type `Settings`:


```
public class Settings {
 public String email;
}
```

Using source code as storage format, the settings file would look like this:

```
public class MySettingsFile1 {
 public static final int VERSION = 1; 
 public static Settings load() {
 Settings s = new Settings();
 s.email = "[email protected]";
 return s;
 }
}
```

The settings file could then be loaded by leveraging the class loading mechanism:

```
String className = "MySettingsFile1";
String source = ... (read settings file to string)
Class<?> clazz = new CustomClassLoader().compileAndLoad(className, source);
Settings settings = (Settings) clazz.getMethod("load").invoke(null);
System.out.println(settings.email);
```

I already know out how to [load a class from a String][1].

Pros
----
- no parsing / binding code required
- text format (human readable)
- concise (verbosity comparable to JSON)
- supports comments

Cons
----
- how to support different versions and upgrade from older versions?
- security (sandbox the loading code?)

Edit: The real use case I have in mind are files that are only modified by a custom GUI editor, and then compiled and packaged with the application (no security concerns here). These files are already under version control (XML currently).


 [1]: https://stackoverflow.com/questions/10882952/java-load-class-from-string/72011705#72011705