Skip to content

DATACOUCH-34 - Can't deserialize long/Long/Date fields. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
}