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

https://github.com/philwills/mongo-java-driver · Java · 118 lines · 105 code · 11 blank · 2 comment · 38 complexity · 328be1d006d70953782df99928c16e45 MD5 · raw file

  1. // JSONCallback.java
  2. package com.mongodb.util;
  3. import java.text.ParsePosition;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6. import java.util.regex.Pattern;
  7. import org.bson.*;
  8. import org.bson.types.*;
  9. import com.mongodb.*;
  10. public class JSONCallback extends BasicBSONCallback {
  11. @Override
  12. public BSONObject create(){
  13. return new BasicDBObject();
  14. }
  15. @Override
  16. protected BSONObject createList() {
  17. return new BasicDBList();
  18. }
  19. public void objectStart(boolean array, String name){
  20. _lastArray = array;
  21. super.objectStart( array , name );
  22. }
  23. public Object objectDone(){
  24. String name = curName();
  25. Object o = super.objectDone();
  26. BSONObject b = (BSONObject)o;
  27. // override the object if it's a special type
  28. if ( ! _lastArray ) {
  29. if ( b.containsField( "$oid" ) ) {
  30. o = new ObjectId((String)b.get("$oid"));
  31. if (!isStackEmpty()) {
  32. gotObjectId( name, (ObjectId)o);
  33. } else {
  34. setRoot(o);
  35. }
  36. } else if ( b.containsField( "$date" ) ) {
  37. SimpleDateFormat format =
  38. new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  39. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  40. o = format.parse((String)b.get("$date"), new ParsePosition(0));
  41. if (!isStackEmpty()) {
  42. cur().put( name, o );
  43. } else {
  44. setRoot(o);
  45. }
  46. } else if ( b.containsField( "$regex" ) ) {
  47. o = Pattern.compile( (String)b.get( "$regex" ),
  48. BSON.regexFlags( (String)b.get( "$options" )) );
  49. if (!isStackEmpty()) {
  50. cur().put( name, o );
  51. } else {
  52. setRoot(o);
  53. }
  54. } else if ( b.containsField( "$ts" ) ) {
  55. Long ts = ((Number)b.get("$ts")).longValue();
  56. Long inc = ((Number)b.get("$inc")).longValue();
  57. o = new BSONTimestamp(ts.intValue(), inc.intValue());
  58. if (!isStackEmpty()) {
  59. cur().put( name, o );
  60. } else {
  61. setRoot(o);
  62. }
  63. } else if ( b.containsField( "$code" ) ) {
  64. if (b.containsField("$scope")) {
  65. o = new CodeWScope((String)b.get("$code"), (DBObject)b.get("$scope"));
  66. } else {
  67. o = new Code((String)b.get("$code"));
  68. }
  69. if (!isStackEmpty()) {
  70. cur().put( name, o );
  71. } else {
  72. setRoot(o);
  73. }
  74. } else if ( b.containsField( "$ref" ) ) {
  75. o = new DBRef(null, (String)b.get("$ref"), b.get("$id"));
  76. if (!isStackEmpty()) {
  77. cur().put( name, o );
  78. } else {
  79. setRoot(o);
  80. }
  81. } else if ( b.containsField( "$minKey" ) ) {
  82. o = new MinKey();
  83. if (!isStackEmpty()) {
  84. cur().put( name, o );
  85. } else {
  86. setRoot(o);
  87. }
  88. } else if ( b.containsField( "$maxKey" ) ) {
  89. o = new MaxKey();
  90. if (!isStackEmpty()) {
  91. cur().put( name, o );
  92. } else {
  93. setRoot(o);
  94. }
  95. } else if ( b.containsField( "$uuid" ) ) {
  96. o = UUID.fromString((String)b.get("$uuid"));
  97. if (!isStackEmpty()) {
  98. cur().put( name, o );
  99. } else {
  100. setRoot(o);
  101. }
  102. }
  103. }
  104. return o;
  105. }
  106. private boolean _lastArray = false;
  107. }