square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.12k stars 7.3k forks source link

Support CBOR in JacksonConverterFactory #3403

Closed robertvazan closed 9 months ago

robertvazan commented 4 years ago

I am using CBOR for request/response encoding. I am creating Jackson ObjectMapper with CBORFactory like this:

ObjectMapper mapper = new ObjectMapper(new CBORFactory())
    .setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

I then tried to use the existing JacksonConverterFactory like this:

Retrofit retrofit = new Retrofit.Builder()
    .client(client)
    .baseUrl("http://" + host + ":" + port + "/")
    .addConverterFactory(JacksonConverterFactory.create(mapper))
    .build();

But this failed with an exception, because JacksonConverterFactory insists on reading/writing documents as text while CBOR requires binary I/O.

I have implemented custom converter as a workaround. It would be nice to have CBOR supported simply by passing in custom ObjectMapper. Switching to binary I/O should have no negative impact on JSON serialization AFAIK.

ctlove0523 commented 4 years ago

Now standard Jackson binary format backends contains avro, cbor, protobuf and smile,the JacksonConverterFactory can switch to binary format when the ObjectMapper support binary format data.please make a pull request.

robertvazan commented 4 years ago

Eclipse didn't like the project for some reason. I don't have time to investigate it right now. Anyone with working build/tests should have easy time implementing this.

In JacksonResponseBodyConverter, replace value.charStream() with value.byteStream().

In JacksonRequestBodyConverter, turn MEDIA_TYPE into configurable parameter (passed from JacksonConverterFactory).

And perhaps add a test case in JacksonConverterFactoryTest.