Datetime:2016-08-23 03:10:45         Topic: Java          Share        Original >>
Here to See The Original Article!!!

Jolyglot allows to convert objects to and from Json without depending on any concrete implementation. Thus, you can happy code against this polyglot abstraction , and let the clients of your library choose whatever json provider which better suits their needs.

Available Json providers:

If you need another json provider, feel free to open an issue to address it.


Add JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        maven { url '' }

And add Jolyglot api module in the build.gradle of your library module:

dependencies {
    compile 'com.github.VictorAlbertos.Jolyglot:api:0.0.1'

Ask to the clients of your library to add one of the next json providers:

dependencies {
    compile 'com.github.VictorAlbertos.Jolyglot:gson:0.0.1'
    compile 'com.github.VictorAlbertos.Jolyglot:jackson:0.0.1'
    compile 'com.github.VictorAlbertos.Jolyglot:moshi:0.0.1'


Instantiate Jolyglot.

Ask to the client of your library for a concrete implementation of Jolyglot .

Depending on the provider chosen by your client, the instance of Jolyglot will be created in one of the next ways:

Jolyglot jolyglot = new GsonSpeaker()
Jolyglot jolyglot = new GsonSpeaker(gson) //overloaded constructor to customize the gson object.

Jolyglot jolyglot = new JacksonSpeaker()
Jolyglot jolyglot = new JacksonSpeaker(objectMapper) //overloaded constructor to customize the objectMapper object.

Jolyglot jolyglot = new MoshiSpeaker()
Jolyglot jolyglot = new MoshiSpeaker(moshi) //overloaded constructor to customize the moshi object.

Object to json.


Parameterized object to json.

Type type = jolyglot.newParameterizedType(YourParameterized.class, YourEnclosing.class);
jolyglot.toJson(parameterizedObject, type);

Json to object.

String json = "";
jolyglot.fromJson(json, Your.class);

Json to parameterized object.

String json = "";
Type type = jolyglot.newParameterizedType(YourParameterized.class, YourEnclosing.class);
jolyglot.fromJson(json, type);

For a complete example go here .

Be aware that the idiosyncrasy of every underlying json provider still remains. Jolyglot is only an abstraction layer to honor this diversity, but, in the end, every java class serialized/deserialized neeeds to fulfill the requirements of every json provider.


Put your ads here, just $200 per month.