I'm basically searching for a way to convert mongo JSON back to a variant. Mainly the opposite of VariantSaveMongoJson.
The best I found so far was the combination of Bson and BsonDocumentToDoc which seemed to be working except the loss of the binary data.
]]>It is likely that BsonDocumentToDoc() change the type of the BLOB.
As it is expected for a TDocVariant.
I already tried GetVarData instead of GetValueOrNull but the result remains the same.
I couldn't yet find a way to convert a DocVariantType into a BsonVariantType and I'm wondering how TRestStorageMongoDB.RetrieveBlobFields accomplishes this.
]]>So the "Test" binary is transformed by TBsonElement.ToVariant into a BsonVariantType holding a betBinary RawByteString buffer.
Normally, GetValueOrNull() should call SetVariantByValue() and BV should be back to a TBsonVariant.
Try to debug to find what is wrong.
]]>Example code:
var
BV,V: Variant;
JSON: String;
RBS : RawByteString;
begin
TDocVariant.New(V);
RBS:=#1#2#3;
BsonVariantType.FromBinary(RBS,bbtGeneric,BV);
TDocVariantData(V).AddValue('Test',BV);
JSON:=VariantSaveMongoJson(V,modMongoStrict);
V:=BsonDocumentToDoc(Bson(JSON));
BV:=TDocVariantData(V).GetValueOrNull('Test');
if not VarIsNull(BV)
then Result:=BsonVariantType.ToBlob(BV,RBS);
end;
BsonVariantType.ToBlob returns false because it expects another variant type.
]]>