Bson library for Delphi?

别说谁变了你拦得住时间么 提交于 2019-12-03 07:22:47

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!