DATACOUCH-58 allow for null values to be persisted for List elements and Map values.

This commit is contained in:
Ken Dombeck
2013-12-13 11:33:58 -06:00
committed by Michael Nitschinger
parent b11fc6c53c
commit 15ad87a82f
4 changed files with 15 additions and 21 deletions

View File

@@ -227,6 +227,8 @@ public class JacksonTranslationService implements TranslationService, Initializi
}
case VALUE_NUMBER_FLOAT:
return parser.getValueAsDouble();
case VALUE_NULL:
return null;
default:
throw new MappingException("Could not decode primitve value " + token);
}

View File

@@ -259,13 +259,11 @@ public class CouchbaseDocument implements CouchbaseStorable {
* <p>If this is not the case, a {@link IllegalArgumentException} is
* thrown.</p>
*
* Objects that are NULL cannot be stored.
*
* @param value the object to verify its type.
*/
private void verifyValueType(final Object value) {
if(value == null) {
throw new IllegalArgumentException("Attribute of type null cannot be stored.");
return;
}
final Class<?> clazz = value.getClass();
if (simpleTypeHolder.isSimpleType(clazz)) {

View File

@@ -69,7 +69,7 @@ public class CouchbaseList implements CouchbaseStorable {
* @return the {@link CouchbaseList} object for chaining purposes.
*/
public final CouchbaseList put(final Object value) {
verifyValueType(value.getClass());
verifyValueType(value);
payload.add(value);
return this;
@@ -170,9 +170,14 @@ public class CouchbaseList implements CouchbaseStorable {
* <p>If this is not the case, a {@link IllegalArgumentException} is
* thrown.</p>
*
* @param clazz the class type to check and verify.
* @param value the object to verify its type.
*/
private void verifyValueType(final Class<?> clazz) {
private void verifyValueType(final Object value) {
if(value == null) {
return;
}
final Class<?> clazz = value.getClass();
if (simpleTypeHolder.isSimpleType(clazz)) {
return;
}

View File

@@ -43,7 +43,6 @@ 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
@@ -133,10 +132,12 @@ public class CouchbaseTemplateTests {
List<String> names = new ArrayList<String>();
names.add("Michael");
names.add("Thomas");
names.add(null);
List<Integer> votes = new LinkedList<Integer>();
Map<String, Boolean> info1 = new HashMap<String, Boolean>();
info1.put("foo", true);
info1.put("bar", false);
info1.put("nullValue", null);
Map<String, Integer> info2 = new HashMap<String, Integer>();
ComplexPerson complex = new ComplexPerson(id, names, votes, info1, info2);
@@ -144,8 +145,8 @@ public class CouchbaseTemplateTests {
template.save(complex);
String expected = "{\"_class\":\"org.springframework.data.couchbase.core."
+ "CouchbaseTemplateTests$ComplexPerson\",\"info1\":{\"foo\":true,\"bar\""
+ ":false},\"votes\":[],\"firstnames\":[\"Michael\",\"Thomas\"],\"info2\":"
+ "CouchbaseTemplateTests$ComplexPerson\",\"info1\":{\"nullValue\":null,\"foo\":true,\"bar\""
+ ":false},\"votes\":[],\"firstnames\":[\"Michael\",\"Thomas\",null],\"info2\":"
+ "{}}";
assertEquals(expected, client.get(id));
@@ -189,18 +190,6 @@ public class CouchbaseTemplateTests {
}
}
@Test
public void shouldNotSaveNull() {
final Map<String, String> things = new HashMap<String, String>();
things.put("key", null);
try {
template.save(things);
fail("We should not be able to store a NULL!");
} catch (final IllegalArgumentException e) {
assertTrue(true);
}
}
@Test
public void shouldDeserialiseLongs() {
final long time = new Date().getTime();