blob: 5f2905a97ea6bf26c3cb4f577a6f3bde2985686b [file] [log] [blame]
Roger Meier2b2c0b22012-09-12 20:09:02 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19unit Thrift.Serializer;
20
Jens Geyer9f7f11e2016-04-14 21:37:11 +020021{$I Thrift.Defines.inc}
Nick4f5229e2016-04-14 16:43:22 +030022
Roger Meier2b2c0b22012-09-12 20:09:02 +000023interface
24
25uses
Jens Geyer9f7f11e2016-04-14 21:37:11 +020026 {$IFDEF OLD_UNIT_NAMES}
27 Classes, Windows, SysUtils,
Nick4f5229e2016-04-14 16:43:22 +030028 {$ELSE}
Jens Geyer9f7f11e2016-04-14 21:37:11 +020029 System.Classes, Winapi.Windows, System.SysUtils,
30 {$ENDIF}
Roger Meier2b2c0b22012-09-12 20:09:02 +000031 Thrift.Protocol,
32 Thrift.Transport,
33 Thrift.Stream;
34
35
36type
37 // Generic utility for easily serializing objects into a byte array or Stream.
38 TSerializer = class
39 private
40 FStream : TMemoryStream;
41 FTransport : ITransport;
42 FProtocol : IProtocol;
43
44 public
45 // Create a new TSerializer that uses the TBinaryProtocol by default.
46 constructor Create; overload;
47
48 // Create a new TSerializer.
49 // It will use the TProtocol specified by the factory that is passed in.
50 constructor Create( const factory : IProtocolFactory); overload;
51
52 // DTOR
53 destructor Destroy; override;
54
55 // Serialize the Thrift object.
56 function Serialize( const input : IBase) : TBytes; overload;
57 procedure Serialize( const input : IBase; const aStm : TStream); overload;
58 end;
59
60
61 // Generic utility for easily deserializing objects from byte array or Stream.
62 TDeserializer = class
63 private
64 FStream : TMemoryStream;
65 FTransport : ITransport;
66 FProtocol : IProtocol;
67
68 public
69 // Create a new TDeserializer that uses the TBinaryProtocol by default.
70 constructor Create; overload;
71
72 // Create a new TDeserializer.
73 // It will use the TProtocol specified by the factory that is passed in.
74 constructor Create( const factory : IProtocolFactory); overload;
75
76 // DTOR
77 destructor Destroy; override;
78
79 // Deserialize the Thrift object data.
80 procedure Deserialize( const input : TBytes; const target : IBase); overload;
81 procedure Deserialize( const input : TStream; const target : IBase); overload;
82 end;
83
84
85
86implementation
87
88
89{ TSerializer }
90
91
92constructor TSerializer.Create();
93// Create a new TSerializer that uses the TBinaryProtocol by default.
94begin
Jens Geyerd5436f52014-10-03 19:50:38 +020095 //no inherited;
Roger Meier2b2c0b22012-09-12 20:09:02 +000096 Create( TBinaryProtocolImpl.TFactory.Create);
97end;
98
99
100constructor TSerializer.Create( const factory : IProtocolFactory);
101// Create a new TSerializer.
102// It will use the TProtocol specified by the factory that is passed in.
103var adapter : IThriftStream;
104begin
105 inherited Create;
106 FStream := TMemoryStream.Create;
107 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
108 FTransport := TStreamTransportImpl.Create( nil, adapter);
109 FProtocol := factory.GetProtocol( FTransport);
110end;
111
112
113destructor TSerializer.Destroy;
114begin
115 try
116 FProtocol := nil;
117 FTransport := nil;
118 FreeAndNil( FStream);
119 finally
120 inherited Destroy;
121 end;
122end;
123
124
125function TSerializer.Serialize( const input : IBase) : TBytes;
126// Serialize the Thrift object into a byte array. The process is simple,
127// just clear the byte array output, write the object into it, and grab the
128// raw bytes.
129var iBytes : Int64;
130begin
131 try
132 FStream.Size := 0;
133 input.Write( FProtocol);
134 SetLength( result, FStream.Size);
135 iBytes := Length(result);
136 if iBytes > 0
137 then Move( FStream.Memory^, result[0], iBytes);
138 finally
139 FStream.Size := 0; // free any allocated memory
140 end;
141end;
142
143
144procedure TSerializer.Serialize( const input : IBase; const aStm : TStream);
145// Serialize the Thrift object into a byte array. The process is simple,
146// just clear the byte array output, write the object into it, and grab the
Jens Geyerd5436f52014-10-03 19:50:38 +0200147// raw bytes.
Roger Meier2b2c0b22012-09-12 20:09:02 +0000148const COPY_ENTIRE_STREAM = 0;
149begin
150 try
151 FStream.Size := 0;
152 input.Write( FProtocol);
153 aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM);
154 finally
155 FStream.Size := 0; // free any allocated memory
156 end;
157end;
158
159
160{ TDeserializer }
161
162
163constructor TDeserializer.Create();
164// Create a new TDeserializer that uses the TBinaryProtocol by default.
165begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200166 //no inherited;
Roger Meier2b2c0b22012-09-12 20:09:02 +0000167 Create( TBinaryProtocolImpl.TFactory.Create);
168end;
169
170
171constructor TDeserializer.Create( const factory : IProtocolFactory);
172// Create a new TDeserializer.
173// It will use the TProtocol specified by the factory that is passed in.
174var adapter : IThriftStream;
175begin
176 inherited Create;
177 FStream := TMemoryStream.Create;
178 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
179 FTransport := TStreamTransportImpl.Create( adapter, nil);
180 FProtocol := factory.GetProtocol( FTransport);
181end;
182
183
184destructor TDeserializer.Destroy;
185begin
186 try
187 FProtocol := nil;
188 FTransport := nil;
189 FreeAndNil( FStream);
190 finally
191 inherited Destroy;
192 end;
193end;
194
195
196procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase);
197// Deserialize the Thrift object data from the byte array.
198var iBytes : Int64;
199begin
200 try
201 iBytes := Length(input);
202 FStream.Size := iBytes;
203 if iBytes > 0
204 then Move( input[0], FStream.Memory^, iBytes);
205
206 target.Read( FProtocol);
207 finally
208 FStream.Size := 0; // free any allocated memory
209 end;
210end;
211
212
213procedure TDeserializer.Deserialize( const input : TStream; const target : IBase);
214// Deserialize the Thrift object data from the byte array.
215const COPY_ENTIRE_STREAM = 0;
216var before : Int64;
217begin
218 try
219 before := FStream.Position;
220 FStream.CopyFrom( input, COPY_ENTIRE_STREAM);
221 FStream.Position := before;
222 target.Read( FProtocol);
223 finally
224 FStream.Size := 0; // free any allocated memory
225 end;
226end;
227
228
229end.
230