From 6fd4d925fb20b7e52ab15655d347b080e789273f Mon Sep 17 00:00:00 2001 From: David Harrigan Date: Wed, 9 Oct 2013 16:57:46 +0100 Subject: [PATCH] DATACOUCH-34 - Can't deserialize long/Long/Date fields. Catch the attempt to parse a long as an int, then attempt to return a long value. -=david=- --- .../JacksonTranslationService.java | 17 +++++-- .../core/CouchbaseTemplateTests.java | 49 +++++++++++++++++-- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java b/src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java index 5fa1c8eaf..72d55f32c 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java @@ -50,6 +50,7 @@ public class JacksonTranslationService implements TranslationService { * Encode a {@link CouchbaseStorable} to a JSON string. * * @param source the source document to encode. + * * @return the encoded JSON String. */ @Override @@ -72,6 +73,7 @@ public final Object encode(final CouchbaseStorable source) { * * @param source the source document * @param generator the JSON generator. + * * @throws IOException */ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator generator) throws IOException { @@ -103,6 +105,7 @@ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator * * @param source the source formatted document. * @param target the target of the populated data. + * * @return the decoded structure. */ @Override @@ -132,8 +135,9 @@ public final CouchbaseStorable decode(final Object source, final CouchbaseStorab * * @param parser the JSON parser with the content. * @param target the target where the content should be stored. - * @returns the decoded object. + * * @throws IOException + * @returns the decoded object. */ private CouchbaseDocument decodeObject(final JsonParser parser, final CouchbaseDocument target) throws IOException { JsonToken currentToken = parser.nextToken(); @@ -161,8 +165,9 @@ private CouchbaseDocument decodeObject(final JsonParser parser, final CouchbaseD * * @param parser the JSON parser with the content. * @param target the target where the content should be stored. - * @returns the decoded list. + * * @throws IOException + * @returns the decoded list. */ private CouchbaseList decodeArray(final JsonParser parser, final CouchbaseList target) throws IOException { JsonToken currentToken = parser.nextToken(); @@ -187,7 +192,9 @@ private CouchbaseList decodeArray(final JsonParser parser, final CouchbaseList t * * @param token the type of token. * @param parser the parser with the content. + * * @return the decoded primitve. + * * @throws IOException */ private Object decodePrimitive(final JsonToken token, final JsonParser parser) throws IOException { @@ -198,7 +205,11 @@ private Object decodePrimitive(final JsonToken token, final JsonParser parser) t case VALUE_STRING: return parser.getValueAsString(); case VALUE_NUMBER_INT: - return parser.getValueAsInt(); + try { + return parser.getValueAsInt(); + } catch (final JsonParseException e) { + return parser.getValueAsLong(); + } case VALUE_NUMBER_FLOAT: return parser.getValueAsDouble(); default: diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java index a2f201f2d..c486e196a 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java @@ -19,8 +19,6 @@ import com.couchbase.client.CouchbaseClient; import com.couchbase.client.protocol.views.Query; import com.couchbase.client.protocol.views.Stale; -import net.spy.memcached.internal.OperationFuture; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -32,9 +30,18 @@ import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Michael Nitschinger @@ -191,6 +198,14 @@ public void shouldNotSaveNull() { assertTrue(true); } } + + @Test + public void shouldDeserialiseLongs() { + SimpleWithLong simpleWithLong = new SimpleWithLong("simpleWithLong:simple", new Date().getTime()); + template.save(simpleWithLong); + simpleWithLong = template.findById("simpleWithLong:simple", SimpleWithLong.class); + assertNotNull(simpleWithLong); + } /** * A sample document with just an id and property. @@ -265,4 +280,30 @@ String getId() { return id; } } + + @Document + static class SimpleWithLong { + + @Id + private String id; + + private long value; + + SimpleWithLong(final String id, final long value) { + this.id = id; + this.value = value; + } + + String getId() { + return id; + } + + long getValue() { + return value; + } + + void setValue(final long value) { + this.value = value; + } + } }