BSON

提供: RAD Studio
移動先: 案内検索

マルチデバイス アプリケーションでの RTL の使用 への移動


BSON(バイナリ JSON)は、JSON 風のドキュメントをバイナリ形式でエンコードつまり表現するためのオープン標準です。RAD Studio には、BSON をシリアル化/逆シリアル化するためのクラスやメソッドを含んだユニット System.JSON.BSON が用意されています。

すべての BSON 型がサポートされています。さまざまな型の詳細については、System.JSON.BSON.TBsonType の API ドキュメントや BSON 公式サイトを参照してください。

BSON フレームワーク

RAD Studio には、Seattle バージョン以降、BSON データを扱う新しい API が組み込まれています。

新しいフレームワークには、BSON データを読み書きするためのクラス TBsonReader および TBsonWriter が含まれています。

TBsonWriter を使用すると、JSON テキストを BSON にシリアル化できます。WriteToken メソッドを使用すると、JSON オブジェクトを調べてトークンごとにシリアル化することができます。

TBsonReader を使用すると、BSON データを読み取り、BSON を JSON または他の形式に逆シリアル化することができます。

BSON の作成

以下のサンプル コードでは次の JSON オブジェクトが使用されます。

{				
  "colors": [		
    {			
      "name": "red",	
      "hex": "#f00"	
    }			
  ]								
}

BSON ドキュメントを直接作成するには、次のようにします。

uses 

System.JSON.BSON,
System.JSON.Writers,
System.JSON.Builders;

procedure BSONWriterSample;
var
  Builder: TJSONObjectBuilder; //To use the built-in JSON builder 
  BytesStream: TBytesStream;  //To create the BSON document as a byte stream
  Writer: TBsonWriter; //A BSON writer
  Bytes: TBytes; //To contain the bytes from the byte stream
begin
  BytesStream := TBytesStream.Create;
  Writer := TBsonWriter.Create(BytesStream);
  Builder := TJSONObjectBuilder.Create(Writer);
  try
    Builder
     .BeginObject
      .BeginArray('colors')
       .BeginObject
        .Add('name','red')
        .Add('hex','#f00')
     .EndAll;

    Bytes := BytesStream.Bytes;
    SetLength(Bytes, BytesStream.Size); //Bytes contains the BSON document.
    Memo1.text := Bytes2String(Bytes); //To see the representation of the BSON document in a TMemo, passing the Bytes var that contains the BSON document.
  finally
    BytesStream.free;
    Writer.free;
    Builder.free;  
  end;
end;

用意された JSON オブジェクトを BSON ライタを使って書き込んだ結果を 16 進表記で表したものが、次の BSON ドキュメントです。

36-00-00-00-04-63-6F-6C-6F-72-73-00-29-00-00-00-03-30-00-21-00-00-00-02-6E-61-6D-65-00-04-00-00-00-72-65-64-00-02-68-65-78-00-05-00-00-00-23-66-30-30-00-00-00-00

BSON ドキュメントの 16 進表現を取得するには、次のような関数を使用できます。

function Bytes2String(const ABytes: TBytes): string;
var
  I: Integer;
begin
  Result := '';
  for I := Low(ABytes) to High(ABytes) do
  if I = 0 then
    Result := IntToHex(ABytes[I], 2)
  else
    Result := Result + '-' + IntToHex(ABytes[I], 2);
end;

JSON から BSON への変換

以下のサンプル コードでは次の JSON オブジェクトが使用されます。

{				
  "colors": [		
    {			
      "name": "red",	
      "hex": "#f00"	
    }			
  ]								
}

JSON オブジェクトを BSON にシリアル化するには、次のようにします。

JSON オブジェクトは TMemo インスタンスにコピーされています。

uses 

System.JSON.BSON,
System.JSON.Writers,
System.JSON.Readers,
System.JSON.Builders;

procedure JSONtoBSON;
var
  SR: TStringReader; //A String Reader
  JsonReader: TJsonTextReader; //A JSON text Reader
  BsonWriter: TBsonWriter; // A BSON writer
  Stream: TBytesStream; //To create the BSON document as a byte stream
  Bytes: TBytes; //To contain the bytes from the byte stream
begin
  SR :=  TStringReader.Create(Memo1.Text); //It gets the JSON object as a string from a TMemo.
  JsonReader := TJsonTextReader.Create(SR);
  Stream := TBytesStream.Create;
  BsonWriter := TBsonWriter.Create(Stream);
  try
    BsonWriter.WriteToken(JsonReader); //It gets the JSON object as an argument and analize the object token by token.
    Stream.Position := 0;
    Bytes := Stream.Bytes;
    SetLength(Bytes, Stream.Size);
    Memo2.Text := Bytes2String(Bytes); //It shows the String representation of the BSON document in a TMemo.
  finally
    SR.free;
    JsonReader.free;
    Stream.free;
    BsonWriter.free;	  
  end;
end;

用意された JSON オブジェクトをシリアル化した結果を 16 進表記で表したものが、次の BSON ドキュメントです。

36-00-00-00-04-63-6F-6C-6F-72-73-00-29-00-00-00-03-30-00-21-00-00-00-02-6E-61-6D-65-00-04-00-00-00-72-65-64-00-02-68-65-78-00-05-00-00-00-23-66-30-30-00-00-00-00
メモfunction Bytes2String(const ABytes: TBytes): string; の関数定義については、上記を参照してください。

関連項目