private static BasicBSONObject canonicalizeBSONObject(final BSONObject from) {
   final BasicBSONObject canonicalized = new BasicBSONObject();
   final TreeSet<String> keysInOrder = new TreeSet<String>(from.keySet());
   for (final String key : keysInOrder) {
     final Object val = from.get(key);
     canonicalized.put(key, canonicalize(val));
   }
   return canonicalized;
 }
 public boolean equals(final Object o) {
   if (o == this) {
     return true;
   }
   if (!(o instanceof BSONObject)) {
     return false;
   }
   final BSONObject other = (BSONObject) o;
   return ((HashMap<String, V>) this).keySet().equals(other.keySet())
       && Arrays.equals(
           canonicalizeBSONObject(this).encode(), canonicalizeBSONObject(other).encode());
 }
 public void putAll(final BSONObject o) {
   for (final String k : o.keySet()) {
     this.put(k, o.get(k));
   }
 }
  /** this is really for embedded objects */
  int putObject(String name, BSONObject o) {

    if (o == null) throw new NullPointerException("can't save a null object");

    if (DEBUG)
      System.out.println(
          "putObject : " + name + " [" + o.getClass() + "]" + " # keys " + o.keySet().size());

    final int start = _buf.getPosition();

    byte myType = OBJECT;
    if (o instanceof List) myType = ARRAY;

    if (handleSpecialObjects(name, o)) return _buf.getPosition() - start;

    if (name != null) {
      _put(myType, name);
    }

    final int sizePos = _buf.getPosition();
    _buf.writeInt(0); // leaving space for this.  set it at the end

    List transientFields = null;
    boolean rewriteID = myType == OBJECT && name == null;

    if (myType == OBJECT) {
      if (rewriteID && o.containsField("_id")) _putObjectField("_id", o.get("_id"));

      {
        Object temp = o.get("_transientFields");
        if (temp instanceof List) transientFields = (List) temp;
      }
    }

    // TODO: reduce repeated code below.
    if (o instanceof Map) {
      for (Entry<String, Object> e : ((Map<String, Object>) o).entrySet()) {

        if (rewriteID && e.getKey().equals("_id")) continue;

        if (transientFields != null && transientFields.contains(e.getKey())) continue;

        _putObjectField(e.getKey(), e.getValue());
      }
    } else {
      for (String s : o.keySet()) {

        if (rewriteID && s.equals("_id")) continue;

        if (transientFields != null && transientFields.contains(s)) continue;

        Object val = o.get(s);

        _putObjectField(s, val);
      }
    }
    _buf.write(EOO);

    _buf.writeInt(sizePos, _buf.getPosition() - sizePos);
    return _buf.getPosition() - start;
  }