@Test public void testJavaToTextConversion() { String string = "I've seen things you people wouldn't believe."; Double number = 12.1d; Calendar complex = Calendar.getInstance(); MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); byte[] result1 = StandardConversions.convertJavaToText(string, stringType, TEXT_PLAIN.withCharset(UTF_16BE)); assertArrayEquals(string.getBytes(UTF_16BE), result1); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); byte[] result2 = StandardConversions.convertJavaToText(number, doubleType, TEXT_PLAIN.withCharset(US_ASCII)); assertArrayEquals("12.1".getBytes(US_ASCII), result2); MediaType customType = APPLICATION_OBJECT.withParameter("type", complex.getClass().getName()); byte[] bytes = StandardConversions.convertJavaToText(complex, customType, TEXT_PLAIN.withCharset(US_ASCII)); assertEquals(complex.toString(), new String(bytes)); }
private Object convertToTextPlain(Object content, MediaType contentType, MediaType destinationType) { if (contentType.match(APPLICATION_UNKNOWN)) { try { return StandardConversions.convertJavaToOctetStream(content, APPLICATION_OBJECT, javaMarshaller); } catch (IOException | InterruptedException e) { throw log.unsupportedContent(content); } } if (contentType.match(APPLICATION_OCTET_STREAM)) { byte[] decoded = StandardConversions.decodeOctetStream(content, destinationType); return StandardConversions.convertOctetStreamToText(decoded, destinationType); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.convertJavaToText(content, contentType, destinationType); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToText(content, contentType, destinationType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToText(content, destinationType); } throw log.unsupportedContent(content); }