@Test public void testEmptyContent() { byte[] empty = Util.EMPTY_BYTE_ARRAY; assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_JSON, APPLICATION_JSON.withCharset(US_ASCII))); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_UNKNOWN, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_OCTET_STREAM, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, TEXT_PLAIN, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_OBJECT, APPLICATION_JSON)); }
@Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { if (destinationType.match(APPLICATION_OCTET_STREAM) || destinationType.match(APPLICATION_UNKNOWN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } boolean outputString = destinationType.hasStringType(); Charset contentCharset = contentType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (destinationType.match(APPLICATION_JSON)) { if (contentType.match(APPLICATION_JSON)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } try { if (content instanceof String || content instanceof byte[]) { return convertTextToJson(content, contentCharset, destinationCharset, outputString); } logger.jsonObjectConversionDeprecated(); if (outputString) { return objectMapper.writeValueAsString(content); } try (ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out, destinationCharset)) { objectMapper.writeValue(osw, content); return out.toByteArray(); } } catch (IOException e) { throw logger.cannotConvertContent(content, contentType, destinationType); } } if (destinationType.match(APPLICATION_OBJECT)) { logger.jsonObjectConversionDeprecated(); try { String destinationClassName = destinationType.getClassType(); Class<?> destinationClass = Object.class; if (destinationClassName != null) destinationClass = Class.forName(destinationClassName); if (content instanceof byte[]) { return objectMapper.readValue((byte[]) content, destinationClass); } return objectMapper.readValue((String) content, destinationClass); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } if (destinationType.match(TEXT_PLAIN)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } throw logger.unsupportedContent(JsonTranscoder.class.getSimpleName(), content); }
JsonTranscoder extends OneToManyTranscoder { @Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { if (destinationType.match(APPLICATION_OCTET_STREAM) || destinationType.match(APPLICATION_UNKNOWN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } boolean outputString = destinationType.hasStringType(); Charset contentCharset = contentType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (destinationType.match(APPLICATION_JSON)) { if (contentType.match(APPLICATION_JSON)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } try { if (content instanceof String || content instanceof byte[]) { return convertTextToJson(content, contentCharset, destinationCharset, outputString); } logger.jsonObjectConversionDeprecated(); if (outputString) { return objectMapper.writeValueAsString(content); } try (ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out, destinationCharset)) { objectMapper.writeValue(osw, content); return out.toByteArray(); } } catch (IOException e) { throw logger.cannotConvertContent(content, contentType, destinationType); } } if (destinationType.match(APPLICATION_OBJECT)) { logger.jsonObjectConversionDeprecated(); try { String destinationClassName = destinationType.getClassType(); Class<?> destinationClass = Object.class; if (destinationClassName != null) destinationClass = Class.forName(destinationClassName); if (content instanceof byte[]) { return objectMapper.readValue((byte[]) content, destinationClass); } return objectMapper.readValue((String) content, destinationClass); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } if (destinationType.match(TEXT_PLAIN)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } throw logger.unsupportedContent(JsonTranscoder.class.getSimpleName(), content); } }
JsonTranscoder extends OneToManyTranscoder { @Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { if (destinationType.match(APPLICATION_OCTET_STREAM) || destinationType.match(APPLICATION_UNKNOWN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } boolean outputString = destinationType.hasStringType(); Charset contentCharset = contentType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (destinationType.match(APPLICATION_JSON)) { if (contentType.match(APPLICATION_JSON)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } try { if (content instanceof String || content instanceof byte[]) { return convertTextToJson(content, contentCharset, destinationCharset, outputString); } logger.jsonObjectConversionDeprecated(); if (outputString) { return objectMapper.writeValueAsString(content); } try (ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out, destinationCharset)) { objectMapper.writeValue(osw, content); return out.toByteArray(); } } catch (IOException e) { throw logger.cannotConvertContent(content, contentType, destinationType); } } if (destinationType.match(APPLICATION_OBJECT)) { logger.jsonObjectConversionDeprecated(); try { String destinationClassName = destinationType.getClassType(); Class<?> destinationClass = Object.class; if (destinationClassName != null) destinationClass = Class.forName(destinationClassName); if (content instanceof byte[]) { return objectMapper.readValue((byte[]) content, destinationClass); } return objectMapper.readValue((String) content, destinationClass); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } if (destinationType.match(TEXT_PLAIN)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } throw logger.unsupportedContent(JsonTranscoder.class.getSimpleName(), content); } JsonTranscoder();  JsonTranscoder(ClassAllowList allowList);  JsonTranscoder(ClassLoader classLoader, ClassAllowList allowList); }
JsonTranscoder extends OneToManyTranscoder { @Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { if (destinationType.match(APPLICATION_OCTET_STREAM) || destinationType.match(APPLICATION_UNKNOWN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } boolean outputString = destinationType.hasStringType(); Charset contentCharset = contentType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (destinationType.match(APPLICATION_JSON)) { if (contentType.match(APPLICATION_JSON)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } try { if (content instanceof String || content instanceof byte[]) { return convertTextToJson(content, contentCharset, destinationCharset, outputString); } logger.jsonObjectConversionDeprecated(); if (outputString) { return objectMapper.writeValueAsString(content); } try (ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out, destinationCharset)) { objectMapper.writeValue(osw, content); return out.toByteArray(); } } catch (IOException e) { throw logger.cannotConvertContent(content, contentType, destinationType); } } if (destinationType.match(APPLICATION_OBJECT)) { logger.jsonObjectConversionDeprecated(); try { String destinationClassName = destinationType.getClassType(); Class<?> destinationClass = Object.class; if (destinationClassName != null) destinationClass = Class.forName(destinationClassName); if (content instanceof byte[]) { return objectMapper.readValue((byte[]) content, destinationClass); } return objectMapper.readValue((String) content, destinationClass); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } if (destinationType.match(TEXT_PLAIN)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } throw logger.unsupportedContent(JsonTranscoder.class.getSimpleName(), content); } JsonTranscoder();  JsonTranscoder(ClassAllowList allowList);  JsonTranscoder(ClassLoader classLoader, ClassAllowList allowList); @Override Object transcode(Object content, MediaType contentType, MediaType destinationType); }
JsonTranscoder extends OneToManyTranscoder { @Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { if (destinationType.match(APPLICATION_OCTET_STREAM) || destinationType.match(APPLICATION_UNKNOWN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } boolean outputString = destinationType.hasStringType(); Charset contentCharset = contentType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (destinationType.match(APPLICATION_JSON)) { if (contentType.match(APPLICATION_JSON)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } try { if (content instanceof String || content instanceof byte[]) { return convertTextToJson(content, contentCharset, destinationCharset, outputString); } logger.jsonObjectConversionDeprecated(); if (outputString) { return objectMapper.writeValueAsString(content); } try (ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(out, destinationCharset)) { objectMapper.writeValue(osw, content); return out.toByteArray(); } } catch (IOException e) { throw logger.cannotConvertContent(content, contentType, destinationType); } } if (destinationType.match(APPLICATION_OBJECT)) { logger.jsonObjectConversionDeprecated(); try { String destinationClassName = destinationType.getClassType(); Class<?> destinationClass = Object.class; if (destinationClassName != null) destinationClass = Class.forName(destinationClassName); if (content instanceof byte[]) { return objectMapper.readValue((byte[]) content, destinationClass); } return objectMapper.readValue((String) content, destinationClass); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } if (destinationType.match(TEXT_PLAIN)) { return convertCharset(content, contentCharset, destinationCharset, outputString); } throw logger.unsupportedContent(JsonTranscoder.class.getSimpleName(), content); } JsonTranscoder();  JsonTranscoder(ClassAllowList allowList);  JsonTranscoder(ClassLoader classLoader, ClassAllowList allowList); @Override Object transcode(Object content, MediaType contentType, MediaType destinationType); static final String TYPE_PROPERTY; }