Skip to content

Commit 373b2b7

Browse files
committed
Merge branch 'master' of github.com:FasterXML/jackson-datatype-joda
2 parents fef4b87 + caaf843 commit 373b2b7

13 files changed

+178
-52
lines changed

release-notes/CREDITS

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ [email protected]:
1212
* Suggested #23: package as a bundle
1313
(2.3.1)
1414

15-
Lorcan C:
15+
Lorcan C
1616
* Contributed #25: Add `KeyDeserializer` for `DateTime`
1717
(2.3.1)
1818

19-
Hendy Irawan: (ceefour@github)
19+
Hendy Irawan (ceefour@github)
2020
* Contributed #27: Allow support for `DateTimeZone`
2121
(2.3.1)
2222

23+
Brad Kennedy (bkenned4@github)
24+
* Contributed #45: Can't use LocalTime, LocalDate & LocalDateTime as Key type for a Map
25+
(2.4.3)
26+

release-notes/VERSION

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
Project: jackson-datatype-joda
2-
Version: 2.4.2 (15-Aug-2014)
2+
Version: 2.4.3 (xx-xxx-2014)
33

4-
No changes since 2.4.0
4+
#45: Can't use LocalTime, LocalDate & LocalDateTime as Key type for a Map
5+
(contributed by Brad K, reported by Guido M)
6+
#46: Interval deserialization fails for negative start instants
7+
(reported by Dan G, dgrabows@github)
58

69
------------------------------------------------------------------------
710
=== History: ===
811
------------------------------------------------------------------------
912

13+
2.4.2 (15-Aug-2014)
14+
15+
No changes since 2.4.0
16+
1017
2.4.1 (17-Jun-2014)
1118

1219
No changes since 2.4.0

src/main/java/com/fasterxml/jackson/datatype/joda/JodaModule.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.fasterxml.jackson.datatype.joda;
22

3-
import org.joda.time.*;
4-
53
import com.fasterxml.jackson.databind.JsonSerializer;
64
import com.fasterxml.jackson.databind.module.SimpleModule;
75
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
86
import com.fasterxml.jackson.datatype.joda.deser.*;
7+
import com.fasterxml.jackson.datatype.joda.deser.key.*;
98
import com.fasterxml.jackson.datatype.joda.ser.*;
9+
import org.joda.time.*;
1010

1111
public class JodaModule extends SimpleModule
1212
{
@@ -19,7 +19,7 @@ public JodaModule()
1919
addDeserializer(DateMidnight.class, new DateMidnightDeserializer());
2020
addDeserializer(DateTime.class, DateTimeDeserializer.forType(DateTime.class));
2121
addDeserializer(DateTimeZone.class, new DateTimeZoneDeserializer());
22-
22+
2323
addDeserializer(Duration.class, new DurationDeserializer());
2424
addDeserializer(Instant.class, new InstantDeserializer());
2525
addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
@@ -49,8 +49,11 @@ public JodaModule()
4949
addSerializer(MonthDay.class, stringSer);
5050
addSerializer(YearMonth.class, stringSer);
5151

52-
// then key deserializers - only one included for DateTime here.
52+
// then key deserializers
5353
addKeyDeserializer(DateTime.class, new DateTimeKeyDeserializer());
54+
addKeyDeserializer(LocalTime.class, new LocalTimeKeyDeserializer());
55+
addKeyDeserializer(LocalDate.class, new LocalDateKeyDeserializer());
56+
addKeyDeserializer(LocalDateTime.class, new LocalDateTimeKeyDeserializer());
5457
}
5558

5659
@Override

src/main/java/com/fasterxml/jackson/datatype/joda/deser/DateTimeKeyDeserializer.java

-22
This file was deleted.

src/main/java/com/fasterxml/jackson/datatype/joda/deser/IntervalDeserializer.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.core.JsonToken;
66
import com.fasterxml.jackson.databind.DeserializationContext;
7+
import com.fasterxml.jackson.databind.JsonMappingException;
8+
79
import org.joda.time.Interval;
810

911
import java.io.IOException;
@@ -19,13 +21,26 @@ public IntervalDeserializer() {
1921
@Override
2022
public Interval deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException
2123
{
22-
if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
23-
String v = jsonParser.getText();
24-
int dashIndex = v.indexOf('-');
25-
long start = Long.valueOf(v.substring(0, dashIndex));
26-
long end = Long.valueOf(v.substring(dashIndex + 1));
27-
return new Interval(start, end);
24+
JsonToken t = jsonParser.getCurrentToken();
25+
if (t != JsonToken.VALUE_STRING) {
26+
throw deserializationContext.mappingException("expected JSON String, got "+t);
27+
}
28+
String v = jsonParser.getText().trim();
29+
30+
int dashIndex = v.isEmpty() ? -1 : v.indexOf('-', 1);
31+
if (dashIndex < 0) {
32+
throw deserializationContext.weirdStringException(v, handledType(), "no hyphen found to separate start, end");
33+
}
34+
long start, end;
35+
String str = v.substring(0, dashIndex);
36+
try {
37+
start = Long.valueOf(str);
38+
str = v.substring(dashIndex + 1);
39+
end = Long.valueOf(str);
40+
} catch (NumberFormatException e) {
41+
throw JsonMappingException.from(jsonParser,
42+
"Failed to parse number from '"+str+"' (full source String '"+v+"') to construct "+handledType().getName());
2843
}
29-
throw deserializationContext.mappingException("expected JSON String");
44+
return new Interval(start, end);
3045
}
3146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fasterxml.jackson.datatype.joda.deser.key;
2+
3+
import java.io.IOException;
4+
5+
import org.joda.time.*;
6+
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import com.fasterxml.jackson.databind.DeserializationContext;
9+
10+
public class DateTimeKeyDeserializer extends JodaKeyDeserializer {
11+
12+
@Override
13+
protected DateTime deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException{
14+
return new DateTime(key, DateTimeZone.forTimeZone(ctxt.getTimeZone()));
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fasterxml.jackson.datatype.joda.deser.key;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import com.fasterxml.jackson.databind.KeyDeserializer;
6+
7+
import java.io.IOException;
8+
9+
abstract class JodaKeyDeserializer extends KeyDeserializer {
10+
private static final long serialVersionUID = 1L;
11+
12+
@Override
13+
public final Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
14+
if (key.length() == 0) { // [JACKSON-360]
15+
return null;
16+
}
17+
return deserialize(key, ctxt);
18+
}
19+
20+
protected abstract Object deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.datatype.joda.deser.key;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import org.joda.time.LocalDate;
6+
import org.joda.time.format.DateTimeFormatter;
7+
import org.joda.time.format.ISODateTimeFormat;
8+
9+
import java.io.IOException;
10+
11+
public class LocalDateKeyDeserializer extends JodaKeyDeserializer {
12+
private static final DateTimeFormatter parser = ISODateTimeFormat.localDateParser();
13+
14+
@Override
15+
protected LocalDate deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
16+
return parser.parseLocalDate(key);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.datatype.joda.deser.key;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import org.joda.time.LocalDateTime;
6+
import org.joda.time.format.DateTimeFormatter;
7+
import org.joda.time.format.ISODateTimeFormat;
8+
9+
import java.io.IOException;
10+
11+
public class LocalDateTimeKeyDeserializer extends JodaKeyDeserializer {
12+
private static final DateTimeFormatter parser = ISODateTimeFormat.localDateOptionalTimeParser();
13+
14+
@Override
15+
protected LocalDateTime deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
16+
return parser.parseLocalDateTime(key);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.datatype.joda.deser.key;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import org.joda.time.LocalTime;
6+
import org.joda.time.format.DateTimeFormatter;
7+
import org.joda.time.format.ISODateTimeFormat;
8+
9+
import java.io.IOException;
10+
11+
public class LocalTimeKeyDeserializer extends JodaKeyDeserializer {
12+
private static final DateTimeFormatter parser = ISODateTimeFormat.localTimeParser();
13+
14+
@Override
15+
protected LocalTime deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
16+
return parser.parseLocalTime(key);
17+
}
18+
}

src/test/java/com/fasterxml/jackson/datatype/joda/JodaTestBase.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package com.fasterxml.jackson.datatype.joda;
22

3-
import java.io.*;
4-
3+
import com.fasterxml.jackson.databind.ObjectMapper;
54
import junit.framework.TestCase;
65

7-
import static org.junit.Assert.*;
6+
import java.io.IOException;
87

9-
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import static org.junit.Assert.*;
109

1110
public abstract class JodaTestBase extends TestCase
1211
{
1312
protected static ObjectMapper jodaMapper()
1413
{
15-
ObjectMapper mapper = new ObjectMapper();
16-
mapper.registerModule(new JodaModule());
17-
return mapper;
14+
return new JodaMapper();
1815
}
1916

2017
/*

src/test/java/com/fasterxml/jackson/datatype/joda/MixedListTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class MixedListTest extends JodaTestBase
1414

1515
public void testMixedList() throws Exception
1616
{
17-
final Map<String, Object> map = new HashMap<String, Object>();
18-
DateTime dt = new DateTime(DateTimeZone.UTC);
19-
map.put("A", dt);
20-
map.put("B", 0);
17+
final Map<String, Object> map = new HashMap<String, Object>();
18+
DateTime dt = new DateTime(DateTimeZone.UTC);
19+
map.put("A", dt);
20+
map.put("B", 0);
2121

2222
final String json = MAPPER.writeValueAsString(map);
2323
// by default, timestamps should come out as longs...
@@ -29,14 +29,14 @@ public void testMixedList() throws Exception
2929
Object obB = result.get("B");
3030
assertNotNull(obB);
3131
if (!(obB instanceof Number)) {
32-
fail("Expected 'B' to be a Number; instead of value of type "+obB.getClass().getName());
32+
fail("Expected 'B' to be a Number; instead of value of type "+obB.getClass().getName());
3333
}
3434

3535
assertEquals(Integer.valueOf(0), result.get("B"));
3636
Object obA = result.get("A");
3737
assertNotNull(obA);
3838
if (!(obA instanceof Number)) {
39-
fail("Expected 'A' to be a number; instead of value of type "+obA.getClass().getName());
39+
fail("Expected 'A' to be a number; instead of value of type "+obA.getClass().getName());
4040
}
4141
}
4242
}

src/test/java/com/fasterxml/jackson/datatype/joda/JodaDeserializationTest.java renamed to src/test/java/com/fasterxml/jackson/datatype/joda/deser/MiscDeserializationTest.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package com.fasterxml.jackson.datatype.joda;
1+
package com.fasterxml.jackson.datatype.joda.deser;
22

33
import com.fasterxml.jackson.annotation.JsonTypeInfo;
44
import com.fasterxml.jackson.core.type.TypeReference;
55
import com.fasterxml.jackson.databind.DeserializationFeature;
66
import com.fasterxml.jackson.databind.JsonMappingException;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.datatype.joda.JodaTestBase;
9+
810
import org.joda.time.*;
911

1012
import java.io.IOException;
@@ -18,7 +20,7 @@
1820
* Basic support is added for handling {@link DateTime}; more can be
1921
* added over time if and when requested.
2022
*/
21-
public class JodaDeserializationTest extends JodaTestBase
23+
public class MiscDeserializationTest extends JodaTestBase
2224
{
2325
/*
2426
/**********************************************************
@@ -236,6 +238,10 @@ public void testIntervalDeser() throws IOException
236238
Interval interval = MAPPER.readValue(quote("1396439982-1396440001"), Interval.class);
237239
assertEquals(1396439982, interval.getStartMillis());
238240
assertEquals(1396440001, interval.getEndMillis());
241+
242+
interval = MAPPER.readValue(quote("-100-1396440001"), Interval.class);
243+
assertEquals(-100, interval.getStartMillis());
244+
assertEquals(1396440001, interval.getEndMillis());
239245
}
240246

241247
public void testIntervalDeserWithTypeInfo() throws IOException
@@ -433,6 +439,30 @@ public void testDateTimeKeyDeserialize() throws IOException {
433439
assertTrue(map.containsKey(DateTime.parse("1970-01-01T00:00:00.000Z")));
434440
}
435441

442+
public void testLocalDateKeyDeserialize() throws IOException {
443+
444+
final String json = "{" + quote("2014-05-23") + ":0}";
445+
final Map<LocalDate, Long> map = MAPPER.readValue(json, new TypeReference<Map<LocalDate, String>>() { });
446+
447+
assertNotNull(map);
448+
assertTrue(map.containsKey(LocalDate.parse("2014-05-23")));
449+
}
450+
451+
public void testLocalTimeKeyDeserialize() throws IOException {
452+
453+
final String json = "{" + quote("00:00:00.000") + ":0}";
454+
final Map<LocalTime, Long> map = MAPPER.readValue(json, new TypeReference<Map<LocalTime, String>>() { });
455+
assertNotNull(map);
456+
assertTrue(map.containsKey(LocalTime.parse("00:00:00.000")));
457+
}
458+
public void testLocalDateTimeKeyDeserialize() throws IOException {
459+
460+
final String json = "{" + quote("2014-05-23T00:00:00.000") + ":0}";
461+
final Map<LocalDateTime, Long> map = MAPPER.readValue(json, new TypeReference<Map<LocalDateTime, String>>() { });
462+
assertNotNull(map);
463+
assertTrue(map.containsKey(LocalDateTime.parse("2014-05-23T00:00:00.000")));
464+
}
465+
436466
public void testDeserMonthDay() throws Exception
437467
{
438468
String monthDayString = new MonthDay(7, 23).toString();

0 commit comments

Comments
 (0)