Skip to content

Commit a9cb5c9

Browse files
committed
#359 Updated Convert.ToBytes(Object) to be null-friendly
1 parent ac3d69b commit a9cb5c9

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

javalite-common/src/main/java/org/javalite/common/Convert.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ private Convert() {
6262
* @return string representation of an object, including {@link java.sql.Clob}.
6363
*/
6464
public static String toString(Object value) {
65-
if(value == null) {
65+
if (value == null) {
6666
return null;
67-
} else if(value instanceof Clob) {
67+
} else if (value instanceof Clob) {
6868
return clobToString((Clob) value);
6969
} else {
7070
return value.toString();
@@ -403,10 +403,14 @@ public static BigDecimal toBigDecimal(Object value){
403403
* @return value converted to byte array.
404404
*/
405405
public static byte[] toBytes(Object value) {
406-
if (value instanceof Blob) {
406+
if (value == null) {
407+
return null;
408+
} else if (value instanceof byte[]) {
409+
return (byte[]) value;
410+
} else if (value instanceof Blob) {
407411
return toBytes((Blob) value);
408412
} else {
409-
return value instanceof byte[] ? (byte[]) value : toString(value).getBytes();
413+
return toString(value).getBytes();
410414
}
411415
}
412416

javalite-common/src/test/java/org/javalite/common/ConvertTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,9 @@ public void shouldCovertToIsoString() {
310310
cal.set(2014, 11, 31, 23, 59, 59);
311311
the(Convert.toIsoString(cal.getTime())).shouldBeEqual("2014-12-31T23:59:59Z");
312312
}
313+
314+
@Test
315+
public void shouldToBytesCovertNull() {
316+
the(Convert.toBytes((Object) null)).shouldBeNull();
317+
}
313318
}

0 commit comments

Comments
 (0)