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 |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 41 | FStream : TMemoryStream; |
| 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 |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 62 | FStream : TMemoryStream; |
| 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; |
| 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; |
| 77 | end; |
| 78 | |
| 79 | |
| 80 | |
| 81 | implementation |
| 82 | |
| 83 | |
| 84 | { TSerializer } |
| 85 | |
| 86 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 87 | constructor TSerializer.Create( const aProtFact : IProtocolFactory; |
| 88 | const aTransFact : ITransportFactory; |
| 89 | const aConfig : IThriftConfiguration); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 90 | var adapter : IThriftStream; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 91 | protfact : IProtocolFactory; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 92 | begin |
| 93 | inherited Create; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 94 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 95 | FStream := TMemoryStream.Create; |
| 96 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 97 | |
| 98 | FTransport := TStreamTransportImpl.Create( nil, adapter, aConfig); |
| 99 | if aTransfact <> nil then FTransport := aTransfact.GetTransport( FTransport); |
| 100 | |
| 101 | if aProtFact <> nil |
| 102 | then protfact := aProtFact |
| 103 | else protfact := TBinaryProtocolImpl.TFactory.Create; |
| 104 | FProtocol := protfact.GetProtocol( FTransport); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 105 | |
| 106 | if not FTransport.IsOpen |
| 107 | then FTransport.Open; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 108 | end; |
| 109 | |
| 110 | |
| 111 | destructor TSerializer.Destroy; |
| 112 | begin |
| 113 | try |
| 114 | FProtocol := nil; |
| 115 | FTransport := nil; |
| 116 | FreeAndNil( FStream); |
| 117 | finally |
| 118 | inherited Destroy; |
| 119 | end; |
| 120 | end; |
| 121 | |
| 122 | |
| 123 | function TSerializer.Serialize( const input : IBase) : TBytes; |
| 124 | // Serialize the Thrift object into a byte array. The process is simple, |
| 125 | // just clear the byte array output, write the object into it, and grab the |
| 126 | // raw bytes. |
| 127 | var iBytes : Int64; |
| 128 | begin |
| 129 | try |
| 130 | FStream.Size := 0; |
| 131 | input.Write( FProtocol); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 132 | FTransport.Flush; |
| 133 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 134 | SetLength( result, FStream.Size); |
| 135 | iBytes := Length(result); |
| 136 | if iBytes > 0 |
| 137 | then Move( FStream.Memory^, result[0], iBytes); |
| 138 | finally |
| 139 | FStream.Size := 0; // free any allocated memory |
| 140 | end; |
| 141 | end; |
| 142 | |
| 143 | |
| 144 | procedure TSerializer.Serialize( const input : IBase; const aStm : TStream); |
| 145 | // Serialize the Thrift object into a byte array. The process is simple, |
| 146 | // 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] | 147 | // raw bytes. |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 148 | const COPY_ENTIRE_STREAM = 0; |
| 149 | begin |
| 150 | try |
| 151 | FStream.Size := 0; |
| 152 | input.Write( FProtocol); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 153 | FTransport.Flush; |
| 154 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 155 | aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM); |
| 156 | finally |
| 157 | FStream.Size := 0; // free any allocated memory |
| 158 | end; |
| 159 | end; |
| 160 | |
| 161 | |
| 162 | { TDeserializer } |
| 163 | |
| 164 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 165 | constructor TDeserializer.Create( const aProtFact : IProtocolFactory; |
| 166 | const aTransFact : ITransportFactory; |
| 167 | const aConfig : IThriftConfiguration); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 168 | var adapter : IThriftStream; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 169 | protfact : IProtocolFactory; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 170 | begin |
| 171 | inherited Create; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 172 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 173 | FStream := TMemoryStream.Create; |
| 174 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 175 | |
| 176 | FTransport := TStreamTransportImpl.Create( adapter, nil, aConfig); |
| 177 | if aTransfact <> nil then FTransport := aTransfact.GetTransport( FTransport); |
| 178 | |
| 179 | if aProtFact <> nil |
| 180 | then protfact := aProtFact |
| 181 | else protfact := TBinaryProtocolImpl.TFactory.Create; |
| 182 | FProtocol := protfact.GetProtocol( FTransport); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 183 | |
| 184 | if not FTransport.IsOpen |
| 185 | then FTransport.Open; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 186 | end; |
| 187 | |
| 188 | |
| 189 | destructor TDeserializer.Destroy; |
| 190 | begin |
| 191 | try |
| 192 | FProtocol := nil; |
| 193 | FTransport := nil; |
| 194 | FreeAndNil( FStream); |
| 195 | finally |
| 196 | inherited Destroy; |
| 197 | end; |
| 198 | end; |
| 199 | |
| 200 | |
| 201 | procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase); |
| 202 | // Deserialize the Thrift object data from the byte array. |
| 203 | var iBytes : Int64; |
| 204 | begin |
| 205 | try |
| 206 | iBytes := Length(input); |
| 207 | FStream.Size := iBytes; |
| 208 | if iBytes > 0 |
| 209 | then Move( input[0], FStream.Memory^, iBytes); |
| 210 | |
| 211 | target.Read( FProtocol); |
| 212 | finally |
| 213 | FStream.Size := 0; // free any allocated memory |
| 214 | end; |
| 215 | end; |
| 216 | |
| 217 | |
| 218 | procedure TDeserializer.Deserialize( const input : TStream; const target : IBase); |
| 219 | // Deserialize the Thrift object data from the byte array. |
| 220 | const COPY_ENTIRE_STREAM = 0; |
| 221 | var before : Int64; |
| 222 | begin |
| 223 | try |
| 224 | before := FStream.Position; |
| 225 | FStream.CopyFrom( input, COPY_ENTIRE_STREAM); |
| 226 | FStream.Position := before; |
| 227 | target.Read( FProtocol); |
| 228 | finally |
| 229 | FStream.Size := 0; // free any allocated memory |
| 230 | end; |
| 231 | end; |
| 232 | |
| 233 | |
| 234 | end. |
| 235 | |