#1 2024-10-13 04:41:40

pangzhenguang
Member
Registered: 2017-11-11
Posts: 2

AddValue of TDocVariant Data Chinese garbled characters

The method AddValue of the TDocVariant object displays Chinese garbled characters when adding a Chinese string

I will write a simple program

version 1.18

procedure TForm1.Button1Click(Sender: TObject);
var
  lItem: TDocVariantData;
begin
  lItem.InitFast;
  lItem.AddValue('Caption','哪个数据库');
end;


in debugger the lItem value is :

(271, [dvoIsObject,dvoReturnNullForUnknownProperty,dvoValueCopiedByReference], ('Caption', '', '', ''), ('鍝釜鏁版嵁搴'#$93, Unassigned, Unassigned, Unassigned), 1)

would you help me

Offline

#2 2024-10-13 06:50:05

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,649
Website

Re: AddValue of TDocVariant Data Chinese garbled characters

Which compiler do you use?

The string value is passed as a variant.
Depending on the compiler version, it may be encoded differently - and unexpectedly.
Normally, the variant value within the TDocVariantData is encoded as varString and with UTF-8 encoding, which seems the case.
What does Item.ToJson or Item.S['Caption'] respond? I guess it is the correct value.
Don't trust the debugger, it displays something unexpected for you, but something correct for the process itself.

Some other ideas:
- write Item.AddValue('Caption', RawUTF8ToVariant('...'));
- write Item._S['Caption'] := '...';
- write Item.AddValueFromText() and a RawUtf8 content,
- write Item.AddNameValuesToObject(['Caption', '...']); or even InitObject(['Caption, '...']);

Offline

#3 2024-10-13 12:27:49

pangzhenguang
Member
Registered: 2017-11-11
Posts: 2

Re: AddValue of TDocVariant Data Chinese garbled characters

Thank you for your reply

Embarcadero Delphi for Win64 compiler version 35.0

You're right, just garbled in the debugger  and placed it in a string variable, that's correct.

Offline

#4 2024-10-14 08:28:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,649
Website

Re: AddValue of TDocVariant Data Chinese garbled characters

I have added the dvoValueDoNotNormalizeAsRawUtf8 option.
https://github.com/synopse/mORMot2/commit/59f17dfb

It will store the value as it was, without any temporary RawUtf8 conversion.
It may help in your case, if you mostly stay at VCL level.
But if you use it for JSON, then this option is not really needed.

Offline

Board footer

Powered by FluxBB