Kripton: how to manage charset problems in Retrofit client
When you work with Retrofit and web services, you are probably (I hope so) working with UTF-8. Just remember that JSON RFC specifies to with it. In somecase I had to manage incorrect situation without change the backend behaviour (the server produce JSON is no UTF-8).
Jackson library in its default behaviour throw an exception. To control this, since kripton 7-rc.9, it is possible to specify the error action to manage charset errors and what are the expected charset.
An example:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.addConverterFactory(KriptonBinderConverterFactory.Builder.create()
.setDefaultCharset(StandardCharsets.UTF_8)
.setErrorAction(CodingErrorAction.IGNORE)
.build())
.build();
With setDefaultCharset
you can specify the charset used to decode incoming stream (response body) and outcoming stream (request body). With setErrorAction
you specify how to manage unexpected char. The default value for error action is IGNORE (the wrong char is deleted, without exception).
Some links
- https://stackoverflow.com/questions/583562/json-character-encoding-is-utf-8-well-supported-by-browsers-or-should-i-use-nu
- https://tools.ietf.org/html/rfc7159