Browse Source
The debug output now uses the already mapped query object when concatenating the log string. Improved applying the id after save operations by inspecting whether the object already has the id set before trying to set it. This could have caused problems in case you use a complex id and don't provide a custom converter as it can be serialized out of the box. Fixed minor glitch in MappingMongoConverter which was not really a bug as another path through the code has covered the scenario later on. Introduced SerializationUtils class that provides a method to safely serialize objects to pseudo JSON. Pseudo in the sense that it simply renders a complex object as { $java : object.toString() }. This is useful for debug output before the DBObject was mapped into Mongo-native types.
1.0.x
8 changed files with 251 additions and 6 deletions
@ -0,0 +1,110 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.mongodb.core; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Map.Entry; |
||||||
|
|
||||||
|
import org.springframework.core.convert.converter.Converter; |
||||||
|
|
||||||
|
import com.mongodb.DBObject; |
||||||
|
import com.mongodb.util.JSON; |
||||||
|
|
||||||
|
/** |
||||||
|
* Utility methods for JSON serialization. |
||||||
|
* |
||||||
|
* @author Oliver Gierke |
||||||
|
*/ |
||||||
|
public abstract class SerializationUtils { |
||||||
|
|
||||||
|
private SerializationUtils() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Serializes the given object into pseudo-JSON meaning it's trying to create a JSON representation as far as possible |
||||||
|
* but falling back to the given object's {@link Object#toString()} method if it's not serializable. Useful for |
||||||
|
* printing raw {@link DBObject}s containing complex values before actually converting them into Mongo native types. |
||||||
|
* |
||||||
|
* @param value |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String serializeToJsonSafely(Object value) { |
||||||
|
|
||||||
|
if (value == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
return JSON.serialize(value); |
||||||
|
} catch (Exception e) { |
||||||
|
if (value instanceof Collection) { |
||||||
|
return toString((Collection<?>) value); |
||||||
|
} else if (value instanceof Map) { |
||||||
|
return toString((Map<?, ?>) value); |
||||||
|
} else if (value instanceof DBObject) { |
||||||
|
return toString(((DBObject) value).toMap()); |
||||||
|
} else { |
||||||
|
return String.format("{ $java : %s }", value.toString()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static String toString(Map<?, ?> source) { |
||||||
|
return iterableToDelimitedString(source.entrySet(), "{ ", " }", new Converter<Entry<?, ?>, Object>() { |
||||||
|
public Object convert(Entry<?, ?> source) { |
||||||
|
return String.format("\"%s\" : %s", source.getKey(), serializeToJsonSafely(source.getValue())); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
private static String toString(Collection<?> source) { |
||||||
|
return iterableToDelimitedString(source, "[ ", " ]", new Converter<Object, Object>() { |
||||||
|
public Object convert(Object source) { |
||||||
|
return serializeToJsonSafely(source); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a string representation from the given {@link Iterable} prepending the postfix, applying the given |
||||||
|
* {@link Converter} to each element before adding it to the result {@link String}, concatenating each element with |
||||||
|
* {@literal ,} and applying the postfix. |
||||||
|
* |
||||||
|
* @param source |
||||||
|
* @param prefix |
||||||
|
* @param postfix |
||||||
|
* @param transformer |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private static <T> String iterableToDelimitedString(Iterable<T> source, String prefix, String postfix, |
||||||
|
Converter<? super T, Object> transformer) { |
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder(prefix); |
||||||
|
Iterator<T> iterator = source.iterator(); |
||||||
|
|
||||||
|
while (iterator.hasNext()) { |
||||||
|
builder.append(transformer.convert(iterator.next())); |
||||||
|
if (iterator.hasNext()) { |
||||||
|
builder.append(", "); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return builder.append(postfix).toString(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,66 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.mongodb.core; |
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*; |
||||||
|
import static org.junit.Assert.*; |
||||||
|
import static org.springframework.data.mongodb.core.SerializationUtils.*; |
||||||
|
|
||||||
|
import java.util.Arrays; |
||||||
|
|
||||||
|
import org.hamcrest.Matcher; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject; |
||||||
|
import com.mongodb.DBObject; |
||||||
|
|
||||||
|
/** |
||||||
|
* Unit tests for {@link SerializationUtils}. |
||||||
|
* |
||||||
|
* @author Oliver Gierke |
||||||
|
*/ |
||||||
|
public class SerializationUtilsUnitTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void writesSimpleDBObject() { |
||||||
|
|
||||||
|
DBObject dbObject = new BasicDBObject("foo", "bar"); |
||||||
|
assertThat(serializeToJsonSafely(dbObject), is("{ \"foo\" : \"bar\"}")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void writesComplexObjectAsPlainToString() { |
||||||
|
|
||||||
|
DBObject dbObject = new BasicDBObject("foo", new Complex()); |
||||||
|
assertThat(serializeToJsonSafely(dbObject), |
||||||
|
startsWith("{ \"foo\" : { $java : org.springframework.data.mongodb.core.SerializationUtilsUnitTests$Complex")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void writesCollection() { |
||||||
|
|
||||||
|
DBObject dbObject = new BasicDBObject("foo", Arrays.asList("bar", new Complex())); |
||||||
|
Matcher<String> expectedOutput = allOf( |
||||||
|
startsWith("{ \"foo\" : [ \"bar\", { $java : org.springframework.data.mongodb.core.SerializationUtilsUnitTests$Complex"), |
||||||
|
endsWith(" } ] }")); |
||||||
|
assertThat(serializeToJsonSafely(dbObject), is(expectedOutput)); |
||||||
|
} |
||||||
|
|
||||||
|
static class Complex { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue