/src/main/com/mongodb/util/JSONCallback.java

http://github.com/mongodb/mongo-java-driver · Java · 134 lines · 102 code · 14 blank · 18 comment · 32 complexity · 249824b719233d5d7b8140834235010d MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 MongoDB, Inc.
  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. // JSONCallback.java
  17. package com.mongodb.util;
  18. import com.mongodb.BasicDBList;
  19. import com.mongodb.BasicDBObject;
  20. import com.mongodb.DBObject;
  21. import com.mongodb.DBRef;
  22. import org.bson.BSON;
  23. import org.bson.BSONObject;
  24. import org.bson.BasicBSONCallback;
  25. import org.bson.types.BSONTimestamp;
  26. import org.bson.types.Binary;
  27. import org.bson.types.Code;
  28. import org.bson.types.CodeWScope;
  29. import org.bson.types.MaxKey;
  30. import org.bson.types.MinKey;
  31. import org.bson.types.ObjectId;
  32. import java.text.ParsePosition;
  33. import java.text.SimpleDateFormat;
  34. import java.util.Date;
  35. import java.util.GregorianCalendar;
  36. import java.util.SimpleTimeZone;
  37. import java.util.UUID;
  38. import java.util.regex.Pattern;
  39. public class JSONCallback extends BasicBSONCallback {
  40. @Override
  41. public BSONObject create() {
  42. return new BasicDBObject();
  43. }
  44. @Override
  45. protected BSONObject createList() {
  46. return new BasicDBList();
  47. }
  48. public void objectStart(boolean array, String name) {
  49. _lastArray = array;
  50. super.objectStart(array, name);
  51. }
  52. public Object objectDone() {
  53. String name = curName();
  54. Object o = super.objectDone();
  55. if (_lastArray) {
  56. return o;
  57. }
  58. BSONObject b = (BSONObject) o;
  59. // override the object if it's a special type
  60. if (b.containsField("$oid")) {
  61. o = new ObjectId((String) b.get("$oid"));
  62. } else if (b.containsField("$date")) {
  63. if (b.get("$date") instanceof Number) {
  64. o = new Date(((Number) b.get("$date")).longValue());
  65. } else {
  66. SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
  67. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  68. o = format.parse(b.get("$date").toString(), new ParsePosition(0));
  69. if (o == null) {
  70. // try older format with no ms
  71. format = new SimpleDateFormat(_secDateFormat);
  72. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  73. o = format.parse(b.get("$date").toString(), new ParsePosition(0));
  74. }
  75. }
  76. } else if (b.containsField("$regex")) {
  77. o = Pattern.compile((String) b.get("$regex"),
  78. BSON.regexFlags((String) b.get("$options")));
  79. } else if (b.containsField("$ts")) { //Legacy timestamp format
  80. Integer ts = ((Number) b.get("$ts")).intValue();
  81. Integer inc = ((Number) b.get("$inc")).intValue();
  82. o = new BSONTimestamp(ts, inc);
  83. } else if (b.containsField("$timestamp")) {
  84. BSONObject tsObject = (BSONObject) b.get("$timestamp");
  85. Integer ts = ((Number) tsObject.get("t")).intValue();
  86. Integer inc = ((Number) tsObject.get("i")).intValue();
  87. o = new BSONTimestamp(ts, inc);
  88. } else if (b.containsField("$code")) {
  89. if (b.containsField("$scope")) {
  90. o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
  91. } else {
  92. o = new Code((String) b.get("$code"));
  93. }
  94. } else if (b.containsField("$ref")) {
  95. o = new DBRef(null, (String) b.get("$ref"), b.get("$id"));
  96. } else if (b.containsField("$minKey")) {
  97. o = new MinKey();
  98. } else if (b.containsField("$maxKey")) {
  99. o = new MaxKey();
  100. } else if (b.containsField("$uuid")) {
  101. o = UUID.fromString((String) b.get("$uuid"));
  102. } else if (b.containsField("$binary")) {
  103. int type = (Integer) b.get("$type");
  104. byte[] bytes = (new Base64Codec()).decode((String) b.get("$binary"));
  105. o = new Binary((byte) type, bytes);
  106. } else if (b.containsField("$numberLong")) {
  107. o = Long.valueOf((String) b.get("$numberLong"));
  108. }
  109. if (!isStackEmpty()) {
  110. _put(name, o);
  111. } else {
  112. o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks( o );
  113. setRoot(o);
  114. }
  115. return o;
  116. }
  117. private boolean _lastArray = false;
  118. public static final String _msDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
  119. public static final String _secDateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  120. }