Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-2171

JsonBindingProvider use incorrect charset encoding

    XMLWordPrintable

Details

    • Hide
      • Create a jax-rs application, ensure jsonb is used, define a resource returning a simple POJO containing some string with utf-8 (non-ascii) characters, annotate it with @Produces("application/json")
      • Start your application server with the system property file.encoding set to a non-utf8 charset (eg: us-ascii)
      • Issue a request, analyse the returned content bytes
      Show
      Create a jax-rs application, ensure jsonb is used, define a resource returning a simple POJO containing some string with utf-8 (non-ascii) characters, annotate it with @Produces("application/json") Start your application server with the system property file.encoding set to a non-utf8 charset (eg: us-ascii) Issue a request, analyse the returned content bytes
    • Compatibility/Configuration, User Experience
    • Workaround Exists
    • Hide

      Annotate your json resources with @Produces("application/json;charset=utf-8").

      Show
      Annotate your json resources with @Produces("application/json;charset=utf-8") .

    Description

      With a rest resource method returning a POJO and annotated with @Produces(Mediatype.APPLICATION_JSON), the returned response response is incorrectly encoded (uses US_ASCII instead of utf-8).

      The workaround is to add a parameter to the mediatype: @Produces("application/json;charset=utf-8").

      There is 2 issues concerning compliance with the RFC7159 json spec:

      • As per the json spec, charset parameters should be discarded:

        Note: No "charset" parameter is defined for this registration. Adding one really has no effect on compliant recipients.

      • As per the json spec, json is utf-8 encoded:

        JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. The default encoding is UTF-8, and JSON texts that are encoded in UTF-8 are interoperable in the sense that they will be read successfully by the maximum number of implementations

      The source of these issues lies in org.jboss.resteasy.plugins.providers.jsonb.AbstractJsonBindingProvider#getCharset, which attempts to figure out the charset to use, defaulting to Charset.defaultCharset(), whereas utf-8 should be used and this logic is not required:

       public static Charset getCharset(final MediaType mediaType) {
            if (mediaType != null)
            {
               String charset = mediaType.getParameters().get("charset");
               if (charset != null) return Charset.forName(charset);
            }
            return Charset.defaultCharset();
         }
      

      This method is called from org.jboss.resteasy.plugins.providers.jsonb.JsonBindingProvider#writeTo:

      entityStream.write(jsonb.toJson(t).getBytes(getCharset(mediaType)));
      

      Attachments

        Issue Links

          Activity

            People

              rhn-support-iweiss Ingo Weiss
              cghislai charles ghislain (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: