Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 1 | (* |
| 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 | *) |
| 19 | unit Thrift.Serializer; |
| 20 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 21 | {$I Thrift.Defines.inc} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 22 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 23 | interface |
| 24 | |
| 25 | uses |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 26 | {$IFDEF OLD_UNIT_NAMES} |
| 27 | Classes, Windows, SysUtils, |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 28 | {$ELSE} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 29 | System.Classes, Winapi.Windows, System.SysUtils, |
| 30 | {$ENDIF} |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 31 | Thrift.Protocol, |
| 32 | Thrift.Transport, |
| 33 | Thrift.Stream; |
| 34 | |
| 35 | |
| 36 | type |
| 37 | // Generic utility for easily serializing objects into a byte array or Stream. |
| 38 | TSerializer = class |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 39 | strict private |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 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 | |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 52 | // Create a new TSerializer. |
| 53 | // It will use the TProtocol and layered transports specified by the factories that are passed in. |
| 54 | constructor Create( const protfact : IProtocolFactory; const transfact : ITransportFactory); overload; |
| 55 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 56 | // DTOR |
| 57 | destructor Destroy; override; |
| 58 | |
| 59 | // Serialize the Thrift object. |
| 60 | function Serialize( const input : IBase) : TBytes; overload; |
| 61 | procedure Serialize( const input : IBase; const aStm : TStream); overload; |
| 62 | end; |
| 63 | |
| 64 | |
| 65 | // Generic utility for easily deserializing objects from byte array or Stream. |
| 66 | TDeserializer = class |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 67 | strict private |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 68 | FStream : TMemoryStream; |
| 69 | FTransport : ITransport; |
| 70 | FProtocol : IProtocol; |
| 71 | |
| 72 | public |
| 73 | // Create a new TDeserializer that uses the TBinaryProtocol by default. |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 74 | constructor Create( const aMaxMessageSize : Integer = DEFAULT_MAX_MESSAGE_SIZE); overload; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 75 | |
| 76 | // Create a new TDeserializer. |
| 77 | // It will use the TProtocol specified by the factory that is passed in. |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 78 | constructor Create( const factory : IProtocolFactory; |
| 79 | const aMaxMessageSize : Integer = DEFAULT_MAX_MESSAGE_SIZE); overload; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 80 | |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 81 | // Create a new TDeserializer. |
| 82 | // It will use the TProtocol and layered transports specified by the factories that are passed in. |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 83 | constructor Create( const protfact : IProtocolFactory; |
| 84 | const transfact : ITransportFactory; |
| 85 | const aMaxMessageSize : Integer = DEFAULT_MAX_MESSAGE_SIZE); overload; |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 86 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 87 | // DTOR |
| 88 | destructor Destroy; override; |
| 89 | |
| 90 | // Deserialize the Thrift object data. |
| 91 | procedure Deserialize( const input : TBytes; const target : IBase); overload; |
| 92 | procedure Deserialize( const input : TStream; const target : IBase); overload; |
| 93 | end; |
| 94 | |
| 95 | |
| 96 | |
| 97 | implementation |
| 98 | |
| 99 | |
| 100 | { TSerializer } |
| 101 | |
| 102 | |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 103 | constructor TSerializer.Create; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 104 | // Create a new TSerializer that uses the TBinaryProtocol by default. |
| 105 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 106 | //no inherited; |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 107 | Create( TBinaryProtocolImpl.TFactory.Create, nil); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 108 | end; |
| 109 | |
| 110 | |
| 111 | constructor TSerializer.Create( const factory : IProtocolFactory); |
| 112 | // Create a new TSerializer. |
| 113 | // It will use the TProtocol specified by the factory that is passed in. |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 114 | begin |
| 115 | //no inherited; |
| 116 | Create( factory, nil); |
| 117 | end; |
| 118 | |
| 119 | |
| 120 | constructor TSerializer.Create( const protfact : IProtocolFactory; const transfact : ITransportFactory); |
| 121 | // Create a new TSerializer. |
| 122 | // It will use the TProtocol specified by the factory that is passed in. |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 123 | var adapter : IThriftStream; |
| 124 | begin |
| 125 | inherited Create; |
| 126 | FStream := TMemoryStream.Create; |
| 127 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 128 | FTransport := TStreamTransportImpl.Create( nil, adapter, TTransportControlImpl.Create(0)); // we don't read anything here |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 129 | if transfact <> nil then FTransport := transfact.GetTransport( FTransport); |
| 130 | FProtocol := protfact.GetProtocol( FTransport); |
| 131 | |
| 132 | if not FTransport.IsOpen |
| 133 | then FTransport.Open; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 134 | end; |
| 135 | |
| 136 | |
| 137 | destructor TSerializer.Destroy; |
| 138 | begin |
| 139 | try |
| 140 | FProtocol := nil; |
| 141 | FTransport := nil; |
| 142 | FreeAndNil( FStream); |
| 143 | finally |
| 144 | inherited Destroy; |
| 145 | end; |
| 146 | end; |
| 147 | |
| 148 | |
| 149 | function TSerializer.Serialize( const input : IBase) : TBytes; |
| 150 | // Serialize the Thrift object into a byte array. The process is simple, |
| 151 | // just clear the byte array output, write the object into it, and grab the |
| 152 | // raw bytes. |
| 153 | var iBytes : Int64; |
| 154 | begin |
| 155 | try |
| 156 | FStream.Size := 0; |
| 157 | input.Write( FProtocol); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 158 | FTransport.Flush; |
| 159 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 160 | SetLength( result, FStream.Size); |
| 161 | iBytes := Length(result); |
| 162 | if iBytes > 0 |
| 163 | then Move( FStream.Memory^, result[0], iBytes); |
| 164 | finally |
| 165 | FStream.Size := 0; // free any allocated memory |
| 166 | end; |
| 167 | end; |
| 168 | |
| 169 | |
| 170 | procedure TSerializer.Serialize( const input : IBase; const aStm : TStream); |
| 171 | // Serialize the Thrift object into a byte array. The process is simple, |
| 172 | // just clear the byte array output, write the object into it, and grab the |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 173 | // raw bytes. |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 174 | const COPY_ENTIRE_STREAM = 0; |
| 175 | begin |
| 176 | try |
| 177 | FStream.Size := 0; |
| 178 | input.Write( FProtocol); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 179 | FTransport.Flush; |
| 180 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 181 | aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM); |
| 182 | finally |
| 183 | FStream.Size := 0; // free any allocated memory |
| 184 | end; |
| 185 | end; |
| 186 | |
| 187 | |
| 188 | { TDeserializer } |
| 189 | |
| 190 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 191 | constructor TDeserializer.Create( const aMaxMessageSize : Integer); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 192 | // Create a new TDeserializer that uses the TBinaryProtocol by default. |
| 193 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 194 | //no inherited; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 195 | Create( TBinaryProtocolImpl.TFactory.Create, nil, aMaxMessageSize); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 196 | end; |
| 197 | |
| 198 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 199 | constructor TDeserializer.Create( const factory : IProtocolFactory; const aMaxMessageSize : Integer); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 200 | // Create a new TDeserializer. |
| 201 | // It will use the TProtocol specified by the factory that is passed in. |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 202 | begin |
| 203 | //no inherited; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 204 | Create( factory, nil, aMaxMessageSize); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 205 | end; |
| 206 | |
| 207 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 208 | constructor TDeserializer.Create( const protfact : IProtocolFactory; |
| 209 | const transfact : ITransportFactory; |
| 210 | const aMaxMessageSize : Integer = DEFAULT_MAX_MESSAGE_SIZE); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 211 | // Create a new TDeserializer. |
| 212 | // It will use the TProtocol specified by the factory that is passed in. |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 213 | var adapter : IThriftStream; |
| 214 | begin |
| 215 | inherited Create; |
| 216 | FStream := TMemoryStream.Create; |
| 217 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 218 | FTransport := TStreamTransportImpl.Create( adapter, nil, TTransportControlImpl.Create(aMaxMessageSize)); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 219 | if transfact <> nil then FTransport := transfact.GetTransport( FTransport); |
| 220 | FProtocol := protfact.GetProtocol( FTransport); |
| 221 | |
| 222 | if not FTransport.IsOpen |
| 223 | then FTransport.Open; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 224 | end; |
| 225 | |
| 226 | |
| 227 | destructor TDeserializer.Destroy; |
| 228 | begin |
| 229 | try |
| 230 | FProtocol := nil; |
| 231 | FTransport := nil; |
| 232 | FreeAndNil( FStream); |
| 233 | finally |
| 234 | inherited Destroy; |
| 235 | end; |
| 236 | end; |
| 237 | |
| 238 | |
| 239 | procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase); |
| 240 | // Deserialize the Thrift object data from the byte array. |
| 241 | var iBytes : Int64; |
| 242 | begin |
| 243 | try |
| 244 | iBytes := Length(input); |
| 245 | FStream.Size := iBytes; |
| 246 | if iBytes > 0 |
| 247 | then Move( input[0], FStream.Memory^, iBytes); |
| 248 | |
| 249 | target.Read( FProtocol); |
| 250 | finally |
| 251 | FStream.Size := 0; // free any allocated memory |
| 252 | end; |
| 253 | end; |
| 254 | |
| 255 | |
| 256 | procedure TDeserializer.Deserialize( const input : TStream; const target : IBase); |
| 257 | // Deserialize the Thrift object data from the byte array. |
| 258 | const COPY_ENTIRE_STREAM = 0; |
| 259 | var before : Int64; |
| 260 | begin |
| 261 | try |
| 262 | before := FStream.Position; |
| 263 | FStream.CopyFrom( input, COPY_ENTIRE_STREAM); |
| 264 | FStream.Position := before; |
| 265 | target.Read( FProtocol); |
| 266 | finally |
| 267 | FStream.Size := 0; // free any allocated memory |
| 268 | end; |
| 269 | end; |
| 270 | |
| 271 | |
| 272 | end. |
| 273 | |