Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUGGEST] Please make support for Retrofit library by making this as jackson-jr Retrofit converter #30

Closed
GulajavaMinistudio opened this issue Nov 14, 2015 · 7 comments
Milestone

Comments

@GulajavaMinistudio
Copy link

I make custom converter factory for Jackson-jr parser.
Jackson-jr itself is good for mobile devices due to resource requirements during startup and usage is quite low.
https://github.com/FasterXML/jackson-jr

And after a long experiment, Converter Factory for Jackson-jr was successfully run.
I use generic class or object T that needed for Jackson-jr for JSON Parsing.

Here's the snippet

public class JacksonJrConverters<T> extends Converter.Factory {

    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
    protected static final String PROTOCOL_CHARSET = "utf-8";

    private Class<T> clazz;

    public JacksonJrConverters(Class<T> tClass) {
        super();
        this.clazz = tClass;
    }


    @Override
    public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
        super.fromResponseBody(type, annotations);

        if (clazz.equals(type)) {

            return new Converter<ResponseBody, T>() {
                @Override
                public T convert(ResponseBody value) throws IOException {

                    return JSON.std.beanFrom(clazz, value.bytes());
                }
            };
        }

        return null;
    }

    @Override
    public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
        super.toRequestBody(type, annotations);

        if (clazz.equals(type)) {
            return new Converter<T, RequestBody>() {
                @Override
                public RequestBody convert(T value) throws IOException {

                    String strs = JSON.std.asString(value);

                    return RequestBody.create(MEDIA_TYPE, strs.getBytes(PROTOCOL_CHARSET));
                }
            };
        }
        return null;
    }
}

Or you can look it in here :
https://gist.github.com/GulajavaMinistudio/67959246f6d94e373d4c

And for Rest Client instance :

public class RestClientsJackson<T> {

    //use class T as generic for Jackson Jr parsing reference
    private Class<T> mClass;
    private OkHttpClient mOkHttpClient;
    public static final String ALAMAT_SERVERS = "https://api.github.com";


    public RestClientsJackson(Class<T> aClass) {
        mClass = aClass;

        mOkHttpClient = new OkHttpClient();
        mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setWriteTimeout(15, TimeUnit.SECONDS);
        mOkHttpClient.setReadTimeout(20, TimeUnit.SECONDS);
    }

    //with jackson jr converter
    public Apis getApiServicesJackson() {

        Log.w("API NULL", "API NULL, INIT LAGI");

        Retrofit.Builder retrobuilder = new Retrofit.Builder();
        retrobuilder.baseUrl(ALAMAT_SERVERS);
        retrobuilder.client(mOkHttpClient);

        retrobuilder.addConverterFactory(new JacksonJrConverters<>(mClass));

        Retrofit retrofits = retrobuilder.build();

        return retrofits.create(Apis.class);
    }
}

Or you can see it here
https://gist.github.com/GulajavaMinistudio/b853e01ee2ef4fb2213a

If you want to testing, i have made sample project for how to usage it :
https://github.com/GulajavaMinistudio/BelajarRetrofitJacksonJr

Hope it can help for someone that need Jackson-jr converter in Retrofit 2.

@cowtowncoder
Copy link
Member

Thank you for contributing this!

I assume this is Retrofit: https://github.com/square/retrofit
(I don't work on Android so while I have heard of it, haven't used it -- looks useful)

I wonder what would be the best way to share this. I guess one possibility would be to add a new Maven sub-module, to produce different jar?

@cowtowncoder
Copy link
Member

@GulajavaMinistudio quick question: it looks like Retrofit 2:

http://square.github.io/retrofit/

already includes converters (see section CONVERTERS) for a few other libraries. Do you think that might be a better home for this extension? I can point Retrofit developers to this issue, to see if they would like to include it, with proper attribution (since you wrote it, give you the credit etc).

@GulajavaMinistudio
Copy link
Author

@cowtowncoder : Yup, that's correct, there is for Retrofit 2. One of networking librray for Android and Java.
http://square.github.io/retrofit/

We make those sample of Jackson-jr converter because some people asking it how to use with converter.
square/retrofit#1035
square/retrofit#1241

And maybe someone can testing jackson-jr converter or maybe improved it (if there's error or fix, althought we have use those jackson jr for days)

Maybe, it's better a some wiki or home for this converter, coz many android apps use retrofit for networking library.

@cowtowncoder
Copy link
Member

@GulajavaMinistudio I think it's a good idea to just provide this as a small library. I reached Retrofit authors, and it sounded like they might prefer not including this (although they do include couple ones already). So when I get a chance, I could just create a new sub-project (that is, sub-maven project) for this project here, and then library can be used via Maven.

@GulajavaMinistudio
Copy link
Author

Alright then, is up to you if you want make it as library or extension. hope this is helpful for someone that needed it.

@GulajavaMinistudio
Copy link
Author

Here the fixed version. We also included for parsing JSON if there is JSON Array form .
ie :
[{....},{....},{.....}]

For Converter.Factory class
https://gist.github.com/GulajavaMinistudio/f0d0a62d8d5d08a3d6c7

For JacksonJrResponseConverter class
https://gist.github.com/GulajavaMinistudio/39dc9a6c9c2a382043eb

For JacksonJrResponseArrayConverter class
https://gist.github.com/GulajavaMinistudio/22ee0e12bc0efa68f2cf

For JacksonJrRequestBodyConverter
https://gist.github.com/GulajavaMinistudio/4e9e96c3b90686cc943a

For complete project :
https://github.com/GulajavaMinistudio/BelajarRetrofitJacksonJr

Hope it will help

@cowtowncoder cowtowncoder added this to the 2.7.0-rc2 milestone Dec 2, 2015
@cowtowncoder
Copy link
Member

@GulajavaMinistudio I finally had time to get this in. I made minor changes, mostly to allow passing specifically configured JSON instance (instead of relying on JSON.std). Hope I did not break anything.

Thank you once again for this contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants