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

https://github.com/dixcychaitanya/mongo-java-driver · Java · 154 lines · 124 code · 12 blank · 18 comment · 40 complexity · 37aa40982fe790b28db4d059f70fd83b 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.ParsePosition;
  19. import java.text.SimpleDateFormat;
  20. import java.util.GregorianCalendar;
  21. import java.util.SimpleTimeZone;
  22. import java.util.UUID;
  23. import java.util.regex.Pattern;
  24. import org.bson.BSON;
  25. import org.bson.BSONObject;
  26. import org.bson.BasicBSONCallback;
  27. import org.bson.types.BSONTimestamp;
  28. import org.bson.types.Code;
  29. import org.bson.types.CodeWScope;
  30. import org.bson.types.MaxKey;
  31. import org.bson.types.MinKey;
  32. import org.bson.types.ObjectId;
  33. import com.mongodb.BasicDBList;
  34. import com.mongodb.BasicDBObject;
  35. import com.mongodb.DBObject;
  36. import com.mongodb.DBRef;
  37. public class JSONCallback extends BasicBSONCallback {
  38. @Override
  39. public BSONObject create(){
  40. return new BasicDBObject();
  41. }
  42. @Override
  43. protected BSONObject createList() {
  44. return new BasicDBList();
  45. }
  46. public void objectStart(boolean array, String name){
  47. _lastArray = array;
  48. super.objectStart( array , name );
  49. }
  50. public Object objectDone(){
  51. String name = curName();
  52. Object o = super.objectDone();
  53. BSONObject b = (BSONObject)o;
  54. // override the object if it's a special type
  55. if ( ! _lastArray ) {
  56. if ( b.containsField( "$oid" ) ) {
  57. o = new ObjectId((String)b.get("$oid"));
  58. if (!isStackEmpty()) {
  59. gotObjectId( name, (ObjectId)o);
  60. } else {
  61. setRoot(o);
  62. }
  63. } else if ( b.containsField( "$date" ) ) {
  64. SimpleDateFormat format =
  65. new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  66. GregorianCalendar calendar = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
  67. format.setCalendar(calendar);
  68. String txtdate = (String) b.get("$date");
  69. o = format.parse(txtdate, new ParsePosition(0));
  70. if (o == null) {
  71. // try older format with no ms
  72. format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  73. format.setCalendar(calendar);
  74. o = format.parse(txtdate, new ParsePosition(0));
  75. }
  76. if (!isStackEmpty()) {
  77. cur().put( name, o );
  78. } else {
  79. setRoot(o);
  80. }
  81. } else if ( b.containsField( "$regex" ) ) {
  82. o = Pattern.compile( (String)b.get( "$regex" ),
  83. BSON.regexFlags( (String)b.get( "$options" )) );
  84. if (!isStackEmpty()) {
  85. cur().put( name, o );
  86. } else {
  87. setRoot(o);
  88. }
  89. } else if ( b.containsField( "$ts" ) ) {
  90. Long ts = ((Number)b.get("$ts")).longValue();
  91. Long inc = ((Number)b.get("$inc")).longValue();
  92. o = new BSONTimestamp(ts.intValue(), inc.intValue());
  93. if (!isStackEmpty()) {
  94. cur().put( name, o );
  95. } else {
  96. setRoot(o);
  97. }
  98. } else if ( b.containsField( "$code" ) ) {
  99. if (b.containsField("$scope")) {
  100. o = new CodeWScope((String)b.get("$code"), (DBObject)b.get("$scope"));
  101. } else {
  102. o = new Code((String)b.get("$code"));
  103. }
  104. if (!isStackEmpty()) {
  105. cur().put( name, o );
  106. } else {
  107. setRoot(o);
  108. }
  109. } else if ( b.containsField( "$ref" ) ) {
  110. o = new DBRef(null, (String)b.get("$ref"), b.get("$id"));
  111. if (!isStackEmpty()) {
  112. cur().put( name, o );
  113. } else {
  114. setRoot(o);
  115. }
  116. } else if ( b.containsField( "$minKey" ) ) {
  117. o = new MinKey();
  118. if (!isStackEmpty()) {
  119. cur().put( name, o );
  120. } else {
  121. setRoot(o);
  122. }
  123. } else if ( b.containsField( "$maxKey" ) ) {
  124. o = new MaxKey();
  125. if (!isStackEmpty()) {
  126. cur().put( name, o );
  127. } else {
  128. setRoot(o);
  129. }
  130. } else if ( b.containsField( "$uuid" ) ) {
  131. o = UUID.fromString((String)b.get("$uuid"));
  132. if (!isStackEmpty()) {
  133. cur().put( name, o );
  134. } else {
  135. setRoot(o);
  136. }
  137. }
  138. }
  139. return o;
  140. }
  141. private boolean _lastArray = false;
  142. }