@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)); }
public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType) { if (source == null) return null; if (sourceMediaType == null || destinationMediaType == null) { throw new NullPointerException("sourceMediaType and destinationMediaType cannot be null!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) { return convertCharset(source, StandardCharsets.UTF_8, destinationMediaType.getCharset()); } else { String asString = decoded.toString(); return asString.getBytes(destinationMediaType.getCharset()); } }
StandardConversions { public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType) { if (source == null) return null; if (sourceMediaType == null || destinationMediaType == null) { throw new NullPointerException("sourceMediaType and destinationMediaType cannot be null!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) { return convertCharset(source, StandardCharsets.UTF_8, destinationMediaType.getCharset()); } else { String asString = decoded.toString(); return asString.getBytes(destinationMediaType.getCharset()); } } }
StandardConversions { public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType) { if (source == null) return null; if (sourceMediaType == null || destinationMediaType == null) { throw new NullPointerException("sourceMediaType and destinationMediaType cannot be null!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) { return convertCharset(source, StandardCharsets.UTF_8, destinationMediaType.getCharset()); } else { String asString = decoded.toString(); return asString.getBytes(destinationMediaType.getCharset()); } }  }
StandardConversions { public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType) { if (source == null) return null; if (sourceMediaType == null || destinationMediaType == null) { throw new NullPointerException("sourceMediaType and destinationMediaType cannot be null!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) { return convertCharset(source, StandardCharsets.UTF_8, destinationMediaType.getCharset()); } else { String asString = decoded.toString(); return asString.getBytes(destinationMediaType.getCharset()); } }  static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType); static byte[] convertTextToOctetStream(Object source, MediaType sourceType); static String convertTextToObject(Object source, MediaType sourceType); static String convertTextToUrlEncoded(Object source, MediaType sourceType); static byte[] convertOctetStreamToText(byte[] source, MediaType destination); static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller); static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller); static byte[] convertJavaToProtoStream(Object source, MediaType sourceMediaType, ImmutableSerializationContext ctx); static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType); static Object decodeObjectContent(Object content, MediaType contentMediaType); static byte[] convertCharset(Object content, Charset fromCharset, Charset toCharset); static byte[] decodeOctetStream(Object input, MediaType octetStream); static String bytesToHex(byte[] bytes); static byte[] hexToBytes(String hex); static Object convertUrlEncodedToObject(Object content); static Object convertUrlEncodedToText(Object content, MediaType destinationType); static Object convertUrlEncodedToOctetStream(Object content); static String urlEncode(Object content, MediaType mediaType); static Object urlDecode(Object content); static Object convertOctetStreamToUrlEncoded(Object content, MediaType contentType); }
StandardConversions { public static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType) { if (source == null) return null; if (sourceMediaType == null || destinationMediaType == null) { throw new NullPointerException("sourceMediaType and destinationMediaType cannot be null!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) { return convertCharset(source, StandardCharsets.UTF_8, destinationMediaType.getCharset()); } else { String asString = decoded.toString(); return asString.getBytes(destinationMediaType.getCharset()); } }  static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType); static byte[] convertTextToOctetStream(Object source, MediaType sourceType); static String convertTextToObject(Object source, MediaType sourceType); static String convertTextToUrlEncoded(Object source, MediaType sourceType); static byte[] convertOctetStreamToText(byte[] source, MediaType destination); static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller); static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller); static byte[] convertJavaToProtoStream(Object source, MediaType sourceMediaType, ImmutableSerializationContext ctx); static byte[] convertJavaToText(Object source, MediaType sourceMediaType, MediaType destinationMediaType); static Object decodeObjectContent(Object content, MediaType contentMediaType); static byte[] convertCharset(Object content, Charset fromCharset, Charset toCharset); static byte[] decodeOctetStream(Object input, MediaType octetStream); static String bytesToHex(byte[] bytes); static byte[] hexToBytes(String hex); static Object convertUrlEncodedToObject(Object content); static Object convertUrlEncodedToText(Object content, MediaType destinationType); static Object convertUrlEncodedToOctetStream(Object content); static String urlEncode(Object content, MediaType mediaType); static Object urlDecode(Object content); static Object convertOctetStreamToUrlEncoded(Object content, MediaType contentType);  }