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} |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 31 | Thrift.Configuration, |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 32 | Thrift.Protocol, |
| 33 | Thrift.Transport, |
| 34 | Thrift.Stream; |
| 35 | |
| 36 | |
| 37 | type |
| 38 | // Generic utility for easily serializing objects into a byte array or Stream. |
| 39 | TSerializer = class |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 40 | strict private |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 41 | FStream : TThriftMemoryStream; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 42 | FTransport : ITransport; |
| 43 | FProtocol : IProtocol; |
| 44 | |
| 45 | public |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 46 | constructor Create( const aProtFact : IProtocolFactory = nil; // defaults to TBinaryProtocol |
| 47 | const aTransFact : ITransportFactory = nil; |
| 48 | const aConfig : IThriftConfiguration = nil); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 49 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 50 | // DTOR |
| 51 | destructor Destroy; override; |
| 52 | |
| 53 | // Serialize the Thrift object. |
| 54 | function Serialize( const input : IBase) : TBytes; overload; |
| 55 | procedure Serialize( const input : IBase; const aStm : TStream); overload; |
| 56 | end; |
| 57 | |
| 58 | |
| 59 | // Generic utility for easily deserializing objects from byte array or Stream. |
| 60 | TDeserializer = class |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 61 | strict private |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 62 | FStream : TThriftMemoryStream; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 63 | FTransport : ITransport; |
| 64 | FProtocol : IProtocol; |
| 65 | |
| 66 | public |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 67 | constructor Create( const aProtFact : IProtocolFactory = nil; // defaults to TBinaryProtocol |
| 68 | const aTransFact : ITransportFactory = nil; |
Jens Geyer | 3e6be73 | 2025-06-04 22:31:55 +0200 | [diff] [blame^] | 69 | const aConfig : IThriftConfiguration = nil); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 70 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 71 | // DTOR |
| 72 | destructor Destroy; override; |
| 73 | |
| 74 | // Deserialize the Thrift object data. |
| 75 | procedure Deserialize( const input : TBytes; const target : IBase); overload; |
| 76 | procedure Deserialize( const input : TStream; const target : IBase); overload; |
Jens Geyer | 3e6be73 | 2025-06-04 22:31:55 +0200 | [diff] [blame^] | 77 | |
| 78 | // helper |
| 79 | property Protocol : IProtocol read FProtocol; |
| 80 | property Transport : ITransport read FTransport; |
| 81 | property Stream : TThriftMemoryStream read FStream; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 82 | end; |
| 83 | |
| 84 | |
| 85 | |
| 86 | implementation |
| 87 | |
| 88 | |
| 89 | { TSerializer } |
| 90 | |
| 91 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 92 | constructor TSerializer.Create( const aProtFact : IProtocolFactory; |
| 93 | const aTransFact : ITransportFactory; |
| 94 | const aConfig : IThriftConfiguration); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 95 | var adapter : IThriftStream; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 96 | protfact : IProtocolFactory; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 97 | begin |
| 98 | inherited Create; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 99 | |
Jens Geyer | f726ae3 | 2021-06-04 11:17:26 +0200 | [diff] [blame] | 100 | FStream := TThriftMemoryStream.Create; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 101 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 102 | |
| 103 | FTransport := TStreamTransportImpl.Create( nil, adapter, aConfig); |
| 104 | if aTransfact <> nil then FTransport := aTransfact.GetTransport( FTransport); |
| 105 | |
| 106 | if aProtFact <> nil |
| 107 | then protfact := aProtFact |
| 108 | else protfact := TBinaryProtocolImpl.TFactory.Create; |
| 109 | FProtocol := protfact.GetProtocol( FTransport); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 110 | |
| 111 | if not FTransport.IsOpen |
| 112 | then FTransport.Open; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 113 | end; |
| 114 | |
| 115 | |
| 116 | destructor TSerializer.Destroy; |
| 117 | begin |
| 118 | try |
| 119 | FProtocol := nil; |
| 120 | FTransport := nil; |
| 121 | FreeAndNil( FStream); |
| 122 | finally |
| 123 | inherited Destroy; |
| 124 | end; |
| 125 | end; |
| 126 | |
| 127 | |
| 128 | function TSerializer.Serialize( const input : IBase) : TBytes; |
| 129 | // Serialize the Thrift object into a byte array. The process is simple, |
| 130 | // just clear the byte array output, write the object into it, and grab the |
| 131 | // raw bytes. |
| 132 | var iBytes : Int64; |
| 133 | begin |
| 134 | try |
| 135 | FStream.Size := 0; |
| 136 | input.Write( FProtocol); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 137 | FTransport.Flush; |
| 138 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 139 | SetLength( result, FStream.Size); |
| 140 | iBytes := Length(result); |
| 141 | if iBytes > 0 |
| 142 | then Move( FStream.Memory^, result[0], iBytes); |
| 143 | finally |
| 144 | FStream.Size := 0; // free any allocated memory |
| 145 | end; |
| 146 | end; |
| 147 | |
| 148 | |
| 149 | procedure TSerializer.Serialize( const input : IBase; const aStm : TStream); |
| 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 |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 152 | // raw bytes. |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 153 | const COPY_ENTIRE_STREAM = 0; |
| 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 | aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM); |
| 161 | finally |
| 162 | FStream.Size := 0; // free any allocated memory |
| 163 | end; |
| 164 | end; |
| 165 | |
| 166 | |
| 167 | { TDeserializer } |
| 168 | |
| 169 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 170 | constructor TDeserializer.Create( const aProtFact : IProtocolFactory; |
| 171 | const aTransFact : ITransportFactory; |
Jens Geyer | 3e6be73 | 2025-06-04 22:31:55 +0200 | [diff] [blame^] | 172 | const aConfig : IThriftConfiguration); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 173 | var adapter : IThriftStream; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 174 | protfact : IProtocolFactory; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 175 | begin |
| 176 | inherited Create; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 177 | |
Jens Geyer | 3e6be73 | 2025-06-04 22:31:55 +0200 | [diff] [blame^] | 178 | FStream := TThriftMemoryStream.Create; |
| 179 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 180 | |
| 181 | FTransport := TStreamTransportImpl.Create( adapter, nil, aConfig); |
| 182 | if aTransfact <> nil then FTransport := aTransfact.GetTransport( FTransport); |
| 183 | |
| 184 | if aProtFact <> nil |
| 185 | then protfact := aProtFact |
| 186 | else protfact := TBinaryProtocolImpl.TFactory.Create; |
| 187 | FProtocol := protfact.GetProtocol( FTransport); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 188 | |
| 189 | if not FTransport.IsOpen |
| 190 | then FTransport.Open; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 191 | end; |
| 192 | |
| 193 | |
| 194 | destructor TDeserializer.Destroy; |
| 195 | begin |
| 196 | try |
| 197 | FProtocol := nil; |
| 198 | FTransport := nil; |
| 199 | FreeAndNil( FStream); |
| 200 | finally |
| 201 | inherited Destroy; |
| 202 | end; |
| 203 | end; |
| 204 | |
| 205 | |
| 206 | procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase); |
| 207 | // Deserialize the Thrift object data from the byte array. |
| 208 | var iBytes : Int64; |
| 209 | begin |
| 210 | try |
| 211 | iBytes := Length(input); |
| 212 | FStream.Size := iBytes; |
Jens Geyer | 3e6be73 | 2025-06-04 22:31:55 +0200 | [diff] [blame^] | 213 | if iBytes > 0 then begin |
| 214 | Move( input[0], FStream.Memory^, iBytes); |
| 215 | Transport.ResetMessageSizeAndConsumedBytes(); // size has changed |
| 216 | Transport.UpdateKnownMessageSize(iBytes); |
| 217 | end; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 218 | |
| 219 | target.Read( FProtocol); |
| 220 | finally |
| 221 | FStream.Size := 0; // free any allocated memory |
| 222 | end; |
| 223 | end; |
| 224 | |
| 225 | |
| 226 | procedure TDeserializer.Deserialize( const input : TStream; const target : IBase); |
| 227 | // Deserialize the Thrift object data from the byte array. |
| 228 | const COPY_ENTIRE_STREAM = 0; |
| 229 | var before : Int64; |
| 230 | begin |
| 231 | try |
Jens Geyer | 3e6be73 | 2025-06-04 22:31:55 +0200 | [diff] [blame^] | 232 | if Assigned(input) then begin |
| 233 | before := FStream.Position; |
| 234 | ASSERT( before = 0); |
| 235 | FStream.CopyFrom( input, COPY_ENTIRE_STREAM); |
| 236 | FStream.Position := before; |
| 237 | Transport.ResetMessageSizeAndConsumedBytes(); // size has changed |
| 238 | Transport.UpdateKnownMessageSize(FStream.Size); |
| 239 | end; |
| 240 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 241 | target.Read( FProtocol); |
| 242 | finally |
| 243 | FStream.Size := 0; // free any allocated memory |
| 244 | end; |
| 245 | end; |
| 246 | |
| 247 | |
| 248 | end. |
| 249 | |