Esempio n. 1
0
 public int getInt(final String key, final int def) {
   final Object foo = this.get(key);
   if (foo == null) {
     return def;
   }
   return BSON.toInt(foo);
 }
Esempio n. 2
0
 public int getInt(final String key) {
   final Object o = this.get(key);
   if (o == null) {
     throw new NullPointerException("no value for: " + key);
   }
   return BSON.toInt(o);
 }
  public Object objectDone() {
    Object o = super.objectDone();
    BSONObject b = (BSONObject) o;

    if (!_lastArray) {
      if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
        if (!isStackEmpty()) {
          gotObjectId(_lastName, (ObjectId) o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$date")) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        o = format.parse((String) b.get("$date"), new ParsePosition(0));
        if (!isStackEmpty()) {
          cur().put(_lastName, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
        if (!isStackEmpty()) {
          cur().put(_lastName, o);
        } else {
          setRoot(o);
        }
      }
    }

    return o;
  }
Esempio n. 4
0
  protected void _putObjectField(String name, Object val) {

    if (name.equals("_transientFields")) return;

    if (DEBUG) System.out.println("\t put thing : " + name);

    if (name.equals("$where") && val instanceof String) {
      _put(CODE, name);
      _putValueString(val.toString());
      return;
    }

    val = BSON.applyEncodingHooks(val);

    if (val == null) putNull(name);
    else if (val instanceof Date) putDate(name, (Date) val);
    else if (val instanceof Number) putNumber(name, (Number) val);
    else if (val instanceof String) putString(name, val.toString());
    else if (val instanceof ObjectId) putObjectId(name, (ObjectId) val);
    else if (val instanceof BSONObject) putObject(name, (BSONObject) val);
    else if (val instanceof Boolean) putBoolean(name, (Boolean) val);
    else if (val instanceof Pattern) putPattern(name, (Pattern) val);
    else if (val instanceof Map) putMap(name, (Map) val);
    else if (val instanceof Iterable) putIterable(name, (Iterable) val);
    else if (val instanceof byte[]) putBinary(name, (byte[]) val);
    else if (val instanceof Binary) putBinary(name, (Binary) val);
    else if (val instanceof UUID) putUUID(name, (UUID) val);
    else if (val.getClass().isArray()) putArray(name, val);
    else if (val instanceof Symbol) {
      putSymbol(name, (Symbol) val);
    } else if (val instanceof BSONTimestamp) {
      putTimestamp(name, (BSONTimestamp) val);
    } else if (val instanceof CodeWScope) {
      putCodeWScope(name, (CodeWScope) val);
    } else if (val instanceof Code) {
      putCode(name, (Code) val);
    } else if (putSpecial(name, val)) {
      // no-op
    } else {
      throw new IllegalArgumentException("can't serialize " + val.getClass());
    }
  }
  public Object objectDone() {
    String name = curName();
    Object o = super.objectDone();
    BSONObject b = (BSONObject) o;

    // override the object if it's a special type
    if (!_lastArray) {
      if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
        if (!isStackEmpty()) {
          gotObjectId(name, (ObjectId) o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$date")) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        GregorianCalendar calendar = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
        format.setCalendar(calendar);
        String txtdate = (String) b.get("$date");
        o = format.parse(txtdate, new ParsePosition(0));
        if (o == null) {
          // try older format with no ms
          format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
          format.setCalendar(calendar);
          o = format.parse(txtdate, new ParsePosition(0));
        }
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$ts")) {
        Long ts = ((Number) b.get("$ts")).longValue();
        Long inc = ((Number) b.get("$inc")).longValue();
        o = new BSONTimestamp(ts.intValue(), inc.intValue());
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$code")) {
        if (b.containsField("$scope")) {
          o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
        } else {
          o = new Code((String) b.get("$code"));
        }
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$ref")) {
        o = new DBRef(null, (String) b.get("$ref"), b.get("$id"));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$minKey")) {
        o = new MinKey();
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$maxKey")) {
        o = new MaxKey();
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      } else if (b.containsField("$uuid")) {
        o = UUID.fromString((String) b.get("$uuid"));
        if (!isStackEmpty()) {
          cur().put(name, o);
        } else {
          setRoot(o);
        }
      }
    }
    return o;
  }