jsonbind - guice configured with json

A big part of my PhD project are simulations of fluid flow under various conditions. Often I am actually not sure what the correct setup is until the paper is written. You try one hypothesis, then another - only to modify it yet again. In this situation it comes in handy if your program allows you to swap certain parts of the program, for example the geometry, without influencing the other. If you can do this at runtime without recompiling the program - even better.

One particular way of structuring the code to reach this kind of flexibility is inversion of control. In practice this means, that the objects do not construct their dependencies explicitly, but get them assigned from outside - a process known as dependency injection. Since this style of coding generates a lot of boiler plate code, a number of frameworks have been developed to support this style of coding. For java google's guice is a common choice.

Unfortunately guice lacks a native mechansim of file based configuration. For XML there is guice-xml-config, however I always found XML rather verbose. For my projects I like to use JSON files as a lightweight alternative. I can easily read and write them from C++, python and java and they nicely map onto a hierarchical set of objects.

While working on my neural network project I have hacked to gather a jackson powered configuration scheme for guice. Since it ended up being quite generic, I've put it on github - hopefully it also works for other people. You can find it here.

To get a taste of how it works, consider the pizza website example from the guice guide. In the example we try to configure a the used transaction log and the credit card processor. For the transaction log we use a database which requires a database connection. You could configure this setup using the following JSON document

[{
    "@type": ".BindAll",
    "de.cprohm.jsonbind.ggtest.classes.TransactionLog": 
        "com.pizza.order.DatabaseTransactionLog",
    "com.pizza.order.CreditCardProcessor": 
        "com.pizza.order.PaypalCreditCardProcessor"
}, {
    "@type": ".BindProperties",
    "JDBC URL": "jdbc:mysql://localhost/pizza"
}]

For more examples and usage hints, checkout the project on github.