blob: 43b5d29efc74eb23227ed6600077f5d4a047436c [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
21interface
22
23uses
24 Classes, Windows, SysUtils,
25 Thrift.Protocol,
26 Thrift.Transport,
27 Thrift.Stream;
28
29
30type
31 // Generic utility for easily serializing objects into a byte array or Stream.
32 TSerializer = class
33 private
34 FStream : TMemoryStream;
35 FTransport : ITransport;
36 FProtocol : IProtocol;
37
38 public
39 // Create a new TSerializer that uses the TBinaryProtocol by default.
40 constructor Create; overload;
41
42 // Create a new TSerializer.
43 // It will use the TProtocol specified by the factory that is passed in.
44 constructor Create( const factory : IProtocolFactory); overload;
45
46 // DTOR
47 destructor Destroy; override;
48
49 // Serialize the Thrift object.
50 function Serialize( const input : IBase) : TBytes; overload;
51 procedure Serialize( const input : IBase; const aStm : TStream); overload;
52 end;
53
54
55 // Generic utility for easily deserializing objects from byte array or Stream.
56 TDeserializer = class
57 private
58 FStream : TMemoryStream;
59 FTransport : ITransport;
60 FProtocol : IProtocol;
61
62 public
63 // Create a new TDeserializer that uses the TBinaryProtocol by default.
64 constructor Create; overload;
65
66 // Create a new TDeserializer.
67 // It will use the TProtocol specified by the factory that is passed in.
68 constructor Create( const factory : IProtocolFactory); overload;
69
70 // DTOR
71 destructor Destroy; override;
72
73 // Deserialize the Thrift object data.
74 procedure Deserialize( const input : TBytes; const target : IBase); overload;
75 procedure Deserialize( const input : TStream; const target : IBase); overload;
76 end;
77
78
79
80implementation
81
82
83{ TSerializer }
84
85
86constructor TSerializer.Create();
87// Create a new TSerializer that uses the TBinaryProtocol by default.
88begin
Jens Geyer718f6ee2013-09-06 21:02:34 +020089 //no inherited;
Roger Meier2b2c0b22012-09-12 20:09:02 +000090 Create( TBinaryProtocolImpl.TFactory.Create);
91end;
92
93
94constructor TSerializer.Create( const factory : IProtocolFactory);
95// Create a new TSerializer.
96// It will use the TProtocol specified by the factory that is passed in.
97var adapter : IThriftStream;
98begin
99 inherited Create;
100 FStream := TMemoryStream.Create;
101 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
102 FTransport := TStreamTransportImpl.Create( nil, adapter);
103 FProtocol := factory.GetProtocol( FTransport);
104end;
105
106
107destructor TSerializer.Destroy;
108begin
109 try
110 FProtocol := nil;
111 FTransport := nil;
112 FreeAndNil( FStream);
113 finally
114 inherited Destroy;
115 end;
116end;
117
118
119function TSerializer.Serialize( const input : IBase) : TBytes;
120// Serialize the Thrift object into a byte array. The process is simple,
121// just clear the byte array output, write the object into it, and grab the
122// raw bytes.
123var iBytes : Int64;
124begin
125 try
126 FStream.Size := 0;
127 input.Write( FProtocol);
128 SetLength( result, FStream.Size);
129 iBytes := Length(result);
130 if iBytes > 0
131 then Move( FStream.Memory^, result[0], iBytes);
132 finally
133 FStream.Size := 0; // free any allocated memory
134 end;
135end;
136
137
138procedure TSerializer.Serialize( const input : IBase; const aStm : TStream);
139// Serialize the Thrift object into a byte array. The process is simple,
140// just clear the byte array output, write the object into it, and grab the
141// raw bytes.
Roger Meier2b2c0b22012-09-12 20:09:02 +0000142const COPY_ENTIRE_STREAM = 0;
143begin
144 try
145 FStream.Size := 0;
146 input.Write( FProtocol);
147 aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM);
148 finally
149 FStream.Size := 0; // free any allocated memory
150 end;
151end;
152
153
154{ TDeserializer }
155
156
157constructor TDeserializer.Create();
158// Create a new TDeserializer that uses the TBinaryProtocol by default.
159begin
Jens Geyer718f6ee2013-09-06 21:02:34 +0200160 //no inherited;
Roger Meier2b2c0b22012-09-12 20:09:02 +0000161 Create( TBinaryProtocolImpl.TFactory.Create);
162end;
163
164
165constructor TDeserializer.Create( const factory : IProtocolFactory);
166// Create a new TDeserializer.
167// It will use the TProtocol specified by the factory that is passed in.
168var adapter : IThriftStream;
169begin
170 inherited Create;
171 FStream := TMemoryStream.Create;
172 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
173 FTransport := TStreamTransportImpl.Create( adapter, nil);
174 FProtocol := factory.GetProtocol( FTransport);
175end;
176
177
178destructor TDeserializer.Destroy;
179begin
180 try
181 FProtocol := nil;
182 FTransport := nil;
183 FreeAndNil( FStream);
184 finally
185 inherited Destroy;
186 end;
187end;
188
189
190procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase);
191// Deserialize the Thrift object data from the byte array.
192var iBytes : Int64;
193begin
194 try
195 iBytes := Length(input);
196 FStream.Size := iBytes;
197 if iBytes > 0
198 then Move( input[0], FStream.Memory^, iBytes);
199
200 target.Read( FProtocol);
201 finally
202 FStream.Size := 0; // free any allocated memory
203 end;
204end;
205
206
207procedure TDeserializer.Deserialize( const input : TStream; const target : IBase);
208// Deserialize the Thrift object data from the byte array.
209const COPY_ENTIRE_STREAM = 0;
210var before : Int64;
211begin
212 try
213 before := FStream.Position;
214 FStream.CopyFrom( input, COPY_ENTIRE_STREAM);
215 FStream.Position := before;
216 target.Read( FProtocol);
217 finally
218 FStream.Size := 0; // free any allocated memory
219 end;
220end;
221
222
223end.
224