From c78ffaaa755107e100faccb817ccc952584c1b52 Mon Sep 17 00:00:00 2001 From: Ricardo Fradinho Date: Tue, 2 Jul 2013 12:08:35 +0100 Subject: [PATCH 1/4] Add exception handling for non-date inputs when parsing Dates. --- .../pig/eval/date/ConvertDateFormat.java | 5 ++-- .../com/mozilla/pig/eval/date/FormatDate.java | 15 ++++++++--- .../com/mozilla/pig/eval/date/ParseDate.java | 2 +- .../com/mozilla/pig/eval/date/TimeDelta.java | 26 +++++++++---------- .../mozilla/pig/eval/date/TimeDeltaTest.java | 23 ++++++++++++++++ 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/mozilla/pig/eval/date/ConvertDateFormat.java b/src/main/java/com/mozilla/pig/eval/date/ConvertDateFormat.java index 8105f52..739d71e 100644 --- a/src/main/java/com/mozilla/pig/eval/date/ConvertDateFormat.java +++ b/src/main/java/com/mozilla/pig/eval/date/ConvertDateFormat.java @@ -21,7 +21,6 @@ package com.mozilla.pig.eval.date; import java.io.IOException; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -50,8 +49,8 @@ public String exec(Tuple input) throws IOException { try { Date d = inputSdf.parse((String)input.get(0)); s = outputSdf.format(d); - } catch (ParseException e) { - pigLogger.warn(this, "Date parsing error", ERRORS.DateParseError); + } catch (Exception e) { + warn("Date parse error: " + e, ERRORS.DateParseError); } return s; diff --git a/src/main/java/com/mozilla/pig/eval/date/FormatDate.java b/src/main/java/com/mozilla/pig/eval/date/FormatDate.java index f67974e..047eb2d 100644 --- a/src/main/java/com/mozilla/pig/eval/date/FormatDate.java +++ b/src/main/java/com/mozilla/pig/eval/date/FormatDate.java @@ -23,10 +23,13 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; import org.apache.pig.EvalFunc; import org.apache.pig.data.Tuple; +import com.mozilla.pig.eval.date.ConvertDateFormat.ERRORS; + public class FormatDate extends EvalFunc { private Calendar cal = Calendar.getInstance(); @@ -41,10 +44,16 @@ public String exec(Tuple input) throws IOException { if (input == null || input.size() == 0 || input.get(0) == null) { return null; } - - cal.setTimeInMillis(((Number)input.get(0)).longValue()); + + String s = null; + try { + cal.setTimeInMillis(((Number)input.get(0)).longValue()); + s = sdf.format(cal.getTime()); + } catch (Exception e) { + warn("Date parse error: " + e, ERRORS.DateParseError); + } - return sdf.format(cal.getTime()); + return s; } } diff --git a/src/main/java/com/mozilla/pig/eval/date/ParseDate.java b/src/main/java/com/mozilla/pig/eval/date/ParseDate.java index d0cb162..c2388a7 100644 --- a/src/main/java/com/mozilla/pig/eval/date/ParseDate.java +++ b/src/main/java/com/mozilla/pig/eval/date/ParseDate.java @@ -49,7 +49,7 @@ public Long exec(Tuple input) throws IOException { Date d = sdf.parse((String)input.get(0)); t = d.getTime(); } catch (ParseException e) { - pigLogger.warn(this, "Date parsing error", ERRORS.DateParseError); + warn("Date parse error: " + e, ERRORS.DateParseError); } return t; diff --git a/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java b/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java index a8540fe..9a26adf 100644 --- a/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java +++ b/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java @@ -63,19 +63,19 @@ public Long exec(Tuple input) throws IOException { return null; } - long delta = 0; - if (parseDate) { - try { - Date d1 = sdf.parse((String)input.get(0)); - Date d2 = sdf.parse((String)input.get(1)); - delta = DateUtil.getTimeDelta(d1.getTime(), d2.getTime(), deltaUnit); - } catch (ParseException e) { - pigLogger.warn(this, "Date parse error", ERRORS.DateParseError); - } - } else { - long t1 = ((Number)input.get(0)).longValue(); - long t2 = ((Number)input.get(1)).longValue(); - delta = DateUtil.getTimeDelta(t1, t2, deltaUnit); + Long delta = null; + try { + if (parseDate) { + Date d1 = sdf.parse((String)input.get(0)); + Date d2 = sdf.parse((String)input.get(1)); + delta = DateUtil.getTimeDelta(d1.getTime(), d2.getTime(), deltaUnit); + } else { + long t1 = ((Number)input.get(0)).longValue(); + long t2 = ((Number)input.get(1)).longValue(); + delta = DateUtil.getTimeDelta(t1, t2, deltaUnit); + } + } catch (Exception e) { + warn("Date parse error: " + e, ERRORS.DateParseError); } return delta; diff --git a/src/test/java/com/mozilla/pig/eval/date/TimeDeltaTest.java b/src/test/java/com/mozilla/pig/eval/date/TimeDeltaTest.java index d647880..53ea7d6 100644 --- a/src/test/java/com/mozilla/pig/eval/date/TimeDeltaTest.java +++ b/src/test/java/com/mozilla/pig/eval/date/TimeDeltaTest.java @@ -83,4 +83,27 @@ public void testExec2() throws IOException, ParseException { assertEquals(10, (long)deltaDays); } + + @Test + public void testInvalidDateFormat() throws IOException, ParseException { + Tuple input = tupleFactory.newTuple(2); + input.set(0, "dummy1"); + input.set(1, "dummy2"); + + TimeDelta daysAgo = new TimeDelta("5", TIME_FORMAT); + Long deltaDays = daysAgo.exec(input); + assertNull(deltaDays); + } + + @Test + public void testInvalidDateType() throws IOException, ParseException { + Tuple input = tupleFactory.newTuple(2); + input.set(0, new Integer(0)); + input.set(1, new Float(0)); + + TimeDelta daysAgo = new TimeDelta("5", TIME_FORMAT); + Long deltaDays = daysAgo.exec(input); + assertNull(deltaDays); + } + } From b9bfa5c51c3b762bf0d84786dad559b7414f6402 Mon Sep 17 00:00:00 2001 From: Ricardo Fradinho Date: Thu, 4 Jul 2013 20:48:03 +0100 Subject: [PATCH 2/4] Add date parsing method to DateUtil that caches parsed dates. --- .../com/mozilla/pig/eval/date/TimeDelta.java | 9 +++-- src/main/java/com/mozilla/util/DateUtil.java | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java b/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java index 9a26adf..c8542e2 100644 --- a/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java +++ b/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -36,7 +35,7 @@ public static enum ERRORS { DateParseError }; private int deltaUnit; private boolean parseDate = false; - private SimpleDateFormat sdf; + private String dateFormat; public TimeDelta() { deltaUnit = Calendar.MILLISECOND; @@ -52,7 +51,7 @@ public TimeDelta(String deltaUnitStr, String dateFormat) throws ParseException { deltaUnit = Integer.parseInt(deltaUnitStr); if (dateFormat != null) { parseDate = true; - sdf = new SimpleDateFormat(dateFormat); + this.dateFormat = dateFormat; } } @@ -66,8 +65,8 @@ public Long exec(Tuple input) throws IOException { Long delta = null; try { if (parseDate) { - Date d1 = sdf.parse((String)input.get(0)); - Date d2 = sdf.parse((String)input.get(1)); + Date d1 = DateUtil.parseAndCacheDate(dateFormat, (String)input.get(0)); + Date d2 = DateUtil.parseAndCacheDate(dateFormat, (String)input.get(1)); delta = DateUtil.getTimeDelta(d1.getTime(), d2.getTime(), deltaUnit); } else { long t1 = ((Number)input.get(0)).longValue(); diff --git a/src/main/java/com/mozilla/util/DateUtil.java b/src/main/java/com/mozilla/util/DateUtil.java index dbfae4f..6e559d4 100644 --- a/src/main/java/com/mozilla/util/DateUtil.java +++ b/src/main/java/com/mozilla/util/DateUtil.java @@ -26,13 +26,46 @@ import static java.util.Calendar.SECOND; import static java.util.Calendar.WEEK_OF_YEAR; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; +import java.util.concurrent.ConcurrentHashMap; public class DateUtil { public static long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; public static long WEEK_IN_MILLIS = DAY_IN_MILLIS * 7; + private static ConcurrentHashMap> parsedDatesMapByFormatMap = new ConcurrentHashMap>(); + + /** + * Parse a date string according to the format and cache the result + * @param format + * @param str + * @throws ParseException + */ + public static Date parseAndCacheDate(String format, String str) throws ParseException { + if(format==null || str==null) { + return null; + } + + ConcurrentHashMap parsedDatesMap = parsedDatesMapByFormatMap.get(format); + if(parsedDatesMap == null) { + parsedDatesMap = new ConcurrentHashMap(); + parsedDatesMapByFormatMap.put(format, parsedDatesMap); + } + + Date result = parsedDatesMap.get(str); + if(result == null) { + SimpleDateFormat sdf = new SimpleDateFormat(format); + result = sdf.parse(str); + parsedDatesMap.put(str, result); + } + + return result; + } + /** * Get the first moment in time for the given time and resolution * @param time From ef5ce8be55c54b0681bf6c80d60696598bcbb165 Mon Sep 17 00:00:00 2001 From: Ricardo Fradinho Date: Thu, 4 Jul 2013 20:50:16 +0100 Subject: [PATCH 3/4] Add Jackson Deserializer that can output Maps using Pig datatypes, avoiding a 2nd step for type conversion. --- .../com/mozilla/pig/eval/json/JsonMap.java | 71 +++------------- .../mozilla/pig/eval/json/JsonTupleMap.java | 82 +++++++++---------- .../deserializers/JsonMapDeserializer.java | 59 +++++++++++++ .../deserializers/JsonTuppleDeserializer.java | 51 ++++++++++++ .../RegularJsonDeserializer.java | 80 ++++++++++++++++++ 5 files changed, 240 insertions(+), 103 deletions(-) create mode 100644 src/main/java/com/mozilla/pig/eval/json/deserializers/JsonMapDeserializer.java create mode 100644 src/main/java/com/mozilla/pig/eval/json/deserializers/JsonTuppleDeserializer.java create mode 100644 src/main/java/com/mozilla/pig/eval/json/deserializers/RegularJsonDeserializer.java diff --git a/src/main/java/com/mozilla/pig/eval/json/JsonMap.java b/src/main/java/com/mozilla/pig/eval/json/JsonMap.java index 74faa38..5921c28 100644 --- a/src/main/java/com/mozilla/pig/eval/json/JsonMap.java +++ b/src/main/java/com/mozilla/pig/eval/json/JsonMap.java @@ -21,20 +21,17 @@ import java.io.EOFException; import java.io.IOException; -import java.util.HashMap; -import java.util.List; import java.util.Map; import org.apache.pig.EvalFunc; -import org.apache.pig.data.BagFactory; -import org.apache.pig.data.DataBag; import org.apache.pig.data.Tuple; -import org.apache.pig.data.TupleFactory; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.mozilla.pig.eval.json.deserializers.JsonMapDeserializer; public class JsonMap extends EvalFunc> { @@ -42,59 +39,15 @@ public static enum ERRORS { JSONParseError, JSONMappingError, EOFError, GenericError }; - private static final BagFactory bagFactory = BagFactory.getInstance(); - protected static final TupleFactory tupleFactory = TupleFactory.getInstance(); - protected final ObjectMapper jsonMapper = new ObjectMapper(); - - /** - * Converts List objects to DataBag to keep Pig happy - * - * @param l - * @return - */ - @SuppressWarnings("unchecked") - private DataBag convertListToBag(List l) { - DataBag dbag = bagFactory.newDefaultBag(); - Tuple t = tupleFactory.newTuple(); - for (Object o : l) { - if (o instanceof List) { - dbag.addAll(convertListToBag((List) o)); - } else { - t.append(o); - } - } - - if (t.size() > 0) { - dbag.add(t); - } - - return dbag; + private static ObjectMapper jsonMapper = null; + + static { + jsonMapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("JsonMap"); + module.addDeserializer(Object.class, new JsonMapDeserializer()); + jsonMapper.registerModule(module); } - - /** - * Convert map and its values to types that Pig can handle - * - * @param m - * @return - */ - @SuppressWarnings("unchecked") - protected Map makeSafe(Map m) { - Map safeValues = new HashMap(); - for (Map.Entry entry : m.entrySet()) { - Object v = entry.getValue(); - if (v != null && v instanceof List) { - DataBag db = convertListToBag((List) v); - safeValues.put(entry.getKey(), db); - } else if (v != null && v instanceof Map) { - safeValues.put(entry.getKey(), makeSafe((Map) v)); - } else { - safeValues.put(entry.getKey(), entry.getValue()); - } - } - - return safeValues; - } - + @Override public Map exec(Tuple input) throws IOException { if (input == null || input.size() == 0) { @@ -102,9 +55,7 @@ public Map exec(Tuple input) throws IOException { } try { - Map values = jsonMapper.readValue((String) input.get(0), - new TypeReference>() {}); - return makeSafe(values); + return jsonMapper.readValue((String) input.get(0), new TypeReference>() {}); } catch (JsonParseException e) { warn("JSON Parse Error: " + e.getMessage(), ERRORS.JSONParseError); } catch (JsonMappingException e) { diff --git a/src/main/java/com/mozilla/pig/eval/json/JsonTupleMap.java b/src/main/java/com/mozilla/pig/eval/json/JsonTupleMap.java index 90928b4..5f94668 100644 --- a/src/main/java/com/mozilla/pig/eval/json/JsonTupleMap.java +++ b/src/main/java/com/mozilla/pig/eval/json/JsonTupleMap.java @@ -19,58 +19,54 @@ */ package com.mozilla.pig.eval.json; -import java.util.HashMap; -import java.util.List; +import java.io.EOFException; +import java.io.IOException; import java.util.Map; +import org.apache.pig.EvalFunc; import org.apache.pig.data.Tuple; -public class JsonTupleMap extends JsonMap { - - /** - * Converts List objects to Tuple to keep Pig happy - * - * @param l - * @return - */ - @SuppressWarnings("unchecked") - private Tuple convertListToTuple(List l) { - Tuple t = tupleFactory.newTuple(); - for (Object o : l) { - if (o instanceof List) { - t.append(convertListToTuple((List) o)); - } else { - t.append(o); - } - } +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.mozilla.pig.eval.json.deserializers.JsonTuppleDeserializer; + +public class JsonTupleMap extends EvalFunc> { + + public static enum ERRORS { + JSONParseError, JSONMappingError, EOFError, GenericError + }; - return t; + private static ObjectMapper jsonMapper = new ObjectMapper(); + + static { + jsonMapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("JsonTupleMap"); + module.addDeserializer(Object.class, new JsonTuppleDeserializer()); + jsonMapper.registerModule(module); } - /** - * Convert map and its values to types that Pig can handle - * - * @param m - * @return - */ + @Override - @SuppressWarnings("unchecked") - protected Map makeSafe(Map m) { - Map safeValues = new HashMap(); - for (Map.Entry entry : m.entrySet()) { - Object v = entry.getValue(); - if (v != null && v instanceof List) { - // This is the main diff from the parent class - // in that it converts lists to - Tuple t = convertListToTuple((List) v); - safeValues.put(entry.getKey(), t); - } else if (v != null && v instanceof Map) { - safeValues.put(entry.getKey(), makeSafe((Map) v)); - } else { - safeValues.put(entry.getKey(), v); - } + public Map exec(Tuple input) throws IOException { + if (input == null || input.size() == 0) { + return null; + } + + try { + return jsonMapper.readValue((String) input.get(0), new TypeReference>() {}); + } catch (JsonParseException e) { + warn("JSON Parse Error: " + e.getMessage(), ERRORS.JSONParseError); + } catch (JsonMappingException e) { + warn("JSON Mapping Error: " + e.getMessage(), ERRORS.JSONMappingError); + } catch (EOFException e) { + warn("Hit EOF unexpectedly", ERRORS.EOFError); + } catch (Exception e) { + warn("Generic error during JSON mapping", ERRORS.GenericError); } - return safeValues; + return null; } } diff --git a/src/main/java/com/mozilla/pig/eval/json/deserializers/JsonMapDeserializer.java b/src/main/java/com/mozilla/pig/eval/json/deserializers/JsonMapDeserializer.java new file mode 100644 index 0000000..4186fb7 --- /dev/null +++ b/src/main/java/com/mozilla/pig/eval/json/deserializers/JsonMapDeserializer.java @@ -0,0 +1,59 @@ +/* + * Copyright 2013 Mozilla Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mozilla.pig.eval.json.deserializers; + +import java.io.IOException; + +import org.apache.pig.data.BagFactory; +import org.apache.pig.data.DataBag; +import org.apache.pig.data.Tuple; +import org.apache.pig.data.TupleFactory; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; + +final public class JsonMapDeserializer extends RegularJsonDeserializer { + + private static final BagFactory bagFactory = BagFactory.getInstance(); + private static final TupleFactory tupleFactory = TupleFactory.getInstance(); + + protected Object deserializeArray(JsonParser jp, DeserializationContext ctx) throws IOException, JsonParseException, JsonProcessingException { + Tuple tupple = tupleFactory.newTuple(); + DataBag dbag = bagFactory.newDefaultBag(); + + while (jp.nextToken() != JsonToken.END_ARRAY) { + Object value = deserializeJson(jp, ctx); + if (value instanceof DataBag) { + dbag.addAll((DataBag) value); + } else { + tupple.append(deserializeJson(jp, ctx)); + } + } + + if (tupple.size() > 0) { + dbag.add(tupple); + } + + return dbag; + } +} diff --git a/src/main/java/com/mozilla/pig/eval/json/deserializers/JsonTuppleDeserializer.java b/src/main/java/com/mozilla/pig/eval/json/deserializers/JsonTuppleDeserializer.java new file mode 100644 index 0000000..6e608fc --- /dev/null +++ b/src/main/java/com/mozilla/pig/eval/json/deserializers/JsonTuppleDeserializer.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012 Mozilla Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mozilla.pig.eval.json.deserializers; + +import java.io.IOException; + +import org.apache.pig.data.Tuple; +import org.apache.pig.data.TupleFactory; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; + +final public class JsonTuppleDeserializer extends RegularJsonDeserializer { + + private static final TupleFactory tupleFactory = TupleFactory.getInstance(); + + protected Object deserializeArray(JsonParser jp, DeserializationContext ctx) throws IOException, JsonParseException, JsonProcessingException { + Tuple tupple = tupleFactory.newTuple(); + + while (jp.nextToken() != JsonToken.END_ARRAY) { + Object value = deserializeJson(jp, ctx); + if (value instanceof Tuple) { + tupple.append((Tuple) value); + } else { + tupple.append(deserializeJson(jp, ctx)); + } + } + + return tupple; + } +} diff --git a/src/main/java/com/mozilla/pig/eval/json/deserializers/RegularJsonDeserializer.java b/src/main/java/com/mozilla/pig/eval/json/deserializers/RegularJsonDeserializer.java new file mode 100644 index 0000000..6ab219b --- /dev/null +++ b/src/main/java/com/mozilla/pig/eval/json/deserializers/RegularJsonDeserializer.java @@ -0,0 +1,80 @@ +/* + * Copyright 2013 Mozilla Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mozilla.pig.eval.json.deserializers; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonMappingException; + +public class RegularJsonDeserializer extends JsonDeserializer { + + protected Object deserializeArray(JsonParser jp, DeserializationContext ctx) throws IOException, JsonParseException, JsonProcessingException { + ArrayList array = new ArrayList(); + while (jp.nextToken() != JsonToken.END_ARRAY) { + array.add(deserializeJson(jp, ctx)); + } + return array; + } + + protected Object deserialzeMap(JsonParser jp, DeserializationContext ctx) throws IOException, JsonParseException, JsonProcessingException { + HashMap map = new HashMap(); + while (jp.nextToken() != JsonToken.END_OBJECT) { + map.put(jp.getCurrentName(), deserializeJson(jp, ctx)); + } + return map; + } + + protected Object deserializeJson(JsonParser jp, DeserializationContext ctx) throws IOException, JsonProcessingException { + Object result = null; + JsonToken token = jp.getCurrentToken(); + if (token == JsonToken.FIELD_NAME) { + // ignore. + } else if (token == JsonToken.VALUE_NUMBER_INT) { + result = jp.getIntValue(); + } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { + result = jp.getNumberValue(); + } else if (token == JsonToken.VALUE_STRING) { + result = jp.getText(); + } else if (token == JsonToken.VALUE_TRUE || token == JsonToken.VALUE_FALSE) { + result = jp.getBooleanValue(); + } else if (token == JsonToken.START_OBJECT) { + result = deserialzeMap(jp, ctx); + } else if (token == JsonToken.START_ARRAY) { + result = deserializeArray(jp, ctx); + } else if (token == JsonToken.VALUE_NULL) { + result = null; + } else { + throw new JsonMappingException("No mapping for " + jp.getCurrentName() + "=" + token.name()); + } + return result; + } + + public Object deserialize(JsonParser jp, DeserializationContext ctx) throws IOException, JsonProcessingException { + return deserializeJson(jp, ctx); + } +} From 058fc8f22ae12caabb1cc6fbfc735b5228854410 Mon Sep 17 00:00:00 2001 From: Ricardo Fradinho Date: Tue, 9 Jul 2013 14:03:00 +0100 Subject: [PATCH 4/4] Code formating. --- src/main/java/com/mozilla/pig/eval/date/TimeDelta.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java b/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java index c8542e2..92bcdfe 100644 --- a/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java +++ b/src/main/java/com/mozilla/pig/eval/date/TimeDelta.java @@ -35,7 +35,7 @@ public static enum ERRORS { DateParseError }; private int deltaUnit; private boolean parseDate = false; - private String dateFormat; + private String dateFormat; public TimeDelta() { deltaUnit = Calendar.MILLISECOND;