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 |
| 39 | private |
| 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 | |
| 52 | // DTOR |
| 53 | destructor Destroy; override; |
| 54 | |
| 55 | // Serialize the Thrift object. |
| 56 | function Serialize( const input : IBase) : TBytes; overload; |
| 57 | procedure Serialize( const input : IBase; const aStm : TStream); overload; |
| 58 | end; |
| 59 | |
| 60 | |
| 61 | // Generic utility for easily deserializing objects from byte array or Stream. |
| 62 | TDeserializer = class |
| 63 | private |
| 64 | FStream : TMemoryStream; |
| 65 | FTransport : ITransport; |
| 66 | FProtocol : IProtocol; |
| 67 | |
| 68 | public |
| 69 | // Create a new TDeserializer that uses the TBinaryProtocol by default. |
| 70 | constructor Create; overload; |
| 71 | |
| 72 | // Create a new TDeserializer. |
| 73 | // It will use the TProtocol specified by the factory that is passed in. |
| 74 | constructor Create( const factory : IProtocolFactory); overload; |
| 75 | |
| 76 | // DTOR |
| 77 | destructor Destroy; override; |
| 78 | |
| 79 | // Deserialize the Thrift object data. |
| 80 | procedure Deserialize( const input : TBytes; const target : IBase); overload; |
| 81 | procedure Deserialize( const input : TStream; const target : IBase); overload; |
| 82 | end; |
| 83 | |
| 84 | |
| 85 | |
| 86 | implementation |
| 87 | |
| 88 | |
| 89 | { TSerializer } |
| 90 | |
| 91 | |
| 92 | constructor TSerializer.Create(); |
| 93 | // Create a new TSerializer that uses the TBinaryProtocol by default. |
| 94 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 95 | //no inherited; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 96 | Create( TBinaryProtocolImpl.TFactory.Create); |
| 97 | end; |
| 98 | |
| 99 | |
| 100 | constructor TSerializer.Create( const factory : IProtocolFactory); |
| 101 | // Create a new TSerializer. |
| 102 | // It will use the TProtocol specified by the factory that is passed in. |
| 103 | var adapter : IThriftStream; |
| 104 | begin |
| 105 | inherited Create; |
| 106 | FStream := TMemoryStream.Create; |
| 107 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
| 108 | FTransport := TStreamTransportImpl.Create( nil, adapter); |
| 109 | FProtocol := factory.GetProtocol( FTransport); |
| 110 | end; |
| 111 | |
| 112 | |
| 113 | destructor TSerializer.Destroy; |
| 114 | begin |
| 115 | try |
| 116 | FProtocol := nil; |
| 117 | FTransport := nil; |
| 118 | FreeAndNil( FStream); |
| 119 | finally |
| 120 | inherited Destroy; |
| 121 | end; |
| 122 | end; |
| 123 | |
| 124 | |
| 125 | function TSerializer.Serialize( const input : IBase) : TBytes; |
| 126 | // Serialize the Thrift object into a byte array. The process is simple, |
| 127 | // just clear the byte array output, write the object into it, and grab the |
| 128 | // raw bytes. |
| 129 | var iBytes : Int64; |
| 130 | begin |
| 131 | try |
| 132 | FStream.Size := 0; |
| 133 | input.Write( FProtocol); |
| 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); |
| 153 | aStm.CopyFrom( FStream, COPY_ENTIRE_STREAM); |
| 154 | finally |
| 155 | FStream.Size := 0; // free any allocated memory |
| 156 | end; |
| 157 | end; |
| 158 | |
| 159 | |
| 160 | { TDeserializer } |
| 161 | |
| 162 | |
| 163 | constructor TDeserializer.Create(); |
| 164 | // Create a new TDeserializer that uses the TBinaryProtocol by default. |
| 165 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 166 | //no inherited; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 167 | Create( TBinaryProtocolImpl.TFactory.Create); |
| 168 | end; |
| 169 | |
| 170 | |
| 171 | constructor TDeserializer.Create( const factory : IProtocolFactory); |
| 172 | // Create a new TDeserializer. |
| 173 | // It will use the TProtocol specified by the factory that is passed in. |
| 174 | var adapter : IThriftStream; |
| 175 | begin |
| 176 | inherited Create; |
| 177 | FStream := TMemoryStream.Create; |
| 178 | adapter := TThriftStreamAdapterDelphi.Create( FStream, FALSE); |
| 179 | FTransport := TStreamTransportImpl.Create( adapter, nil); |
| 180 | FProtocol := factory.GetProtocol( FTransport); |
| 181 | end; |
| 182 | |
| 183 | |
| 184 | destructor TDeserializer.Destroy; |
| 185 | begin |
| 186 | try |
| 187 | FProtocol := nil; |
| 188 | FTransport := nil; |
| 189 | FreeAndNil( FStream); |
| 190 | finally |
| 191 | inherited Destroy; |
| 192 | end; |
| 193 | end; |
| 194 | |
| 195 | |
| 196 | procedure TDeserializer.Deserialize( const input : TBytes; const target : IBase); |
| 197 | // Deserialize the Thrift object data from the byte array. |
| 198 | var iBytes : Int64; |
| 199 | begin |
| 200 | try |
| 201 | iBytes := Length(input); |
| 202 | FStream.Size := iBytes; |
| 203 | if iBytes > 0 |
| 204 | then Move( input[0], FStream.Memory^, iBytes); |
| 205 | |
| 206 | target.Read( FProtocol); |
| 207 | finally |
| 208 | FStream.Size := 0; // free any allocated memory |
| 209 | end; |
| 210 | end; |
| 211 | |
| 212 | |
| 213 | procedure TDeserializer.Deserialize( const input : TStream; const target : IBase); |
| 214 | // Deserialize the Thrift object data from the byte array. |
| 215 | const COPY_ENTIRE_STREAM = 0; |
| 216 | var before : Int64; |
| 217 | begin |
| 218 | try |
| 219 | before := FStream.Position; |
| 220 | FStream.CopyFrom( input, COPY_ENTIRE_STREAM); |
| 221 | FStream.Position := before; |
| 222 | target.Read( FProtocol); |
| 223 | finally |
| 224 | FStream.Size := 0; // free any allocated memory |
| 225 | end; |
| 226 | end; |
| 227 | |
| 228 | |
| 229 | end. |
| 230 | |