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