/mongodb-util/src/main/java/com/torrenttamer/mongodb/gson/GsonDBObjectMarshaller.java
Java | 123 lines | 83 code | 14 blank | 26 comment | 4 complexity | 082dac8c5d4d4df3cb648bd57e64da8a MD5 | raw file
- /*
- * Copyright 2012 Keith Flanagan
- *
- * 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.
- *
- * File created: 28-Nov-2012, 14:08:22
- */
- package com.torrenttamer.mongodb.gson;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DBObject;
- import com.mongodb.util.JSON;
- import com.torrenttamer.mongodb.dbobject.DbObjectMarshaller;
- import com.torrenttamer.mongodb.dbobject.DbObjectMarshallerException;
- import java.lang.reflect.Type;
- import java.util.Map;
- /**
- *
- * @author Keith Flanagan
- */
- public class GsonDBObjectMarshaller
- implements DbObjectMarshaller
- {
- private final Gson gson;
-
- public GsonDBObjectMarshaller()
- {
- this.gson = new Gson();
-
- }
- public GsonDBObjectMarshaller(Map<Type, Object> typeToTypeAdaptors)
- {
- GsonBuilder builder = new GsonBuilder();
- for (Type type : typeToTypeAdaptors.keySet()) {
- builder.registerTypeAdapter(type, typeToTypeAdaptors.get(type));
- }
-
- this.gson = builder.create();
- }
-
-
- @Override
- public BasicDBObject serialize(Object bean) throws DbObjectMarshallerException {
- try {
- return (BasicDBObject) JSON.parse(gson.toJson(bean));
- } catch (Exception ex) {
- throw new DbObjectMarshallerException(
- "Failed to serialize bean type: "+bean.getClass().getName()
- + ", content: "+bean.toString(), ex);
- }
- }
-
- @Override
- public String serializeToString(Object bean) throws DbObjectMarshallerException {
- try {
- StringBuilder sb = new StringBuilder(gson.toJson(bean));
- /*
- * The serializer seems to but double-quote characters at each end.
- * Remove these since they cause problems and are not consistent with the
- * serialization produced by JSON.parse(...).
- */
- if (sb.length() > 0 && sb.charAt(0) == '\"') {
- sb.deleteCharAt(sb.length()-1);
- sb.deleteCharAt(0);
- }
- return sb.toString();
- } catch (Exception ex) {
- throw new DbObjectMarshallerException(
- "Failed to serialize bean type: "+bean.getClass().getName()
- + ", content: "+bean.toString(), ex);
- }
- }
-
- @Override
- public <T> T deserialize(DBObject dbObject, Class<T> type)
- throws DbObjectMarshallerException
- {
- try {
- T item = gson.fromJson(dbObject.toString(), type);
- return item;
- }
- catch(Exception e) {
- throw new RuntimeException(
- "Failed to deserialise object from JSON. Target type: "
- + type + ". Document: " + dbObject, e);
- }
- }
- @Override
- public <T> T deserialize(String jsonString, Class<T> type)
- throws DbObjectMarshallerException
- {
- try {
- T item = gson.fromJson(jsonString, type);
- return item;
- }
- catch(Exception e) {
- throw new RuntimeException(
- "Failed to deserialise object from JSON. Target type: "
- + type + ". Document: " + jsonString, e);
- }
- }
- public Gson getGson() {
- return gson;
- }
- }