這幾天學習 JSON - SuperObject, 非常幸運地得到了其作者 Henri Gourvest 大師的同步指點! (Henri 先生也是 DSPack 和 GDI+ 頭文件的作者).
學習中發現 SuperObject 有些地方對中文(或者說 Unicode)支持不是所期望的, 現在專貼提出來供大師鑒別.
以下例子都會出現亂碼, 雖然都可以有變通的方案, 但如果不亂碼就太好了!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses SuperObject;
procedure TForm1.Button1Click(Sender: TObject);
var
jo: ISuperObject;
stream: TStream;
begin
stream := TMemoryStream.Create;
jo := SO('{"Language" : "簡體中文"}');
jo.SaveTo(stream, False, False);
stream.Position := 0;
Memo1.Lines.LoadFromStream(stream); // {"Language":"€S-?}
stream.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
const
FilePath = 'C:\Temp\Test.txt';
var
jo: ISuperObject;
begin
jo := SO('{"Language" : "簡體中文"}');
jo.SaveTo(FilePath, False, False);
with TStringList.Create do
begin
LoadFromFile(FilePath);
ShowMessage(Text); // {"Language":"€S-?}
Free;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
jo: ISuperObject;
stream: TStream;
begin
stream := TStringStream.Create('{"Language" : "簡體中文"}');
jo := TSuperObject.ParseStream(stream);
ShowMessage(jo.AsJSon(False, False)); // {"Language":"¼òÌåÖÐÎÄ"}
stream.Free;
end;
procedure TForm1.Button4Click(Sender: TObject);
const
FilePath = 'C:\Temp\Test.txt';
var
jo: ISuperObject;
begin
with TStringList.Create do
begin
Add('{"Language" : "簡體中文"}');
SaveToFile(FilePath);
Free;
end;
jo := TSuperObject.ParseFile(FilePath);
ShowMessage(jo.AsJSon(False, False)); // {"Language":"¼òÌåÖÐÎÄ"}
end;
end.