Rodrigo Farias Rezino
Rodrigo Farias Rezino

Reputation: 2799

Bson library for Delphi?

Can someone suggest a complete Bson library for Delphi ? I'm trying to use http://code.google.com/p/pebongo/source/browse/trunk/_bson.pas from http://bsonspec.org, but there are some structures that aren't supported.


Or maybe I'm not using it correctly, like this class doesn't have documentation I can not find the correct usage for it.

I want to create a list of items, this items are my serializable objects.
But how to create a list and put item on a "list" ?

Upvotes: 7

Views: 1740

Answers (2)

user955050
user955050

Reputation:

I came across the same trouble and hacked the source code a bit. Here's what i cooked 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 didn't appreciate the way "Free" method was implemented and remove all of them and introduced new "Destroy" destructor method accordingly where it fit.

I hope it will be of some help for you.

Upvotes: 1

Stijn Sanders
Stijn Sanders

Reputation: 36840

I've created a BSON implementation for Delphi before, heavily based on the existing Variant type in Delphi (and its TVarType). It also supports variant-arrays to some point.

see bsonDoc.pas: https://github.com/stijnsanders/TMongoWire

Upvotes: 7

Related Questions