Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.query;
 
 import java.util.Map;
 
 import  org.springframework.core.convert.converter.Converter;
 
Utility methods for JSON serialization.

Author(s):
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 Object.toString() method if it's not serializable. Useful for printing raw DBObjects containing complex values before actually converting them into Mongo native types.

Parameters:
value
Returns:
 
 	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(((DBObjectvalue).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<ObjectObject>() {
 			public Object convert(Object source) {
 				return serializeToJsonSafely(source);
 			}
 		});
 	}

Creates a string representation from the given Iterable prepending the postfix, applying the given Converter to each element before adding it to the result String, concatenating each element with , and applying the postfix.

Parameters:
source
prefix
postfix
transformer
Returns:
 
 	private static <T> String iterableToDelimitedString(Iterable<T> sourceString prefixString postfix,
 			Converter<? super T, Objecttransformer) {
 
 		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();
	}
New to GrepCode? Check out our FAQ X