From 5317a7ff963984673bcdb0b5973cacf768bca807 Mon Sep 17 00:00:00 2001 From: prabhat Date: Sun, 9 Nov 2014 21:59:52 +0530 Subject: [PATCH] parsing primitive values in a try/catch block --- .../primitive/ParsePrimitiveUtils.java | 74 ++++++++++++------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/json-serde/src/main/java/org/openx/data/jsonserde/objectinspector/primitive/ParsePrimitiveUtils.java b/json-serde/src/main/java/org/openx/data/jsonserde/objectinspector/primitive/ParsePrimitiveUtils.java index 5ffecf31..4239b84c 100644 --- a/json-serde/src/main/java/org/openx/data/jsonserde/objectinspector/primitive/ParsePrimitiveUtils.java +++ b/json-serde/src/main/java/org/openx/data/jsonserde/objectinspector/primitive/ParsePrimitiveUtils.java @@ -19,50 +19,70 @@ public static boolean isHex(String s) { } public static byte parseByte(String s) { - if (isHex(s)) { - return Byte.parseByte(s.substring(2), 16); - } else { - return Byte.parseByte(s); + try { + if (isHex(s)) { + return Byte.parseByte(s.substring(2), 16); + } else { + return Byte.parseByte(s); + } + }catch (Exception e){ + return 0; } } public static int parseInt(String s) { - if (isHex(s)) { - return Integer.parseInt(s.substring(2), 16); - } else { - return Integer.parseInt(s); + try { + if (isHex(s)) { + return Integer.parseInt(s.substring(2), 16); + } else { + return Integer.parseInt(s); + } + }catch (Exception e){ + return 0; } } public static short parseShort(String s) { - if (isHex(s)) { - return Short.parseShort(s.substring(2), 16); - } else { - return Short.parseShort(s); + try { + if (isHex(s)) { + return Short.parseShort(s.substring(2), 16); + } else { + return Short.parseShort(s); + } + }catch (Exception e){ + return 0; } } public static long parseLong(String s) { - if (isHex(s)) { - return Long.parseLong(s.substring(2), 16); - } else { - return Long.parseLong(s); + try { + if (isHex(s)) { + return Long.parseLong(s.substring(2), 16); + } else { + return Long.parseLong(s); + } + }catch (Exception e){ + return 0; } } public static Timestamp parseTimestamp(String s) { - Timestamp value; - if (s.indexOf(':') > 0) { - value = Timestamp.valueOf(s); - } else if (s.indexOf('.') >= 0) { - // it's a float - value = new Timestamp( - (long) ((double) (Double.parseDouble(s) * 1000))); - } else { - // integer - value = new Timestamp(Long.parseLong(s) * 1000); + try { + Timestamp value; + if (s.indexOf(':') > 0) { + value = Timestamp.valueOf(s); + } else if (s.indexOf('.') >= 0) { + // it's a float + value = new Timestamp( + (long) ((double) (Double.parseDouble(s) * 1000))); + } else { + // integer + value = new Timestamp(Long.parseLong(s) * 1000); + } + return value; + }catch (Exception e){ + return null; } - return value; } }