PageRenderTime 3457ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mongodb-util/src/main/java/com/torrenttamer/mongodb/gson/GsonDBObjectMarshaller.java

https://bitbucket.org/keith_f/tamer-common
Java | 123 lines | 83 code | 14 blank | 26 comment | 4 complexity | 082dac8c5d4d4df3cb648bd57e64da8a MD5 | raw file
  1. /*
  2. * Copyright 2012 Keith Flanagan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * File created: 28-Nov-2012, 14:08:22
  17. */
  18. package com.torrenttamer.mongodb.gson;
  19. import com.google.gson.Gson;
  20. import com.google.gson.GsonBuilder;
  21. import com.mongodb.BasicDBObject;
  22. import com.mongodb.DBObject;
  23. import com.mongodb.util.JSON;
  24. import com.torrenttamer.mongodb.dbobject.DbObjectMarshaller;
  25. import com.torrenttamer.mongodb.dbobject.DbObjectMarshallerException;
  26. import java.lang.reflect.Type;
  27. import java.util.Map;
  28. /**
  29. *
  30. * @author Keith Flanagan
  31. */
  32. public class GsonDBObjectMarshaller
  33. implements DbObjectMarshaller
  34. {
  35. private final Gson gson;
  36. public GsonDBObjectMarshaller()
  37. {
  38. this.gson = new Gson();
  39. }
  40. public GsonDBObjectMarshaller(Map<Type, Object> typeToTypeAdaptors)
  41. {
  42. GsonBuilder builder = new GsonBuilder();
  43. for (Type type : typeToTypeAdaptors.keySet()) {
  44. builder.registerTypeAdapter(type, typeToTypeAdaptors.get(type));
  45. }
  46. this.gson = builder.create();
  47. }
  48. @Override
  49. public BasicDBObject serialize(Object bean) throws DbObjectMarshallerException {
  50. try {
  51. return (BasicDBObject) JSON.parse(gson.toJson(bean));
  52. } catch (Exception ex) {
  53. throw new DbObjectMarshallerException(
  54. "Failed to serialize bean type: "+bean.getClass().getName()
  55. + ", content: "+bean.toString(), ex);
  56. }
  57. }
  58. @Override
  59. public String serializeToString(Object bean) throws DbObjectMarshallerException {
  60. try {
  61. StringBuilder sb = new StringBuilder(gson.toJson(bean));
  62. /*
  63. * The serializer seems to but double-quote characters at each end.
  64. * Remove these since they cause problems and are not consistent with the
  65. * serialization produced by JSON.parse(...).
  66. */
  67. if (sb.length() > 0 && sb.charAt(0) == '\"') {
  68. sb.deleteCharAt(sb.length()-1);
  69. sb.deleteCharAt(0);
  70. }
  71. return sb.toString();
  72. } catch (Exception ex) {
  73. throw new DbObjectMarshallerException(
  74. "Failed to serialize bean type: "+bean.getClass().getName()
  75. + ", content: "+bean.toString(), ex);
  76. }
  77. }
  78. @Override
  79. public <T> T deserialize(DBObject dbObject, Class<T> type)
  80. throws DbObjectMarshallerException
  81. {
  82. try {
  83. T item = gson.fromJson(dbObject.toString(), type);
  84. return item;
  85. }
  86. catch(Exception e) {
  87. throw new RuntimeException(
  88. "Failed to deserialise object from JSON. Target type: "
  89. + type + ". Document: " + dbObject, e);
  90. }
  91. }
  92. @Override
  93. public <T> T deserialize(String jsonString, Class<T> type)
  94. throws DbObjectMarshallerException
  95. {
  96. try {
  97. T item = gson.fromJson(jsonString, type);
  98. return item;
  99. }
  100. catch(Exception e) {
  101. throw new RuntimeException(
  102. "Failed to deserialise object from JSON. Target type: "
  103. + type + ". Document: " + jsonString, e);
  104. }
  105. }
  106. public Gson getGson() {
  107. return gson;
  108. }
  109. }