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

https://github.com/maneeshkumar/mongo-java-driver · Java · 145 lines · 115 code · 12 blank · 18 comment · 40 complexity · 9ff81d36082d9cfff10b352de0e80131 MD5 · raw file

  1. // JSONCallback.java
  2. /**
  3. * Copyright (C) 2008 10gen Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.mongodb.util;
  18. import java.text.ParseException;
  19. import java.text.ParsePosition;
  20. import java.text.SimpleDateFormat;
  21. import java.util.*;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import java.util.regex.Pattern;
  25. import org.bson.*;
  26. import org.bson.types.*;
  27. import com.mongodb.*;
  28. public class JSONCallback extends BasicBSONCallback {
  29. @Override
  30. public BSONObject create(){
  31. return new BasicDBObject();
  32. }
  33. @Override
  34. protected BSONObject createList() {
  35. return new BasicDBList();
  36. }
  37. public void objectStart(boolean array, String name){
  38. _lastArray = array;
  39. super.objectStart( array , name );
  40. }
  41. public Object objectDone(){
  42. String name = curName();
  43. Object o = super.objectDone();
  44. BSONObject b = (BSONObject)o;
  45. // override the object if it's a special type
  46. if ( ! _lastArray ) {
  47. if ( b.containsField( "$oid" ) ) {
  48. o = new ObjectId((String)b.get("$oid"));
  49. if (!isStackEmpty()) {
  50. gotObjectId( name, (ObjectId)o);
  51. } else {
  52. setRoot(o);
  53. }
  54. } else if ( b.containsField( "$date" ) ) {
  55. SimpleDateFormat format =
  56. new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  57. GregorianCalendar calendar = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
  58. format.setCalendar(calendar);
  59. String txtdate = (String) b.get("$date");
  60. o = format.parse(txtdate, new ParsePosition(0));
  61. if (o == null) {
  62. // try older format with no ms
  63. format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  64. format.setCalendar(calendar);
  65. o = format.parse(txtdate, new ParsePosition(0));
  66. }
  67. if (!isStackEmpty()) {
  68. cur().put( name, o );
  69. } else {
  70. setRoot(o);
  71. }
  72. } else if ( b.containsField( "$regex" ) ) {
  73. o = Pattern.compile( (String)b.get( "$regex" ),
  74. BSON.regexFlags( (String)b.get( "$options" )) );
  75. if (!isStackEmpty()) {
  76. cur().put( name, o );
  77. } else {
  78. setRoot(o);
  79. }
  80. } else if ( b.containsField( "$ts" ) ) {
  81. Long ts = ((Number)b.get("$ts")).longValue();
  82. Long inc = ((Number)b.get("$inc")).longValue();
  83. o = new BSONTimestamp(ts.intValue(), inc.intValue());
  84. if (!isStackEmpty()) {
  85. cur().put( name, o );
  86. } else {
  87. setRoot(o);
  88. }
  89. } else if ( b.containsField( "$code" ) ) {
  90. if (b.containsField("$scope")) {
  91. o = new CodeWScope((String)b.get("$code"), (DBObject)b.get("$scope"));
  92. } else {
  93. o = new Code((String)b.get("$code"));
  94. }
  95. if (!isStackEmpty()) {
  96. cur().put( name, o );
  97. } else {
  98. setRoot(o);
  99. }
  100. } else if ( b.containsField( "$ref" ) ) {
  101. o = new DBRef(null, (String)b.get("$ref"), b.get("$id"));
  102. if (!isStackEmpty()) {
  103. cur().put( name, o );
  104. } else {
  105. setRoot(o);
  106. }
  107. } else if ( b.containsField( "$minKey" ) ) {
  108. o = new MinKey();
  109. if (!isStackEmpty()) {
  110. cur().put( name, o );
  111. } else {
  112. setRoot(o);
  113. }
  114. } else if ( b.containsField( "$maxKey" ) ) {
  115. o = new MaxKey();
  116. if (!isStackEmpty()) {
  117. cur().put( name, o );
  118. } else {
  119. setRoot(o);
  120. }
  121. } else if ( b.containsField( "$uuid" ) ) {
  122. o = UUID.fromString((String)b.get("$uuid"));
  123. if (!isStackEmpty()) {
  124. cur().put( name, o );
  125. } else {
  126. setRoot(o);
  127. }
  128. }
  129. }
  130. return o;
  131. }
  132. private boolean _lastArray = false;
  133. }