Haruyuki Mohri

毛利春幸のブログです。

C++Builder 10Seattle in bson How to use?

qiita.com I wrote how to use bson in delphi.

The following json to bson Converter in C++Builder 10 Seattle.

Specification of TBsonWriter has been added from 10 Seattle. So I used the TBsonWriter.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String output;
    TBytes arraybytes;
    TBytes array_out;
    std::shared_ptr<TStringReader> sr(new TStringReader(Memo1->Lines->Text));
    std::shared_ptr<TJsonTextReader> jt(new TJsonTextReader(sr.get()));

    std::shared_ptr<TBytesStream> bs(new TBytesStream(arraybytes));
    std::shared_ptr<TBsonWriter> bsonw(new TBsonWriter(bs.get()));
    try
    {
        bsonw->WriteToken(jt.get());
        array_out.set_length(bs->Size);
        bs->Position = 0;
        bs->Read(array_out, bs->Size);

        for (int i = 0; i < bs->Size; i++) {

            output += IntToHex(array_out[i], 2) + " ";
        }
        Memo2->Lines->Text = output;
    }
    catch(Exception &ex)
    {
    }
}

It will work in the iPhone.

f:id:mojeld:20150909182447j:plain

Reference

System.JSON.BSON - RAD Studio API Documentation