예제 #1
0
  @Test
  public void testUTF8() {
    for (int i = 1; i <= Character.MAX_CODE_POINT; i++) {

      if (!Character.isValidCodePoint(i)) continue;

      String orig = new String(Character.toChars(i));
      BSONObject a = new BasicBSONObject(orig, orig);
      BSONObject b = BSON.decode(BSON.encode(a));
      assertEquals(a, b);
    }
  }
예제 #2
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);
 }
예제 #3
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;
  }
예제 #5
0
  @Test
  @SuppressWarnings("unchecked")
  public void testCustomEncoders() throws IOException {
    // If clearEncodingHooks isn't working the first test will fail.
    Transformer tf = new TestDateTransformer();
    BSON.addEncodingHook(TestDate.class, tf);
    BSON.clearEncodingHooks();
    TestDate td = new TestDate(2009, 01, 23, 10, 53, 42);
    BSONObject o = new BasicBSONObject("date", td);
    BSONEncoder e = new BasicBSONEncoder();
    BSONDecoder d = new BasicBSONDecoder();
    BSONCallback cb = new BasicBSONCallback();
    OutputBuffer buf = new BasicOutputBuffer();
    e.set(buf);
    boolean encodeFailed = false;
    try {
      e.putObject(o);
    } catch (IllegalArgumentException ieE) {
      encodeFailed = true;
    }
    assertTrue(encodeFailed);
    // Reset the buffer
    buf.seekStart();
    assertTrue(tf.transform(td) instanceof java.util.Date);

    BSON.addEncodingHook(TestDate.class, tf);
    e.putObject(o);
    e.done();

    d.decode(new ByteArrayInputStream(buf.toByteArray()), cb);
    Object result = cb.get();
    assertTrue(result instanceof BSONObject);
    BSONObject bson = (BSONObject) result;
    assertNotNull(bson.get("date"));
    assertTrue(bson.get("date") instanceof java.util.Date);

    // Check that the hooks registered
    assertNotNull(BSON.getEncodingHooks(TestDate.class));
    Vector expect = new Vector(1);
    expect.add(tf);
    assertEquals(BSON.getEncodingHooks(TestDate.class), expect);
    assertTrue(BSON.getEncodingHooks(TestDate.class).contains(tf));
    BSON.removeEncodingHook(TestDate.class, tf);
    assertFalse(BSON.getEncodingHooks(TestDate.class).contains(tf));
  }
예제 #6
0
  @Test
  @SuppressWarnings({"deprecation", "unchecked"})
  public void testCustomDecoders() throws IOException {
    // If clearDecodingHooks isn't working this whole test will fail.
    Transformer tf = new TestDateTransformer();
    BSON.addDecodingHook(Date.class, tf);
    BSON.clearDecodingHooks();
    TestDate td = new TestDate(2009, 01, 23, 10, 53, 42);
    Date dt = new Date(2009, 01, 23, 10, 53, 42);
    BSONObject o = new BasicBSONObject("date", dt);
    BSONDecoder d = new BasicBSONDecoder();
    BSONEncoder e = new BasicBSONEncoder();
    BSONCallback cb = new BasicBSONCallback();
    OutputBuffer buf = new BasicOutputBuffer();
    e.set(buf);
    e.putObject(o);
    e.done();

    d.decode(new ByteArrayInputStream(buf.toByteArray()), cb);
    Object result = cb.get();
    assertTrue(result instanceof BSONObject);
    BSONObject bson = (BSONObject) result;
    assertNotNull(bson.get("date"));
    assertTrue(bson.get("date") instanceof java.util.Date);

    BSON.addDecodingHook(Date.class, tf);

    d.decode(new ByteArrayInputStream(buf.toByteArray()), cb);
    bson = (BSONObject) cb.get();
    assertNotNull(bson.get("date"));
    assertTrue(bson.get("date") instanceof TestDate);
    assertEquals(bson.get("date"), td);

    // Check that the hooks registered
    assertNotNull(BSON.getDecodingHooks(Date.class));
    Vector expect = new Vector(1);
    expect.add(tf);
    assertEquals(BSON.getDecodingHooks(Date.class), expect);
    assertTrue(BSON.getDecodingHooks(Date.class).contains(tf));
    BSON.removeDecodingHook(Date.class, tf);
    assertFalse(BSON.getDecodingHooks(Date.class).contains(tf));
  }
예제 #7
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());
    }
  }
예제 #8
0
 void _roundTrip(BSONObject o) {
   assertEquals(o, BSON.decode(BSON.encode(o)));
 }
  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;
  }
예제 #10
0
 protected Object getElementValue(ElementRecord record) {
   switch (record.type) {
     case BSON.EOO:
     case BSON.UNDEFINED:
     case BSON.NULL:
       return null;
     case BSON.MAXKEY:
       return new MaxKey();
     case BSON.MINKEY:
       return new MinKey();
     case BSON.BOOLEAN:
       return (_input.get(record.valueOffset) != 0);
     case BSON.NUMBER_INT:
       return _input.getInt(record.valueOffset);
     case BSON.TIMESTAMP:
       int inc = _input.getInt(record.valueOffset);
       int time = _input.getInt(record.valueOffset + 4);
       return new BSONTimestamp(time, inc);
     case BSON.DATE:
       return new Date(_input.getLong(record.valueOffset));
     case BSON.NUMBER_LONG:
       return _input.getLong(record.valueOffset);
     case BSON.NUMBER:
       return Double.longBitsToDouble(_input.getLong(record.valueOffset));
     case BSON.OID:
       return new ObjectId(
           _input.getIntBE(record.valueOffset),
           _input.getIntBE(record.valueOffset + 4),
           _input.getIntBE(record.valueOffset + 8));
     case BSON.SYMBOL:
       return new Symbol(_input.getUTF8String(record.valueOffset));
     case BSON.CODE:
       return new Code(_input.getUTF8String(record.valueOffset));
     case BSON.STRING:
       return _input.getUTF8String(record.valueOffset);
     case BSON.CODE_W_SCOPE:
       int strsize = _input.getInt(record.valueOffset + 4);
       String code = _input.getUTF8String(record.valueOffset + 4);
       BSONObject scope =
           (BSONObject)
               _callback.createObject(_input.array(), record.valueOffset + 4 + 4 + strsize);
       return new CodeWScope(code, scope);
     case BSON.REF:
       int csize = _input.getInt(record.valueOffset);
       String ns = _input.getCString(record.valueOffset + 4);
       int oidOffset = record.valueOffset + csize + 4;
       ObjectId oid =
           new ObjectId(
               _input.getIntBE(oidOffset),
               _input.getIntBE(oidOffset + 4),
               _input.getIntBE(oidOffset + 8));
       return _callback.createDBRef(ns, oid);
     case BSON.OBJECT:
       return _callback.createObject(_input.array(), record.valueOffset);
     case BSON.ARRAY:
       return _callback.createArray(_input.array(), record.valueOffset);
     case BSON.BINARY:
       return readBinary(record.valueOffset);
     case BSON.REGEX:
       int n = sizeCString(record.valueOffset);
       String pattern = _input.getCString(record.valueOffset);
       String flags = _input.getCString(record.valueOffset + n);
       return Pattern.compile(pattern, BSON.regexFlags(flags));
     default:
       throw new BSONException(
           "Invalid type " + record.type + " for field " + getElementFieldName(record.offset));
   }
 }