Skip to content

Commit 5ae7c76

Browse files
committed
Merge pull request #10 from dharrigan/DATACOUCH-34
DATACOUCH-34 - Can't deserialize long/Long/Date fields.
2 parents 270f573 + 6fd4d92 commit 5ae7c76

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class JacksonTranslationService implements TranslationService {
5050
* Encode a {@link CouchbaseStorable} to a JSON string.
5151
*
5252
* @param source the source document to encode.
53+
*
5354
* @return the encoded JSON String.
5455
*/
5556
@Override
@@ -72,6 +73,7 @@ public final Object encode(final CouchbaseStorable source) {
7273
*
7374
* @param source the source document
7475
* @param generator the JSON generator.
76+
*
7577
* @throws IOException
7678
*/
7779
private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator generator) throws IOException {
@@ -103,6 +105,7 @@ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator
103105
*
104106
* @param source the source formatted document.
105107
* @param target the target of the populated data.
108+
*
106109
* @return the decoded structure.
107110
*/
108111
@Override
@@ -132,8 +135,9 @@ public final CouchbaseStorable decode(final Object source, final CouchbaseStorab
132135
*
133136
* @param parser the JSON parser with the content.
134137
* @param target the target where the content should be stored.
135-
* @returns the decoded object.
138+
*
136139
* @throws IOException
140+
* @returns the decoded object.
137141
*/
138142
private CouchbaseDocument decodeObject(final JsonParser parser, final CouchbaseDocument target) throws IOException {
139143
JsonToken currentToken = parser.nextToken();
@@ -161,8 +165,9 @@ private CouchbaseDocument decodeObject(final JsonParser parser, final CouchbaseD
161165
*
162166
* @param parser the JSON parser with the content.
163167
* @param target the target where the content should be stored.
164-
* @returns the decoded list.
168+
*
165169
* @throws IOException
170+
* @returns the decoded list.
166171
*/
167172
private CouchbaseList decodeArray(final JsonParser parser, final CouchbaseList target) throws IOException {
168173
JsonToken currentToken = parser.nextToken();
@@ -187,7 +192,9 @@ private CouchbaseList decodeArray(final JsonParser parser, final CouchbaseList t
187192
*
188193
* @param token the type of token.
189194
* @param parser the parser with the content.
195+
*
190196
* @return the decoded primitve.
197+
*
191198
* @throws IOException
192199
*/
193200
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
198205
case VALUE_STRING:
199206
return parser.getValueAsString();
200207
case VALUE_NUMBER_INT:
201-
return parser.getValueAsInt();
208+
try {
209+
return parser.getValueAsInt();
210+
} catch (final JsonParseException e) {
211+
return parser.getValueAsLong();
212+
}
202213
case VALUE_NUMBER_FLOAT:
203214
return parser.getValueAsDouble();
204215
default:

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.couchbase.client.CouchbaseClient;
2020
import com.couchbase.client.protocol.views.Query;
2121
import com.couchbase.client.protocol.views.Stale;
22-
import net.spy.memcached.internal.OperationFuture;
23-
import org.junit.Before;
2422
import org.junit.Test;
2523
import org.junit.runner.RunWith;
2624
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,9 +30,18 @@
3230
import org.springframework.test.context.TestExecutionListeners;
3331
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3432

35-
import java.util.*;
33+
import java.util.ArrayList;
34+
import java.util.Date;
35+
import java.util.HashMap;
36+
import java.util.LinkedList;
37+
import java.util.List;
38+
import java.util.Map;
3639

37-
import static org.junit.Assert.*;
40+
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertNotNull;
42+
import static org.junit.Assert.assertNull;
43+
import static org.junit.Assert.assertTrue;
44+
import static org.junit.Assert.fail;
3845

3946
/**
4047
* @author Michael Nitschinger
@@ -191,6 +198,14 @@ public void shouldNotSaveNull() {
191198
assertTrue(true);
192199
}
193200
}
201+
202+
@Test
203+
public void shouldDeserialiseLongs() {
204+
SimpleWithLong simpleWithLong = new SimpleWithLong("simpleWithLong:simple", new Date().getTime());
205+
template.save(simpleWithLong);
206+
simpleWithLong = template.findById("simpleWithLong:simple", SimpleWithLong.class);
207+
assertNotNull(simpleWithLong);
208+
}
194209

195210
/**
196211
* A sample document with just an id and property.
@@ -265,4 +280,30 @@ String getId() {
265280
return id;
266281
}
267282
}
283+
284+
@Document
285+
static class SimpleWithLong {
286+
287+
@Id
288+
private String id;
289+
290+
private long value;
291+
292+
SimpleWithLong(final String id, final long value) {
293+
this.id = id;
294+
this.value = value;
295+
}
296+
297+
String getId() {
298+
return id;
299+
}
300+
301+
long getValue() {
302+
return value;
303+
}
304+
305+
void setValue(final long value) {
306+
this.value = value;
307+
}
308+
}
268309
}

0 commit comments

Comments
 (0)