Giter Club home page Giter Club logo

jolyglot's Introduction

Android Arsenal

Jolyglot

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

Available json providers supporting generics

Setup

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

allprojects {
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

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

dependencies {
    compile 'com.github.VictorAlbertos.Jolyglot:api:0.0.6'
}

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

dependencies {
    implementation 'com.github.VictorAlbertos.Jolyglot:gson:0.0.6'
    implementation 'com.github.VictorAlbertos.Jolyglot:jackson:0.0.6'
    implementation 'com.github.VictorAlbertos.Jolyglot:moshi:0.0.6'
    implementation 'com.github.VictorAlbertos.Jolyglot:fastjson:0.0.6'
}

Usage

Instantiate Jolyglot.

Ask to the client of your library for an implementation of Jolyglot if your library doesn't need to deal with generics. Otherwise, ask for an instance of JolyglotGenerics.

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.

Jolyglot jolyglot = new FastJsonSpeaker()

Object to json.

jolyglot.toJson(object);

Json to object.

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

Generic object to json.

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

Json to generic 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 needs to fulfill the requirements of every json provider.

jolyglot's People

Contributors

rolf-smit avatar victoralbertos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jolyglot's Issues

Segregate generics into another interface.

Currently, both generic and non generic objects binding operations are defined in the same interface.

Split them in order to provide a more granular selecction and support another json providers which only could work with non generic objects.

Plus, in case generics are not required and the user of the Jolyglot does not want to use any of the built-in json providers, the complexity of a manual implementation of Jolyglot would be far less.

MoshiSpeaker does not close the `BufferedSource` in `fromJson`

The MoshiSpeaker implementation of JolyglotGenerics does not close the BufferedSource object, this is not a breaking problem on some Linux based distributions but can lead to breaking problems on Windows based machines.

But anyway, this is bad behavior and should be fixed, isn't it 😉?

The GsonSpeaker doesn't seem to have this problem.

GsonSpeaker and FastJsonSpeaker

GsonSpeaker implements JolyglotGenerics,but,FastJsonSpeaker implements Jolyglot.
RxCache.Builder() .persistence() not do FastJsonSpeaker ?

Version 0.0.5 Not Available on Jitpack

I'm trying to upgrade from 0.0.3 to 0.0.5 but Gradle is unable to resolve the library, as if this version is not available on Jitpack. I get the following error when trying to synce Gradle Failed to resolve: com.github.VictorAlbertos.Jolyglot:jackson:0.0.5

Fastjson Support Jolyglotgenerics

I use this class in Kotlin because Newparameterizedtype method error, not in Rxcache normal use, please use Java code to see if the normal use

implementation "com.alibaba:fastjson:1.1.64.android"

public class FastJsonSpeaker implements JolyglotGenerics {

    @Override
    public String toJson(Object src, Type typeOfSrc) {
        return JSON.toJSONString(src);
    }

    @Override
    public <T> T fromJson(String json, Type type) throws RuntimeException {
        return JSON.parseObject(json, type);
    }

    @Override
    public <T> T fromJson(File file, Type typeOfT) throws RuntimeException {
        String json = readFile2String(file);
        return JSON.parseObject(json, typeOfT);
    }

    @Override
    public GenericArrayType arrayOf(Type componentType) {
        return Types.arrayOf(componentType);
    }

    @Override
    public ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) {
        return Types.newParameterizedType(rawType, typeArguments);
    }

    @Override
    public String toJson(Object src) {
        return JSON.toJSONString(src);
    }

    @Override
    public <T> T fromJson(String json, Class<T> classOfT) throws RuntimeException {
        return JSON.parseObject(json, classOfT);
    }

    @Override
    public <T> T fromJson(File file, Class<T> classOfT) throws RuntimeException {
        String json = readFile2String(file);
        return JSON.parseObject(json, classOfT);
    }

    private static String readFile2String(File file) {
        if (file == null || !file.exists()) {
            return null;
        }
        BufferedReader reader = null;
        try {
            StringBuilder sb = new StringBuilder();
            if (StringUtils.isSpace("UTF-8")) {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            } else {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            }
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return  sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Transitive dependencies

With update from 0.0.3 to 0.0.5 and even 0.0.6 version there are missing transitive dependencies that we are expecting to be there like the Types class from common module and in my case the jackson library (com.fasterxml.jackson package) is missing. Is this expected behavior?

  • if no I think the problem is in the build.gradle script instead of using implementation it should be api (the replacement of compile) and the script for building the lib should include all the dependencies
  • if yes how to resolve this issue and another questions is if I'm using different version of the jackson library will Jolyglot library work?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.