bson(json)序列还原
bson(json)序列还原
unit Unit2; interface uses Grijjy.Bson.Serialization, Grijjy.Bson, Grijjy.Bson.IO, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TTest = record public id: Integer; name: TBytes; end; TForm2 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure Button8Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} var Bson: TBytes; json: string; procedure TForm2.Button1Click(Sender: TObject); //bson write begin var w: IgoBsonWriter := TgoBsonWriter.Create; w.WriteStartDocument; w.WriteInt32('int', 2); w.WriteString('str', '中国'); w.WriteEndDocument; Bson := w.ToBson; end; procedure TForm2.Button2Click(Sender: TObject); //bson read begin Memo1.Clear; var r: IgoBsonReader := TgoBsonReader.Create(Bson); r.ReadStartDocument; Memo1.Lines.Add(r.ReadName); Memo1.Lines.Add(r.ReadInt32.ToString); Memo1.Lines.Add(r.ReadName); Memo1.Lines.Add(r.ReadString); r.ReadEndDocument; end; procedure TForm2.Button3Click(Sender: TObject); //json write begin var w: IgoJsonWriter := TgoJsonWriter.Create; w.WriteStartDocument; w.WriteInt32('int', 2); w.WriteString('str', '中国'); w.WriteEndDocument; json := w.ToJson; Memo1.Text := json; //{ "int" : 2, "str" : "\u4e2d\u56fd" } end; procedure TForm2.Button4Click(Sender: TObject); //json read begin Memo1.Clear; var r: IgoJsonReader := TgoJsonReader.Create(json); r.ReadStartDocument; Memo1.Lines.Add(r.ReadName); Memo1.Lines.Add(r.ReadInt32.ToString); Memo1.Lines.Add(r.ReadName); Memo1.Lines.Add(r.ReadString); r.ReadEndDocument; end; procedure TForm2.Button5Click(Sender: TObject); //record序列为bson(json) begin var rec: TTest; rec.id := 2; rec.name := BytesOf('中国'); TgoBsonSerializer.Serialize(rec, Bson); //bson // TgoBsonSerializer.Serialize(rec, json); //json end; procedure TForm2.Button6Click(Sender: TObject); //bson(json)还原为record begin Memo1.Clear; var rec: TTest; TgoBsonSerializer.Deserialize(Bson, rec); //bson // TgoBsonSerializer.Deserialize(json, rec); //json Memo1.Lines.Add(rec.id.ToString); Memo1.Lines.Add(StringOf(rec.name)); end; procedure TForm2.Button7Click(Sender: TObject); begin var w: TgoBsonDocument := TgoBsonDocument.Create; w.Add('int', 2); w.Add('str', '中国'); w.Add('bytes', bytesof('汉字')); Bson := w.ToBson; //bson // json := w.ToJson; //json end; procedure TForm2.Button8Click(Sender: TObject); begin Memo1.Clear; var r: TgoBsonDocument; r := r.Load(Bson); //bson // r.TryParse(json, r); //json Memo1.Lines.Add(r.ValuesByName['int'].AsInteger.ToString); Memo1.Lines.Add(r.ValuesByName['str'].AsString); Memo1.Lines.Add(StringOf(r.ValuesByName['bytes'].AsByteArray)); end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/14210652.html