Skip to content

Commit

Permalink
Fixed #46
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 27, 2014
1 parent bdf6c97 commit 34e107e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
8 changes: 6 additions & 2 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ [email protected]:
* Suggested #23: package as a bundle
(2.3.1)

Lorcan C:
Lorcan C
* Contributed #25: Add `KeyDeserializer` for `DateTime`
(2.3.1)

Hendy Irawan: (ceefour@github)
Hendy Irawan (ceefour@github)
* Contributed #27: Allow support for `DateTimeZone`
(2.3.1)

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

11 changes: 9 additions & 2 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
Project: jackson-datatype-joda
Version: 2.4.2 (15-Aug-2014)
Version: 2.4.3 (xx-xxx-2014)

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

------------------------------------------------------------------------
=== History: ===
------------------------------------------------------------------------

2.4.2 (15-Aug-2014)

No changes since 2.4.0

2.4.1 (17-Jun-2014)

No changes since 2.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;

import org.joda.time.Interval;

import java.io.IOException;
Expand All @@ -19,13 +21,26 @@ public IntervalDeserializer() {
@Override
public Interval deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException
{
if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
String v = jsonParser.getText();
int dashIndex = v.indexOf('-');
long start = Long.valueOf(v.substring(0, dashIndex));
long end = Long.valueOf(v.substring(dashIndex + 1));
return new Interval(start, end);
JsonToken t = jsonParser.getCurrentToken();
if (t != JsonToken.VALUE_STRING) {
throw deserializationContext.mappingException("expected JSON String, got "+t);
}
String v = jsonParser.getText().trim();

int dashIndex = v.isEmpty() ? -1 : v.indexOf('-', 1);
if (dashIndex < 0) {
throw deserializationContext.weirdStringException(v, handledType(), "no hyphen found to separate start, end");
}
long start, end;
String str = v.substring(0, dashIndex);
try {
start = Long.valueOf(str);
str = v.substring(dashIndex + 1);
end = Long.valueOf(str);
} catch (NumberFormatException e) {
throw JsonMappingException.from(jsonParser,
"Failed to parse number from '"+str+"' (full source String '"+v+"') to construct "+handledType().getName());
}
throw deserializationContext.mappingException("expected JSON String");
return new Interval(start, end);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class MixedListTest extends JodaTestBase

public void testMixedList() throws Exception
{
final Map<String, Object> map = new HashMap<String, Object>();
DateTime dt = new DateTime(DateTimeZone.UTC);
map.put("A", dt);
map.put("B", 0);
final Map<String, Object> map = new HashMap<String, Object>();
DateTime dt = new DateTime(DateTimeZone.UTC);
map.put("A", dt);
map.put("B", 0);

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

assertEquals(Integer.valueOf(0), result.get("B"));
Object obA = result.get("A");
assertNotNull(obA);
if (!(obA instanceof Number)) {
fail("Expected 'A' to be a number; instead of value of type "+obA.getClass().getName());
fail("Expected 'A' to be a number; instead of value of type "+obA.getClass().getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.fasterxml.jackson.datatype.joda;
package com.fasterxml.jackson.datatype.joda.deser;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaTestBase;

import org.joda.time.*;

import java.io.IOException;
Expand All @@ -18,7 +20,7 @@
* Basic support is added for handling {@link DateTime}; more can be
* added over time if and when requested.
*/
public class JodaDeserializationTest extends JodaTestBase
public class MiscDeserializationTest extends JodaTestBase
{
/*
/**********************************************************
Expand Down Expand Up @@ -236,6 +238,10 @@ public void testIntervalDeser() throws IOException
Interval interval = MAPPER.readValue(quote("1396439982-1396440001"), Interval.class);
assertEquals(1396439982, interval.getStartMillis());
assertEquals(1396440001, interval.getEndMillis());

interval = MAPPER.readValue(quote("-100-1396440001"), Interval.class);
assertEquals(-100, interval.getStartMillis());
assertEquals(1396440001, interval.getEndMillis());
}

public void testIntervalDeserWithTypeInfo() throws IOException
Expand Down

0 comments on commit 34e107e

Please sign in to comment.