1 { 2 全能中间件 —— 简单、高效、稳定、安全的三层中间件 3 4 1.支持 多账套多数据库,包括SQLite, MySQL, SQL Server, Oracle, PostgreSQL, DB2, SQL Anywhere,Firebird等多种数据库。 5 2.支持 快速开发,封装了 TRFConnection 和 TRFDataSet 控件供客户端使用,传统两层应用轻松升级为三层应用。 6 3.支持 数据压缩、加密传输,效率和安全有保障。 7 4.支持 数据连接池,提高并发响应速度。 8 5.支持 客户端执行SQL语句,存储过程,亦可以把逻辑SQL放在中间层,控制权由您一手掌握。 9 6.支持 Delta更新,比传统的拼接SQL更新方式效率高,兼容性更好。 10 7.支持 SQL 参数化查询、 分页查询。 11 8.支持 多表事务更新,会保证数据的一致性。 12 9.支持 多表联合查询更新其中一个表。 13 10.支持 多平台开发(Windows/Android/iOS/OS X),各平台使用方法一致。 14 11.支持 7x24小时长时间稳定运行,已经过多个正式项目长时间考验。 15 16 Copyright @2014-2017 by QQ:64445322 17 All rights reserved. 18 19 QQ群:339616649 20 } 21 unit Unit1; 22 23 interface 24 25 uses 26 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 27 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.JSON.Writers, System.JSON.Types, System.JSON.Builders, 28 System.JSON.Readers, Vcl.StdCtrls; 29 30 type 31 TForm1 = class(TForm) 32 btnBuilder: TButton; 33 Memo1: TMemo; 34 btnReader: TButton; 35 Memo2: TMemo; 36 procedure FormCreate(Sender: TObject); 37 procedure btnBuilderClick(Sender: TObject); 38 procedure btnReaderClick(Sender: TObject); 39 private 40 { Private declarations } 41 public 42 { Public declarations } 43 end; 44 //http://docwiki.embarcadero.com/RADStudio/Tokyo/en/JSON#JSON_Frameworks 45 TJSONBuilder = class(TJSONObjectBuilder) 46 private 47 FJsonWriter: TJsonTextWriter; 48 FStringWriter: TStringWriter; 49 FStringBuilder: TStringBuilder; 50 strict private 51 class var 52 FJsonBuilder: TJSONBuilder; 53 54 class destructor Destroy; 55 public 56 constructor Create(const AFormatting: TJsonFormatting = TJsonFormatting.None); 57 destructor Destroy; override; 58 59 function ToJSON: string; 60 61 class function NewBuilder(const AFormatting: TJsonFormatting = TJsonFormatting.None): TJSONBuilder; static; 62 end; 63 64 TJsonObjectReader = class (TJsonTextReader) 65 private 66 FStringReader : TStringReader; 67 strict private 68 class var 69 FJsonObjectReader: TJsonObjectReader; 70 class destructor Destroy; 71 public 72 constructor Create(const AJson: string); 73 destructor Destroy; override; 74 75 class function NewReader(const AJson:string): TJsonObjectReader; static; 76 end; 77 78 var 79 Form1: TForm1; 80 81 implementation 82 83 {$R *.dfm} 84 85 86 procedure TForm1.btnBuilderClick(Sender: TObject); 87 var 88 Builder: TJSONBuilder; 89 begin 90 Builder := TJSONBuilder.NewBuilder(TJsonFormatting.Indented); 91 Builder 92 .BeginObject 93 .BeginArray(‘Transaction‘) 94 .BeginObject.Add(‘id‘, 662713) 95 .Add(‘firstName‘, ‘John‘) 96 .Add(‘lastName‘, ‘Doe‘) 97 .Add(‘price‘, 2.1) 98 .AddNull(‘parent_id‘) 99 .Add(‘validated‘, True) 100 .EndObject 101 .BeginObject 102 .Add(‘id‘, 662714) 103 .Add(‘firstName‘, ‘Anna‘) 104 .Add(‘lastName‘, ‘Smith‘) 105 .Add(‘price‘, 4.5) 106 .AddNull(‘parent_id‘) 107 .Add(‘validated‘, False) 108 .EndObject 109 .BeginObject 110 .Add(‘id‘, 662715) 111 .Add(‘firstName‘, ‘Peter‘) 112 .Add(‘lastName‘, ‘Jones‘) 113 .Add(‘price‘, 3.6) 114 .AddNull(‘parent_id‘) 115 .Add(‘validated‘, True) 116 .EndObject 117 .EndArray 118 .EndObject; 119 120 Memo1.Text := Builder.ToJSON; 121 122 end; 123 124 procedure TForm1.btnReaderClick(Sender: TObject); 125 var 126 Reader:TJsonObjectReader; 127 begin 128 Memo2.Clear; 129 Reader:=TJsonObjectReader.NewReader(Memo1.Text); 130 131 while Reader.Read do 132 case Reader.TokenType of 133 TJsonToken.PropertyName: 134 Memo2.Lines.Add(Reader.Value.AsString); 135 TJsonToken.String: 136 Memo2.Lines[Memo2.Lines.Count-1] := Memo2.Lines[Memo2.Lines.Count-1] + ‘: ‘ +Reader.Value.AsString; 137 end; 138 end; 139 140 procedure TForm1.FormCreate(Sender: TObject); 141 begin 142 ReportMemoryLeaksOnShutdown := True; 143 end; 144 145 { TJSONBuilder } 146 147 constructor TJSONBuilder.Create(const AFormatting: TJsonFormatting); 148 begin 149 FJsonBuilder := nil; 150 FStringBuilder := TStringBuilder.Create; 151 FStringWriter := TStringWriter.Create(FStringBuilder); 152 FJsonWriter := TJsonTextWriter.Create(FStringWriter); 153 FJsonWriter.Formatting := AFormatting; 154 inherited Create(FJsonWriter); 155 end; 156 157 destructor TJSONBuilder.Destroy; 158 begin 159 FreeAndNil(FJsonWriter); 160 FreeAndNil(FStringWriter); 161 FreeAndNil(FStringBuilder); 162 inherited; 163 end; 164 165 class destructor TJSONBuilder.Destroy; 166 begin 167 FreeAndNil(FJsonBuilder); 168 end; 169 170 class function TJSONBuilder.NewBuilder(const AFormatting: TJsonFormatting): TJSONBuilder; 171 begin 172 if FJsonBuilder <> nil then 173 FreeAndNil(FJsonBuilder); 174 175 FJsonBuilder := TJSONBuilder.Create(AFormatting); 176 177 Result := FJsonBuilder; 178 end; 179 180 function TJSONBuilder.ToJSON: string; 181 begin 182 Result := FStringBuilder.ToString; 183 end; 184 185 { TJsonObjectReader } 186 187 constructor TJsonObjectReader.Create(const AJson: string); 188 begin 189 FStringReader:= TStringReader.Create(AJson); 190 inherited Create(FStringReader) ; 191 end; 192 193 class destructor TJsonObjectReader.Destroy; 194 begin 195 FreeAndNil(FJsonObjectReader); 196 end; 197 198 destructor TJsonObjectReader.Destroy; 199 begin 200 FreeAndNil(FStringReader); 201 inherited; 202 end; 203 204 class function TJsonObjectReader.NewReader(const AJson:string): TJsonObjectReader; 205 begin 206 if FJsonObjectReader <> nil then 207 FreeAndNil(FJsonObjectReader); 208 209 FJsonObjectReader := TJsonObjectReader.Create(AJson); 210 211 Result := FJsonObjectReader; 212 end; 213 214 end.
运行效果如图:
时间: 2024-10-24 08:45:20