/** 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; }