blob: dce68639e1690781f4b50ec5ff6a3a7135ac0296 [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
89 Create( TBinaryProtocolImpl.TFactory.Create);
90end;
91
92
93constructor TSerializer.Create( const factory : IProtocolFactory);
94// Create a new TSerializer.
95// It will use the TProtocol specified by the factory that is passed in.
96var adapter : IThriftStream;
97begin
98 inherited Create;
99 FStream := TMemoryStream.Create;
100 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
101 FTransport := TStreamTransportImpl.Create( nil, adapter);
102 FProtocol := factory.GetProtocol( FTransport);
103end;
104
105
106destructor TSerializer.Destroy;
107begin
108 try
109 FProtocol := nil;
110 FTransport := nil;
111 FreeAndNil( FStream);
112 finally
113 inherited Destroy;
114 end;
115end;
116
117
118function TSerializer.Serialize( const input : IBase) : TBytes;
119// Serialize the Thrift object into a byte array. The process is simple,
120// just clear the byte array output, write the object into it, and grab the
121// raw bytes.
122var iBytes : Int64;
123begin
124 try
125 FStream.Size := 0;
126 input.Write( FProtocol);
127 SetLength( result, FStream.Size);
128 iBytes := Length(result);
129 if iBytes > 0
130 then Move( FStream.Memory^, result[0], iBytes);
131 finally
132 FStream.Size := 0; // free any allocated memory
133 end;
134end;
135
136
137procedure TSerializer.Serialize( const input : IBase; const aStm : TStream);
138// Serialize the Thrift object into a byte array. The process is simple,
139// just clear the byte array output, write the object into it, and grab the
140// raw bytes.
Roger Meier2b2c0b22012-09-12 20:09:02 +0000141const COPY_ENTIRE_STREAM = 0;
142begin
143 try
144 FStream.Size := 0;
145 input.Write( FProtocol);
146 aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM);
147 finally
148 FStream.Size := 0; // free any allocated memory
149 end;
150end;
151
152
153{ TDeserializer }
154
155
156constructor TDeserializer.Create();
157// Create a new TDeserializer that uses the TBinaryProtocol by default.
158begin
159 Create( TBinaryProtocolImpl.TFactory.Create);
160end;
161
162
163constructor TDeserializer.Create( const factory : IProtocolFactory);
164// Create a new TDeserializer.
165// It will use the TProtocol specified by the factory that is passed in.
166var adapter : IThriftStream;
167begin
168 inherited Create;
169 FStream := TMemoryStream.Create;
170 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
171 FTransport := TStreamTransportImpl.Create( adapter, nil);
172 FProtocol := factory.GetProtocol( FTransport);
173end;
174
175
176destructor TDeserializer.Destroy;
177begin
178 try
179 FProtocol := nil;
180 FTransport := nil;
181 FreeAndNil( FStream);
182 finally
183 inherited Destroy;
184 end;
185end;
186
187
188procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase);
189// Deserialize the Thrift object data from the byte array.
190var iBytes : Int64;
191begin
192 try
193 iBytes := Length(input);
194 FStream.Size := iBytes;
195 if iBytes > 0
196 then Move( input[0], FStream.Memory^, iBytes);
197
198 target.Read( FProtocol);
199 finally
200 FStream.Size := 0; // free any allocated memory
201 end;
202end;
203
204
205procedure TDeserializer.Deserialize( const input : TStream; const target : IBase);
206// Deserialize the Thrift object data from the byte array.
207const COPY_ENTIRE_STREAM = 0;
208var before : Int64;
209begin
210 try
211 before := FStream.Position;
212 FStream.CopyFrom( input, COPY_ENTIRE_STREAM);
213 FStream.Position := before;
214 target.Read( FProtocol);
215 finally
216 FStream.Size := 0; // free any allocated memory
217 end;
218end;
219
220
221end.
222