I ran into the same problem and cracked the source code a bit. Here is what I prepared then:
procedure TBSONDocument.ReadStream(F: TStream); var len : Integer; elmtype : Byte; elmname : string; begin Clear; F.Read(len, SizeOf(len)); F.Read(elmtype, SizeOf(Byte) ); while elmtype <> BSON_EOF do begin elmname := _ReadString(F); SetLength(FItems, Length(FItems)+1); case elmtype of BSON_FLOAT: FItems[High(FItems)] := TBSONDoubleItem.Create; BSON_STRING: FItems[High(FItems )] := TBSONStringItem.Create; BSON_DOC: FItems[High(FItems )] := TBSONDocumentItem.Create; // Mrsky BSON_ARRAY: FItems[High(FItems)] := TBSONArrayItem.Create; BSON_BINARY: FItems[High(FItems)] := TBSONBinaryItem.Create; BSON_OBJECTID: FItems[High(FItems )] := TBSONObjectIDItem.Create; BSON_BOOLEAN: FItems[High(FItems )] := TBSONBooleanItem.Create; BSON_DATETIME: FItems[High(FItems)] := TBSONDatetimeItem.Create(0); // Mrsky BSON_REGEX: FItems[High(FItems)] := TBSONRegExItem.Create; BSON_DBPTR: FItems[High(FItems)] := TBSONDBRefItem.Create; BSON_JS: FItems[High(FItems )] := TBSONJSItem.Create; BSON_SYMBOL: FItems[High(FItems)] := TBSONSymbolItem.Create; BSON_JSSCOPE: FItems[High(FItems )] := TBSONScopedJSItem.Create; BSON_INT32: FItems[High(FItems )] := TBSONIntItem.Create; BSON_INT64: FItems[High(FItems )] := TBSONInt64Item.Create; BSON_MINKEY: FItems[High(FItems)] := TBSONItem.Create(BSON_MINKEY); BSON_MAXKEY: FItems[High(FItems)] := TBSONItem.Create(BSON_MAXKEY); else raise EBSONException.Create('Unimplemented element handler '+IntToStr(elmtype)); end; with FItems[High(FItems)] do begin elname := elmname; ReadStream(f); end; f.Read(elmtype, SizeOf(Byte)); end; end;
I did not appreciate how the βFreeβ method was implemented, and removed all of them and introduced a new Destroy method of the destructor, where it fits.
Hope this helps you.
user955050
source share