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