blob: a81cef082610f11631b91c4a821aceed6885f9d2 [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.
141var iBytes : Int64;
142const 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
160 Create( TBinaryProtocolImpl.TFactory.Create);
161end;
162
163
164constructor TDeserializer.Create( const factory : IProtocolFactory);
165// Create a new TDeserializer.
166// It will use the TProtocol specified by the factory that is passed in.
167var adapter : IThriftStream;
168begin
169 inherited Create;
170 FStream := TMemoryStream.Create;
171 adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE);
172 FTransport := TStreamTransportImpl.Create( adapter, nil);
173 FProtocol := factory.GetProtocol( FTransport);
174end;
175
176
177destructor TDeserializer.Destroy;
178begin
179 try
180 FProtocol := nil;
181 FTransport := nil;
182 FreeAndNil( FStream);
183 finally
184 inherited Destroy;
185 end;
186end;
187
188
189procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase);
190// Deserialize the Thrift object data from the byte array.
191var iBytes : Int64;
192begin
193 try
194 iBytes := Length(input);
195 FStream.Size := iBytes;
196 if iBytes > 0
197 then Move( input[0], FStream.Memory^, iBytes);
198
199 target.Read( FProtocol);
200 finally
201 FStream.Size := 0; // free any allocated memory
202 end;
203end;
204
205
206procedure TDeserializer.Deserialize( const input : TStream; const target : IBase);
207// Deserialize the Thrift object data from the byte array.
208const COPY_ENTIRE_STREAM = 0;
209var before : Int64;
210begin
211 try
212 before := FStream.Position;
213 FStream.CopyFrom( input, COPY_ENTIRE_STREAM);
214 FStream.Position := before;
215 target.Read( FProtocol);
216 finally
217 FStream.Size := 0; // free any allocated memory
218 end;
219end;
220
221
222end.
223