Есть строка в формате BSON (закодированная в base64), как раскодировать ее в JSON-строку?
Строка в таком виде
ugEAAAMwAGsAAAACX2lkAAsAAAAxNUdaczF4UWt3AAJVc2VyQnVpbGQAJQAAADRlMTI3VzEwN3M2N1MxYjc4ZjEwM1U5MHkzN3cxMDBlMTE5ZwAIVXNlckZ1bGxBY2Nlc3MAAQhVc2VyQ2FuQWRkAAAAAzEAagAAAAJfaWQACwAAADV2QlV2ZWdsVm4AAlVzZXJCdWlsZAAkAAAANzF6MTIySjEwM1Y2OFI4MmU4M2o4NWI5OHE2MG45Nms1MloACFVzZXJGdWxsQWNjZXNzAAAIVXNlckNhbkFkZAAAAAMyAGoAAAACX2lkAAsAAAAwWk81anpwb3MzAAhVc2VyRnVsbEFjY2VzcwABAlVzZXJCdWlsZAAkAAAAMTA3TTEyMEwxUDg4WjY3czczQTk0ZDcwRTk3ajEwMWsyNEUACFVzZXJDYW5BZGQAAAADMwBqAAAAAl9pZAALAAAAb0prbDhyZk9GbwACVXNlckJ1aWxkACQAAAAxMjNXODNkOTRpMTRPNjVMOTRFMTI1VTExM3Y2MXU1OFQ4VQAIVXNlckZ1bGxBY2Nlc3MAAQhVc2VyQ2FuQWRkAAAAAA==
В документации сказано, что это:
bson результат запроса, закодированный в base64
Раскодировать base64 пробовал так
function Base64Decode(S: string): string;
var
i, a, b, X: Integer;
begin
a := 0;
b := 0;
for i := 1 to Length(S) do
begin
X := pos(S[i], Cod) - 1;
if X >= 0 then
begin
b := b * 64 + X;
inc(a, 6);
if a >= 8 then
begin
dec(a, 8);
X := b shr a;
b := b mod (1 shl a);
X := X mod 256;
Result := Result + chr(X);
end;
end
else
Exit;
end;
end;
После такого раскодировывания получилась строка
'â£'#$008A'(¥º)'#$D#$008A'(¢'#$008C#$0084'+î('#$00AD#$008A'(¢»]éä'#$009B'ÓÄz'#$D'Ã(¤ß'#$0098'''ük'#$17'ó'#$0095#6#$008A'M¢'#$008A'(ÖïYÔ'#$0083#$7F'Óº\5'#$0082'\'#$15'»'#$008A'Ù'#$0084#$008E'ÓºX'#$17#$0085'Y'#$1B'½\9»XÒïYÓ'#$0085#$008F'¢'#$0092'~'#$15'ï'#$009A'èÁ'#$0095#$E'Ë'#$0088'%ï'#$0098'5'#$008A')ªß'#$0098'''üh#øj'#6'î(¢'#$008A'+Ó'#$008A#$0092'¢'#$008A'(¤é'#$0092#6#$008A'3¢'#$008A'(×Âj÷Â'#$008E#9'ö'#$7F#1#$A'*÷ý'#$008E#$14'Ì'#$009E#$B'ö'#$008C'¢®(¢'#$008A'_ÓÆYÔ¼rÓºX'#$18#$0082'`ô'#$0084'['#7#$0084'X,'#$0084'^'#4#$0085'a'#$13#$0082'Y'#1#5'_'#$D#$0081'Zü'#$008A'0÷ý'#$008E#$14'Ð'#$009E#$E'öj'#5'í'#$008E#$15'ý(¢'#$0092'~'#$15'ï'#$009A'åë'#$0096'ãî'#$008C'¢'#$008A'(¥¼)'#$C#$008A'(¢'#$008C#$0084'+î('#$00AD#$008A'(¢º'#$0082'â'#1#$0093#$1C'ú'#$0094'5½(ªß'#$0098'''üo'#$17'ö'#$0094'ãí'#$0088'''ý'#$009B'¢'#$008B'*÷ý'#$008E#$14'Ì'#$009E#$B'ö'#$008C'¢®(¢'#$008A'YÒ'#$0083'uÓ¼Xî»xÚ'#$0084#$0082'Ø'#$0083#$009B'Ù½iÛ'#$0080#$008C'ÙºmÛ'#$0083#$0092'ÓºZ'#$D'¼\ç'#$008A'0÷ý'#$008E#$14'Í'#$008A#1'K'#$008D#6#$008A'(¢'#$008D'[¢ô(¢'#$008A'*òs'#$008C'¢'#$0095'(¢'#$008A#$0094#$C'õ'#$0094'Úü'#$008E'âP'#$0097'¢'#$008C'~'#$15'ï'#$009A'äÁ'#$0092#$E'î(Æ'#$008A'(¢»ZÕá`ÕîaÖóYÖÙ^×ÖaÖÏYÔ'#$0081'}Ó»X8'#$0082'Z'#$17#$0081'`ö'#$0084'}¢'#$0092'~'#$15'ï'#$009A'èÁ'#$0095#$E'Ë'#$0088'%ï'#$0098'5'#$008A')ªß'#$0098'''üh#øj'#6'î(¢'#$008A'('
Ответ
Это можно сделать вот так:
uses
System.NetEncoding;
...
var
VBson: string;
VJson: string;
begin
VBson := 'ugEAAAMwAGsAAA...
VJson := Bson2Json(TNetEncoding.Base64.DecodeStringToBytes(VBson));
Writeln(VJson);
end;
За декодирование BSON отвечает следующий код (взят из этих сорцов):
uses
Classes,
System.JSON,
System.JSON.BSON,
System.JSON.Writers,
System.SysUtils;
type
TJsonStringWriter = class(TJsonTextWriter)
private
FStrinBuilder: TStringBuilder;
FStringWriter: TStringWriter;
public
constructor Create;
destructor Destroy; override;
function ToString: string;
end;
{ TJsonStringWriter }
constructor TJsonStringWriter.Create;
begin
FStrinBuilder := TStringBuilder.Create;
FStringWriter := TStringWriter.Create(FStrinBuilder);
inherited Create(FStringWriter);
end;
destructor TJsonStringWriter.Destroy;
begin
FStringWriter.Free;
FStrinBuilder.Free;
inherited Destroy;
end;
function TJsonStringWriter.ToString: string;
begin
Result := FStrinBuilder.ToString;
end;
function Bson2Json(const ABytes: TBytes): string;
var
JsonWriter: TJsonStringWriter;
BsonReader: TBsonReader;
Stream: TBytesStream;
begin
Stream := TBytesStream.Create(ABytes);
BsonReader := TBsonReader.Create(Stream);
JsonWriter := TJsonStringWriter.Create;
try
JsonWriter.WriteToken(BsonReader);
Result := JsonWriter.ToString;
finally
JsonWriter.Free;
BsonReader.Free;
Stream.Free;
end;
end;
Комментариев нет:
Отправить комментарий