Jake Farrell | 2727422 | 2011-11-10 20:32:44 +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 | |
| 20 | {$SCOPEDENUMS ON} |
| 21 | |
| 22 | unit Thrift.Protocol.JSON; |
| 23 | |
| 24 | interface |
| 25 | |
| 26 | uses |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 27 | Character, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 28 | Classes, |
| 29 | SysUtils, |
| 30 | Math, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 31 | Generics.Collections, |
| 32 | Thrift.Transport, |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 33 | Thrift.Protocol, |
| 34 | Thrift.Utils; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 35 | |
| 36 | type |
| 37 | IJSONProtocol = interface( IProtocol) |
| 38 | ['{F0DAFDBD-692A-4B71-9736-F5D485A2178F}'] |
| 39 | // Read a byte that must match b; otherwise an exception is thrown. |
| 40 | procedure ReadJSONSyntaxChar( b : Byte); |
| 41 | end; |
| 42 | |
| 43 | // JSON protocol implementation for thrift. |
| 44 | // This is a full-featured protocol supporting Write and Read. |
| 45 | // Please see the C++ class header for a detailed description of the protocol's wire format. |
| 46 | // Adapted from the C# version. |
| 47 | TJSONProtocolImpl = class( TProtocolImpl, IJSONProtocol) |
| 48 | public |
| 49 | type |
| 50 | TFactory = class( TInterfacedObject, IProtocolFactory) |
| 51 | public |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 52 | function GetProtocol( const trans: ITransport): IProtocol; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 53 | end; |
| 54 | |
| 55 | private |
| 56 | class function GetTypeNameForTypeID(typeID : TType) : string; |
| 57 | class function GetTypeIDForTypeName( const name : string) : TType; |
| 58 | |
| 59 | protected |
| 60 | type |
| 61 | // Base class for tracking JSON contexts that may require |
| 62 | // inserting/Reading additional JSON syntax characters. |
| 63 | // This base context does nothing. |
| 64 | TJSONBaseContext = class |
| 65 | protected |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 66 | FProto : Pointer; // weak IJSONProtocol; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 67 | public |
| 68 | constructor Create( const aProto : IJSONProtocol); |
| 69 | procedure Write; virtual; |
| 70 | procedure Read; virtual; |
| 71 | function EscapeNumbers : Boolean; virtual; |
| 72 | end; |
| 73 | |
| 74 | // Context for JSON lists. |
| 75 | // Will insert/Read commas before each item except for the first one. |
| 76 | TJSONListContext = class( TJSONBaseContext) |
| 77 | private |
| 78 | FFirst : Boolean; |
| 79 | public |
| 80 | constructor Create( const aProto : IJSONProtocol); |
| 81 | procedure Write; override; |
| 82 | procedure Read; override; |
| 83 | end; |
| 84 | |
| 85 | // Context for JSON records. Will insert/Read colons before the value portion of each record |
| 86 | // pair, and commas before each key except the first. In addition, will indicate that numbers |
| 87 | // in the key position need to be escaped in quotes (since JSON keys must be strings). |
| 88 | TJSONPairContext = class( TJSONBaseContext) |
| 89 | private |
| 90 | FFirst, FColon : Boolean; |
| 91 | public |
| 92 | constructor Create( const aProto : IJSONProtocol); |
| 93 | procedure Write; override; |
| 94 | procedure Read; override; |
| 95 | function EscapeNumbers : Boolean; override; |
| 96 | end; |
| 97 | |
| 98 | // Holds up to one byte from the transport |
| 99 | TLookaheadReader = class |
| 100 | protected |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 101 | FProto : Pointer; // weak IJSONProtocol; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 102 | constructor Create( const aProto : IJSONProtocol); |
| 103 | |
| 104 | private |
| 105 | FHasData : Boolean; |
| 106 | FData : TBytes; |
| 107 | |
| 108 | public |
| 109 | // Return and consume the next byte to be Read, either taking it from the |
| 110 | // data buffer if present or getting it from the transport otherwise. |
| 111 | function Read : Byte; |
| 112 | |
| 113 | // Return the next byte to be Read without consuming, filling the data |
| 114 | // buffer if it has not been filled alReady. |
| 115 | function Peek : Byte; |
| 116 | end; |
| 117 | |
| 118 | protected |
| 119 | // Stack of nested contexts that we may be in |
| 120 | FContextStack : TStack<TJSONBaseContext>; |
| 121 | |
| 122 | // Current context that we are in |
| 123 | FContext : TJSONBaseContext; |
| 124 | |
| 125 | // Reader that manages a 1-byte buffer |
| 126 | FReader : TLookaheadReader; |
| 127 | |
| 128 | // Push/pop a new JSON context onto/from the stack. |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame] | 129 | procedure ResetContextStack; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 130 | procedure PushContext( const aCtx : TJSONBaseContext); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 131 | procedure PopContext; |
| 132 | |
| 133 | public |
| 134 | // TJSONProtocolImpl Constructor |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 135 | constructor Create( const aTrans : ITransport); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 136 | destructor Destroy; override; |
| 137 | |
| 138 | protected |
| 139 | // IJSONProtocol |
| 140 | // Read a byte that must match b; otherwise an exception is thrown. |
| 141 | procedure ReadJSONSyntaxChar( b : Byte); |
| 142 | |
| 143 | private |
| 144 | // Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its corresponding hex value |
| 145 | class function HexVal( ch : Byte) : Byte; |
| 146 | |
| 147 | // Convert a byte containing a hex value to its corresponding hex character |
| 148 | class function HexChar( val : Byte) : Byte; |
| 149 | |
| 150 | // Write the bytes in array buf as a JSON characters, escaping as needed |
| 151 | procedure WriteJSONString( const b : TBytes); overload; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 152 | procedure WriteJSONString( const str : string); overload; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 153 | |
| 154 | // Write out number as a JSON value. If the context dictates so, it will be |
| 155 | // wrapped in quotes to output as a JSON string. |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 156 | procedure WriteJSONInteger( const num : Int64); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 157 | |
| 158 | // Write out a double as a JSON value. If it is NaN or infinity or if the |
| 159 | // context dictates escaping, Write out as JSON string. |
| 160 | procedure WriteJSONDouble( const num : Double); |
| 161 | |
| 162 | // Write out contents of byte array b as a JSON string with base-64 encoded data |
| 163 | procedure WriteJSONBase64( const b : TBytes); |
| 164 | |
| 165 | procedure WriteJSONObjectStart; |
| 166 | procedure WriteJSONObjectEnd; |
| 167 | procedure WriteJSONArrayStart; |
| 168 | procedure WriteJSONArrayEnd; |
| 169 | |
| 170 | public |
| 171 | // IProtocol |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 172 | procedure WriteMessageBegin( const aMsg : IMessage); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 173 | procedure WriteMessageEnd; override; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 174 | procedure WriteStructBegin( const struc: IStruct); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 175 | procedure WriteStructEnd; override; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 176 | procedure WriteFieldBegin( const field: IField); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 177 | procedure WriteFieldEnd; override; |
| 178 | procedure WriteFieldStop; override; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 179 | procedure WriteMapBegin( const map: IMap); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 180 | procedure WriteMapEnd; override; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 181 | procedure WriteListBegin( const list: IList); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 182 | procedure WriteListEnd(); override; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 183 | procedure WriteSetBegin( const set_: ISet ); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 184 | procedure WriteSetEnd(); override; |
| 185 | procedure WriteBool( b: Boolean); override; |
| 186 | procedure WriteByte( b: ShortInt); override; |
| 187 | procedure WriteI16( i16: SmallInt); override; |
| 188 | procedure WriteI32( i32: Integer); override; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 189 | procedure WriteI64( const i64: Int64); override; |
| 190 | procedure WriteDouble( const d: Double); override; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 191 | procedure WriteString( const s: string ); override; |
| 192 | procedure WriteBinary( const b: TBytes); override; |
| 193 | // |
| 194 | function ReadMessageBegin: IMessage; override; |
| 195 | procedure ReadMessageEnd(); override; |
| 196 | function ReadStructBegin: IStruct; override; |
| 197 | procedure ReadStructEnd; override; |
| 198 | function ReadFieldBegin: IField; override; |
| 199 | procedure ReadFieldEnd(); override; |
| 200 | function ReadMapBegin: IMap; override; |
| 201 | procedure ReadMapEnd(); override; |
| 202 | function ReadListBegin: IList; override; |
| 203 | procedure ReadListEnd(); override; |
| 204 | function ReadSetBegin: ISet; override; |
| 205 | procedure ReadSetEnd(); override; |
| 206 | function ReadBool: Boolean; override; |
| 207 | function ReadByte: ShortInt; override; |
| 208 | function ReadI16: SmallInt; override; |
| 209 | function ReadI32: Integer; override; |
| 210 | function ReadI64: Int64; override; |
| 211 | function ReadDouble:Double; override; |
| 212 | function ReadString : string; override; |
| 213 | function ReadBinary: TBytes; override; |
| 214 | |
| 215 | |
| 216 | private |
| 217 | // Reading methods. |
| 218 | |
| 219 | // Read in a JSON string, unescaping as appropriate. |
| 220 | // Skip Reading from the context if skipContext is true. |
| 221 | function ReadJSONString( skipContext : Boolean) : TBytes; |
| 222 | |
| 223 | // Return true if the given byte could be a valid part of a JSON number. |
| 224 | function IsJSONNumeric( b : Byte) : Boolean; |
| 225 | |
| 226 | // Read in a sequence of characters that are all valid in JSON numbers. Does |
| 227 | // not do a complete regex check to validate that this is actually a number. |
| 228 | function ReadJSONNumericChars : String; |
| 229 | |
| 230 | // Read in a JSON number. If the context dictates, Read in enclosing quotes. |
| 231 | function ReadJSONInteger : Int64; |
| 232 | |
| 233 | // Read in a JSON double value. Throw if the value is not wrapped in quotes |
| 234 | // when expected or if wrapped in quotes when not expected. |
| 235 | function ReadJSONDouble : Double; |
| 236 | |
| 237 | // Read in a JSON string containing base-64 encoded data and decode it. |
| 238 | function ReadJSONBase64 : TBytes; |
| 239 | |
| 240 | procedure ReadJSONObjectStart; |
| 241 | procedure ReadJSONObjectEnd; |
| 242 | procedure ReadJSONArrayStart; |
| 243 | procedure ReadJSONArrayEnd; |
| 244 | end; |
| 245 | |
| 246 | |
| 247 | implementation |
| 248 | |
| 249 | var |
| 250 | COMMA : TBytes; |
| 251 | COLON : TBytes; |
| 252 | LBRACE : TBytes; |
| 253 | RBRACE : TBytes; |
| 254 | LBRACKET : TBytes; |
| 255 | RBRACKET : TBytes; |
| 256 | QUOTE : TBytes; |
| 257 | BACKSLASH : TBytes; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 258 | ESCSEQ : TBytes; |
| 259 | |
| 260 | const |
| 261 | VERSION = 1; |
| 262 | JSON_CHAR_TABLE : array[0..$2F] of Byte |
| 263 | = (0,0,0,0, 0,0,0,0, Byte('b'),Byte('t'),Byte('n'),0, Byte('f'),Byte('r'),0,0, |
| 264 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
| 265 | 1,1,Byte('"'),1, 1,1,1,1, 1,1,1,1, 1,1,1,1); |
| 266 | |
Jens Geyer | 2136694 | 2013-12-30 22:04:51 +0100 | [diff] [blame] | 267 | ESCAPE_CHARS = '"\/btnfr'; |
| 268 | ESCAPE_CHAR_VALS = '"\/'#8#9#10#12#13; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 269 | |
| 270 | DEF_STRING_SIZE = 16; |
| 271 | |
| 272 | NAME_BOOL = 'tf'; |
| 273 | NAME_BYTE = 'i8'; |
| 274 | NAME_I16 = 'i16'; |
| 275 | NAME_I32 = 'i32'; |
| 276 | NAME_I64 = 'i64'; |
| 277 | NAME_DOUBLE = 'dbl'; |
| 278 | NAME_STRUCT = 'rec'; |
| 279 | NAME_STRING = 'str'; |
| 280 | NAME_MAP = 'map'; |
| 281 | NAME_LIST = 'lst'; |
| 282 | NAME_SET = 'set'; |
| 283 | |
| 284 | INVARIANT_CULTURE : TFormatSettings |
| 285 | = ( ThousandSeparator: ','; |
| 286 | DecimalSeparator: '.'); |
| 287 | |
| 288 | |
| 289 | |
| 290 | //--- TJSONProtocolImpl ---------------------- |
| 291 | |
| 292 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 293 | function TJSONProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 294 | begin |
| 295 | result := TJSONProtocolImpl.Create(trans); |
| 296 | end; |
| 297 | |
| 298 | class function TJSONProtocolImpl.GetTypeNameForTypeID(typeID : TType) : string; |
| 299 | begin |
| 300 | case typeID of |
| 301 | TType.Bool_: result := NAME_BOOL; |
| 302 | TType.Byte_: result := NAME_BYTE; |
| 303 | TType.I16: result := NAME_I16; |
| 304 | TType.I32: result := NAME_I32; |
| 305 | TType.I64: result := NAME_I64; |
| 306 | TType.Double_: result := NAME_DOUBLE; |
| 307 | TType.String_: result := NAME_STRING; |
| 308 | TType.Struct: result := NAME_STRUCT; |
| 309 | TType.Map: result := NAME_MAP; |
| 310 | TType.Set_: result := NAME_SET; |
| 311 | TType.List: result := NAME_LIST; |
| 312 | else |
| 313 | raise TProtocolException.Create( TProtocolException.NOT_IMPLEMENTED, 'Unrecognized type ('+IntToStr(Ord(typeID))+')'); |
| 314 | end; |
| 315 | end; |
| 316 | |
| 317 | |
| 318 | class function TJSONProtocolImpl.GetTypeIDForTypeName( const name : string) : TType; |
| 319 | begin |
| 320 | if name = NAME_BOOL then result := TType.Bool_ |
| 321 | else if name = NAME_BYTE then result := TType.Byte_ |
| 322 | else if name = NAME_I16 then result := TType.I16 |
| 323 | else if name = NAME_I32 then result := TType.I32 |
| 324 | else if name = NAME_I64 then result := TType.I64 |
| 325 | else if name = NAME_DOUBLE then result := TType.Double_ |
| 326 | else if name = NAME_STRUCT then result := TType.Struct |
| 327 | else if name = NAME_STRING then result := TType.String_ |
| 328 | else if name = NAME_MAP then result := TType.Map |
| 329 | else if name = NAME_LIST then result := TType.List |
| 330 | else if name = NAME_SET then result := TType.Set_ |
| 331 | else raise TProtocolException.Create( TProtocolException.NOT_IMPLEMENTED, 'Unrecognized type ('+name+')'); |
| 332 | end; |
| 333 | |
| 334 | |
| 335 | constructor TJSONProtocolImpl.TJSONBaseContext.Create( const aProto : IJSONProtocol); |
| 336 | begin |
| 337 | inherited Create; |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 338 | FProto := Pointer(aProto); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 339 | end; |
| 340 | |
| 341 | |
| 342 | procedure TJSONProtocolImpl.TJSONBaseContext.Write; |
| 343 | begin |
| 344 | // nothing |
| 345 | end; |
| 346 | |
| 347 | |
| 348 | procedure TJSONProtocolImpl.TJSONBaseContext.Read; |
| 349 | begin |
| 350 | // nothing |
| 351 | end; |
| 352 | |
| 353 | |
| 354 | function TJSONProtocolImpl.TJSONBaseContext.EscapeNumbers : Boolean; |
| 355 | begin |
| 356 | result := FALSE; |
| 357 | end; |
| 358 | |
| 359 | |
| 360 | constructor TJSONProtocolImpl.TJSONListContext.Create( const aProto : IJSONProtocol); |
| 361 | begin |
| 362 | inherited Create( aProto); |
| 363 | FFirst := TRUE; |
| 364 | end; |
| 365 | |
| 366 | |
| 367 | procedure TJSONProtocolImpl.TJSONListContext.Write; |
| 368 | begin |
| 369 | if FFirst |
| 370 | then FFirst := FALSE |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 371 | else IJSONProtocol(FProto).Transport.Write( COMMA); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 372 | end; |
| 373 | |
| 374 | |
| 375 | procedure TJSONProtocolImpl.TJSONListContext.Read; |
| 376 | begin |
| 377 | if FFirst |
| 378 | then FFirst := FALSE |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 379 | else IJSONProtocol(FProto).ReadJSONSyntaxChar( COMMA[0]); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 380 | end; |
| 381 | |
| 382 | |
| 383 | constructor TJSONProtocolImpl.TJSONPairContext.Create( const aProto : IJSONProtocol); |
| 384 | begin |
| 385 | inherited Create( aProto); |
| 386 | FFirst := TRUE; |
| 387 | FColon := TRUE; |
| 388 | end; |
| 389 | |
| 390 | |
| 391 | procedure TJSONProtocolImpl.TJSONPairContext.Write; |
| 392 | begin |
| 393 | if FFirst then begin |
| 394 | FFirst := FALSE; |
| 395 | FColon := TRUE; |
| 396 | end |
| 397 | else begin |
| 398 | if FColon |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 399 | then IJSONProtocol(FProto).Transport.Write( COLON) |
| 400 | else IJSONProtocol(FProto).Transport.Write( COMMA); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 401 | FColon := not FColon; |
| 402 | end; |
| 403 | end; |
| 404 | |
| 405 | |
| 406 | procedure TJSONProtocolImpl.TJSONPairContext.Read; |
| 407 | begin |
| 408 | if FFirst then begin |
| 409 | FFirst := FALSE; |
| 410 | FColon := TRUE; |
| 411 | end |
| 412 | else begin |
| 413 | if FColon |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 414 | then IJSONProtocol(FProto).ReadJSONSyntaxChar( COLON[0]) |
| 415 | else IJSONProtocol(FProto).ReadJSONSyntaxChar( COMMA[0]); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 416 | FColon := not FColon; |
| 417 | end; |
| 418 | end; |
| 419 | |
| 420 | |
| 421 | function TJSONProtocolImpl.TJSONPairContext.EscapeNumbers : Boolean; |
| 422 | begin |
| 423 | result := FColon; |
| 424 | end; |
| 425 | |
| 426 | |
| 427 | constructor TJSONProtocolImpl.TLookaheadReader.Create( const aProto : IJSONProtocol); |
| 428 | begin |
| 429 | inherited Create; |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 430 | FProto := Pointer(aProto); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 431 | FHasData := FALSE; |
| 432 | end; |
| 433 | |
| 434 | |
| 435 | function TJSONProtocolImpl.TLookaheadReader.Read : Byte; |
| 436 | begin |
| 437 | if FHasData |
| 438 | then FHasData := FALSE |
| 439 | else begin |
| 440 | SetLength( FData, 1); |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 441 | IJSONProtocol(FProto).Transport.ReadAll( FData, 0, 1); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 442 | end; |
| 443 | result := FData[0]; |
| 444 | end; |
| 445 | |
| 446 | |
| 447 | function TJSONProtocolImpl.TLookaheadReader.Peek : Byte; |
| 448 | begin |
| 449 | if not FHasData then begin |
| 450 | SetLength( FData, 1); |
Roger Meier | febe845 | 2012-06-06 10:32:24 +0000 | [diff] [blame] | 451 | IJSONProtocol(FProto).Transport.ReadAll( FData, 0, 1); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 452 | FHasData := TRUE; |
| 453 | end; |
| 454 | result := FData[0]; |
| 455 | end; |
| 456 | |
| 457 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 458 | constructor TJSONProtocolImpl.Create( const aTrans : ITransport); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 459 | begin |
| 460 | inherited Create( aTrans); |
| 461 | |
| 462 | // Stack of nested contexts that we may be in |
| 463 | FContextStack := TStack<TJSONBaseContext>.Create; |
| 464 | |
| 465 | FContext := TJSONBaseContext.Create( Self); |
| 466 | FReader := TLookaheadReader.Create( Self); |
| 467 | end; |
| 468 | |
| 469 | |
| 470 | destructor TJSONProtocolImpl.Destroy; |
| 471 | begin |
| 472 | try |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame] | 473 | ResetContextStack; // free any contents |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 474 | FreeAndNil( FReader); |
| 475 | FreeAndNil( FContext); |
| 476 | FreeAndNil( FContextStack); |
| 477 | finally |
| 478 | inherited Destroy; |
| 479 | end; |
| 480 | end; |
| 481 | |
| 482 | |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame] | 483 | procedure TJSONProtocolImpl.ResetContextStack; |
| 484 | begin |
| 485 | while FContextStack.Count > 0 |
| 486 | do PopContext; |
| 487 | end; |
| 488 | |
| 489 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 490 | procedure TJSONProtocolImpl.PushContext( const aCtx : TJSONBaseContext); |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame] | 491 | begin |
| 492 | FContextStack.Push( FContext); |
| 493 | FContext := aCtx; |
| 494 | end; |
| 495 | |
| 496 | |
| 497 | procedure TJSONProtocolImpl.PopContext; |
| 498 | begin |
| 499 | FreeAndNil(FContext); |
| 500 | FContext := FContextStack.Pop; |
| 501 | end; |
| 502 | |
| 503 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 504 | procedure TJSONProtocolImpl.ReadJSONSyntaxChar( b : Byte); |
| 505 | var ch : Byte; |
| 506 | begin |
| 507 | ch := FReader.Read; |
| 508 | if (ch <> b) |
| 509 | then raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Unexpected character ('+Char(ch)+')'); |
| 510 | end; |
| 511 | |
| 512 | |
| 513 | class function TJSONProtocolImpl.HexVal( ch : Byte) : Byte; |
| 514 | var i : Integer; |
| 515 | begin |
| 516 | i := StrToIntDef( '$0'+Char(ch), -1); |
| 517 | if (0 <= i) and (i < $10) |
| 518 | then result := i |
| 519 | else raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Expected hex character ('+Char(ch)+')'); |
| 520 | end; |
| 521 | |
| 522 | |
| 523 | class function TJSONProtocolImpl.HexChar( val : Byte) : Byte; |
| 524 | const HEXCHARS = '0123456789ABCDEF'; |
| 525 | begin |
| 526 | result := Byte( PChar(HEXCHARS)[val and $0F]); |
| 527 | ASSERT( Pos( Char(result), HEXCHARS) > 0); |
| 528 | end; |
| 529 | |
| 530 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 531 | procedure TJSONProtocolImpl.WriteJSONString( const str : string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 532 | begin |
| 533 | WriteJSONString( SysUtils.TEncoding.UTF8.GetBytes( str)); |
| 534 | end; |
| 535 | |
| 536 | |
| 537 | procedure TJSONProtocolImpl.WriteJSONString( const b : TBytes); |
| 538 | var i : Integer; |
| 539 | tmp : TBytes; |
| 540 | begin |
| 541 | FContext.Write; |
| 542 | Transport.Write( QUOTE); |
| 543 | for i := 0 to Length(b)-1 do begin |
| 544 | |
| 545 | if (b[i] and $00FF) >= $30 then begin |
| 546 | |
| 547 | if (b[i] = BACKSLASH[0]) then begin |
| 548 | Transport.Write( BACKSLASH); |
| 549 | Transport.Write( BACKSLASH); |
| 550 | end |
| 551 | else begin |
| 552 | Transport.Write( b, i, 1); |
| 553 | end; |
| 554 | |
| 555 | end |
| 556 | else begin |
| 557 | SetLength( tmp, 2); |
| 558 | tmp[0] := JSON_CHAR_TABLE[b[i]]; |
| 559 | if (tmp[0] = 1) then begin |
| 560 | Transport.Write( b, i, 1) |
| 561 | end |
| 562 | else if (tmp[0] > 1) then begin |
| 563 | Transport.Write( BACKSLASH); |
| 564 | Transport.Write( tmp, 0, 1); |
| 565 | end |
| 566 | else begin |
| 567 | Transport.Write( ESCSEQ); |
| 568 | tmp[0] := HexChar( b[i] div $10); |
| 569 | tmp[1] := HexChar( b[i]); |
| 570 | Transport.Write( tmp, 0, 2); |
| 571 | end; |
| 572 | end; |
| 573 | end; |
| 574 | Transport.Write( QUOTE); |
| 575 | end; |
| 576 | |
| 577 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 578 | procedure TJSONProtocolImpl.WriteJSONInteger( const num : Int64); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 579 | var str : String; |
| 580 | escapeNum : Boolean; |
| 581 | begin |
| 582 | FContext.Write; |
| 583 | str := IntToStr(num); |
| 584 | |
| 585 | escapeNum := FContext.EscapeNumbers; |
| 586 | if escapeNum |
| 587 | then Transport.Write( QUOTE); |
| 588 | |
| 589 | Transport.Write( SysUtils.TEncoding.UTF8.GetBytes( str)); |
| 590 | |
| 591 | if escapeNum |
| 592 | then Transport.Write( QUOTE); |
| 593 | end; |
| 594 | |
| 595 | |
| 596 | procedure TJSONProtocolImpl.WriteJSONDouble( const num : Double); |
| 597 | var str : string; |
| 598 | special : Boolean; |
| 599 | escapeNum : Boolean; |
| 600 | begin |
| 601 | FContext.Write; |
| 602 | |
| 603 | str := FloatToStr( num, INVARIANT_CULTURE); |
| 604 | special := FALSE; |
| 605 | |
| 606 | case UpCase(str[1]) of |
| 607 | 'N' : special := TRUE; // NaN |
| 608 | 'I' : special := TRUE; // Infinity |
| 609 | '-' : special := (UpCase(str[2]) = 'I'); // -Infinity |
| 610 | end; |
| 611 | |
| 612 | escapeNum := special or FContext.EscapeNumbers; |
| 613 | |
| 614 | |
| 615 | if escapeNum |
| 616 | then Transport.Write( QUOTE); |
| 617 | |
| 618 | Transport.Write( SysUtils.TEncoding.UTF8.GetBytes( str)); |
| 619 | |
| 620 | if escapeNum |
| 621 | then Transport.Write( QUOTE); |
| 622 | end; |
| 623 | |
| 624 | |
| 625 | procedure TJSONProtocolImpl.WriteJSONBase64( const b : TBytes); |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 626 | var len, off, cnt : Integer; |
| 627 | tmpBuf : TBytes; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 628 | begin |
| 629 | FContext.Write; |
| 630 | Transport.Write( QUOTE); |
| 631 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 632 | len := Length(b); |
| 633 | off := 0; |
| 634 | SetLength( tmpBuf, 4); |
| 635 | |
| 636 | while len >= 3 do begin |
| 637 | // Encode 3 bytes at a time |
| 638 | Base64Utils.Encode( b, off, 3, tmpBuf, 0); |
| 639 | Transport.Write( tmpBuf, 0, 4); |
| 640 | Inc( off, 3); |
| 641 | Dec( len, 3); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 642 | end; |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 643 | |
| 644 | // Encode remainder, if any |
| 645 | if len > 0 then begin |
| 646 | cnt := Base64Utils.Encode( b, off, len, tmpBuf, 0); |
| 647 | Transport.Write( tmpBuf, 0, cnt); |
| 648 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 649 | |
| 650 | Transport.Write( QUOTE); |
| 651 | end; |
| 652 | |
| 653 | |
| 654 | procedure TJSONProtocolImpl.WriteJSONObjectStart; |
| 655 | begin |
| 656 | FContext.Write; |
| 657 | Transport.Write( LBRACE); |
| 658 | PushContext( TJSONPairContext.Create( Self)); |
| 659 | end; |
| 660 | |
| 661 | |
| 662 | procedure TJSONProtocolImpl.WriteJSONObjectEnd; |
| 663 | begin |
| 664 | PopContext; |
| 665 | Transport.Write( RBRACE); |
| 666 | end; |
| 667 | |
| 668 | |
| 669 | procedure TJSONProtocolImpl.WriteJSONArrayStart; |
| 670 | begin |
| 671 | FContext.Write; |
| 672 | Transport.Write( LBRACKET); |
| 673 | PushContext( TJSONListContext.Create( Self)); |
| 674 | end; |
| 675 | |
| 676 | |
| 677 | procedure TJSONProtocolImpl.WriteJSONArrayEnd; |
| 678 | begin |
| 679 | PopContext; |
| 680 | Transport.Write( RBRACKET); |
| 681 | end; |
| 682 | |
| 683 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 684 | procedure TJSONProtocolImpl.WriteMessageBegin( const aMsg : IMessage); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 685 | begin |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame] | 686 | ResetContextStack; // THRIFT-1473 |
| 687 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 688 | WriteJSONArrayStart; |
| 689 | WriteJSONInteger(VERSION); |
| 690 | |
| 691 | WriteJSONString( SysUtils.TEncoding.UTF8.GetBytes( aMsg.Name)); |
| 692 | |
| 693 | WriteJSONInteger( LongInt( aMsg.Type_)); |
| 694 | WriteJSONInteger( aMsg.SeqID); |
| 695 | end; |
| 696 | |
| 697 | procedure TJSONProtocolImpl.WriteMessageEnd; |
| 698 | begin |
| 699 | WriteJSONArrayEnd; |
| 700 | end; |
| 701 | |
| 702 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 703 | procedure TJSONProtocolImpl.WriteStructBegin( const struc: IStruct); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 704 | begin |
| 705 | WriteJSONObjectStart; |
| 706 | end; |
| 707 | |
| 708 | |
| 709 | procedure TJSONProtocolImpl.WriteStructEnd; |
| 710 | begin |
| 711 | WriteJSONObjectEnd; |
| 712 | end; |
| 713 | |
| 714 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 715 | procedure TJSONProtocolImpl.WriteFieldBegin( const field : IField); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 716 | begin |
| 717 | WriteJSONInteger(field.ID); |
| 718 | WriteJSONObjectStart; |
| 719 | WriteJSONString( GetTypeNameForTypeID(field.Type_)); |
| 720 | end; |
| 721 | |
| 722 | |
| 723 | procedure TJSONProtocolImpl.WriteFieldEnd; |
| 724 | begin |
| 725 | WriteJSONObjectEnd; |
| 726 | end; |
| 727 | |
| 728 | |
| 729 | procedure TJSONProtocolImpl.WriteFieldStop; |
| 730 | begin |
| 731 | // nothing to do |
| 732 | end; |
| 733 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 734 | procedure TJSONProtocolImpl.WriteMapBegin( const map: IMap); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 735 | begin |
| 736 | WriteJSONArrayStart; |
| 737 | WriteJSONString( GetTypeNameForTypeID( map.KeyType)); |
| 738 | WriteJSONString( GetTypeNameForTypeID( map.ValueType)); |
| 739 | WriteJSONInteger( map.Count); |
| 740 | WriteJSONObjectStart; |
| 741 | end; |
| 742 | |
| 743 | |
| 744 | procedure TJSONProtocolImpl.WriteMapEnd; |
| 745 | begin |
| 746 | WriteJSONObjectEnd; |
| 747 | WriteJSONArrayEnd; |
| 748 | end; |
| 749 | |
| 750 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 751 | procedure TJSONProtocolImpl.WriteListBegin( const list: IList); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 752 | begin |
| 753 | WriteJSONArrayStart; |
| 754 | WriteJSONString( GetTypeNameForTypeID( list.ElementType)); |
| 755 | WriteJSONInteger(list.Count); |
| 756 | end; |
| 757 | |
| 758 | |
| 759 | procedure TJSONProtocolImpl.WriteListEnd; |
| 760 | begin |
| 761 | WriteJSONArrayEnd; |
| 762 | end; |
| 763 | |
| 764 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 765 | procedure TJSONProtocolImpl.WriteSetBegin( const set_: ISet); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 766 | begin |
| 767 | WriteJSONArrayStart; |
| 768 | WriteJSONString( GetTypeNameForTypeID( set_.ElementType)); |
| 769 | WriteJSONInteger( set_.Count); |
| 770 | end; |
| 771 | |
| 772 | |
| 773 | procedure TJSONProtocolImpl.WriteSetEnd; |
| 774 | begin |
| 775 | WriteJSONArrayEnd; |
| 776 | end; |
| 777 | |
| 778 | procedure TJSONProtocolImpl.WriteBool( b: Boolean); |
| 779 | begin |
| 780 | if b |
| 781 | then WriteJSONInteger( 1) |
| 782 | else WriteJSONInteger( 0); |
| 783 | end; |
| 784 | |
| 785 | procedure TJSONProtocolImpl.WriteByte( b: ShortInt); |
| 786 | begin |
| 787 | WriteJSONInteger( b); |
| 788 | end; |
| 789 | |
| 790 | procedure TJSONProtocolImpl.WriteI16( i16: SmallInt); |
| 791 | begin |
| 792 | WriteJSONInteger( i16); |
| 793 | end; |
| 794 | |
| 795 | procedure TJSONProtocolImpl.WriteI32( i32: Integer); |
| 796 | begin |
| 797 | WriteJSONInteger( i32); |
| 798 | end; |
| 799 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 800 | procedure TJSONProtocolImpl.WriteI64( const i64: Int64); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 801 | begin |
| 802 | WriteJSONInteger(i64); |
| 803 | end; |
| 804 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 805 | procedure TJSONProtocolImpl.WriteDouble( const d: Double); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 806 | begin |
| 807 | WriteJSONDouble( d); |
| 808 | end; |
| 809 | |
| 810 | procedure TJSONProtocolImpl.WriteString( const s: string ); |
| 811 | begin |
| 812 | WriteJSONString( SysUtils.TEncoding.UTF8.GetBytes( s)); |
| 813 | end; |
| 814 | |
| 815 | procedure TJSONProtocolImpl.WriteBinary( const b: TBytes); |
| 816 | begin |
| 817 | WriteJSONBase64( b); |
| 818 | end; |
| 819 | |
| 820 | |
| 821 | function TJSONProtocolImpl.ReadJSONString( skipContext : Boolean) : TBytes; |
| 822 | var buffer : TMemoryStream; |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 823 | ch : Byte; |
| 824 | wch : Word; |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 825 | highSurogate: Char; |
| 826 | surrogatePairs: Array[0..1] of Char; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 827 | off : Integer; |
| 828 | tmp : TBytes; |
| 829 | begin |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 830 | highSurogate := #0; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 831 | buffer := TMemoryStream.Create; |
| 832 | try |
| 833 | if not skipContext |
| 834 | then FContext.Read; |
| 835 | |
| 836 | ReadJSONSyntaxChar( QUOTE[0]); |
| 837 | |
| 838 | while TRUE do begin |
| 839 | ch := FReader.Read; |
| 840 | |
| 841 | if (ch = QUOTE[0]) |
| 842 | then Break; |
| 843 | |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 844 | // check for escapes |
| 845 | if (ch <> ESCSEQ[0]) then begin |
| 846 | buffer.Write( ch, 1); |
| 847 | Continue; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 848 | end; |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 849 | |
| 850 | // distuinguish between \uNNNN and \? |
| 851 | ch := FReader.Read; |
| 852 | if (ch <> ESCSEQ[1]) |
| 853 | then begin |
| 854 | off := Pos( Char(ch), ESCAPE_CHARS); |
| 855 | if off < 1 |
| 856 | then raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Expected control char'); |
| 857 | ch := Byte( ESCAPE_CHAR_VALS[off]); |
| 858 | buffer.Write( ch, 1); |
| 859 | Continue; |
| 860 | end; |
| 861 | |
| 862 | // it is \uXXXX |
| 863 | SetLength( tmp, 4); |
| 864 | Transport.ReadAll( tmp, 0, 4); |
| 865 | wch := (HexVal(tmp[0]) shl 12) |
| 866 | + (HexVal(tmp[1]) shl 8) |
| 867 | + (HexVal(tmp[2]) shl 4) |
| 868 | + HexVal(tmp[3]); |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 869 | |
Jens Geyer | 7bb44a3 | 2014-02-07 22:24:37 +0100 | [diff] [blame] | 870 | // we need to make UTF8 bytes from it, to be decoded later |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 871 | if CharUtils.IsHighSurrogate(char(wch)) then begin |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 872 | if highSurogate <> #0 |
| 873 | then raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Expected low surrogate char'); |
| 874 | highSurogate := char(wch); |
| 875 | end |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 876 | else if CharUtils.IsLowSurrogate(char(wch)) then begin |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 877 | if highSurogate = #0 |
| 878 | then TProtocolException.Create( TProtocolException.INVALID_DATA, 'Expected high surrogate char'); |
| 879 | surrogatePairs[0] := highSurogate; |
| 880 | surrogatePairs[1] := char(wch); |
| 881 | tmp := TEncoding.UTF8.GetBytes(surrogatePairs); |
| 882 | buffer.Write( tmp[0], Length(tmp)); |
| 883 | highSurogate := #0; |
| 884 | end |
| 885 | else begin |
| 886 | tmp := SysUtils.TEncoding.UTF8.GetBytes(Char(wch)); |
| 887 | buffer.Write( tmp[0], Length(tmp)); |
| 888 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 889 | end; |
| 890 | |
Phongphan Phuttha | a6509f7 | 2015-10-31 01:09:47 +0700 | [diff] [blame] | 891 | if highSurogate <> #0 |
| 892 | then raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Expected low surrogate char'); |
| 893 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 894 | SetLength( result, buffer.Size); |
Jake Farrell | a2a9ee9 | 2011-12-15 20:50:31 +0000 | [diff] [blame] | 895 | if buffer.Size > 0 then Move( buffer.Memory^, result[0], Length(result)); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 896 | |
| 897 | finally |
| 898 | buffer.Free; |
| 899 | end; |
| 900 | end; |
| 901 | |
| 902 | |
| 903 | function TJSONProtocolImpl.IsJSONNumeric( b : Byte) : Boolean; |
| 904 | const NUMCHARS = ['+','-','.','0','1','2','3','4','5','6','7','8','9','E','e']; |
| 905 | begin |
| 906 | result := CharInSet( Char(b), NUMCHARS); |
| 907 | end; |
| 908 | |
| 909 | |
| 910 | function TJSONProtocolImpl.ReadJSONNumericChars : string; |
| 911 | var strbld : TThriftStringBuilder; |
| 912 | ch : Byte; |
| 913 | begin |
| 914 | strbld := TThriftStringBuilder.Create; |
| 915 | try |
| 916 | while TRUE do begin |
| 917 | ch := FReader.Peek; |
| 918 | if IsJSONNumeric(ch) |
| 919 | then strbld.Append( Char(FReader.Read)) |
| 920 | else Break; |
| 921 | end; |
| 922 | result := strbld.ToString; |
| 923 | |
| 924 | finally |
| 925 | strbld.Free; |
| 926 | end; |
| 927 | end; |
| 928 | |
| 929 | |
| 930 | function TJSONProtocolImpl.ReadJSONInteger : Int64; |
| 931 | var str : string; |
| 932 | begin |
| 933 | FContext.Read; |
| 934 | if FContext.EscapeNumbers |
| 935 | then ReadJSONSyntaxChar( QUOTE[0]); |
| 936 | |
| 937 | str := ReadJSONNumericChars; |
| 938 | |
| 939 | if FContext.EscapeNumbers |
| 940 | then ReadJSONSyntaxChar( QUOTE[0]); |
| 941 | |
| 942 | try |
| 943 | result := StrToInt64(str); |
| 944 | except |
| 945 | on e:Exception do begin |
| 946 | raise TProtocolException.Create( TProtocolException.INVALID_DATA, |
| 947 | 'Bad data encounted in numeric data ('+str+') ('+e.Message+')'); |
| 948 | end; |
| 949 | end; |
| 950 | end; |
| 951 | |
| 952 | |
| 953 | function TJSONProtocolImpl.ReadJSONDouble : Double; |
| 954 | var dub : Double; |
| 955 | str : string; |
| 956 | begin |
| 957 | FContext.Read; |
| 958 | |
| 959 | if FReader.Peek = QUOTE[0] |
| 960 | then begin |
| 961 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( TRUE)); |
| 962 | dub := StrToFloat( str, INVARIANT_CULTURE); |
| 963 | |
| 964 | if not FContext.EscapeNumbers() |
| 965 | and not Math.IsNaN(dub) |
| 966 | and not Math.IsInfinite(dub) |
| 967 | then begin |
| 968 | // Throw exception -- we should not be in a string in Self case |
| 969 | raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Numeric data unexpectedly quoted'); |
| 970 | end; |
| 971 | result := dub; |
| 972 | Exit; |
| 973 | end; |
| 974 | |
| 975 | // will throw - we should have had a quote if escapeNum == true |
| 976 | if FContext.EscapeNumbers |
| 977 | then ReadJSONSyntaxChar( QUOTE[0]); |
| 978 | |
| 979 | try |
| 980 | str := ReadJSONNumericChars; |
| 981 | result := StrToFloat( str, INVARIANT_CULTURE); |
| 982 | except |
| 983 | on e:Exception |
| 984 | do raise TProtocolException.Create( TProtocolException.INVALID_DATA, |
| 985 | 'Bad data encounted in numeric data ('+str+') ('+e.Message+')'); |
| 986 | end; |
| 987 | end; |
| 988 | |
| 989 | |
| 990 | function TJSONProtocolImpl.ReadJSONBase64 : TBytes; |
| 991 | var b : TBytes; |
Jens Geyer | 9f9535c | 2014-12-14 04:16:05 +0100 | [diff] [blame] | 992 | len, off, size : Integer; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 993 | begin |
| 994 | b := ReadJSONString(false); |
| 995 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 996 | len := Length(b); |
| 997 | off := 0; |
| 998 | size := 0; |
| 999 | |
| 1000 | // reduce len to ignore fill bytes |
| 1001 | Dec(len); |
| 1002 | while (len >= 0) and (b[len] = Byte('=')) do Dec(len); |
| 1003 | Inc(len); |
| 1004 | |
| 1005 | // read & decode full byte triplets = 4 source bytes |
| 1006 | while (len >= 4) do begin |
| 1007 | // Decode 4 bytes at a time |
| 1008 | Inc( size, Base64Utils.Decode( b, off, 4, b, size)); // decoded in place |
| 1009 | Inc( off, 4); |
| 1010 | Dec( len, 4); |
| 1011 | end; |
| 1012 | |
| 1013 | // Don't decode if we hit the end or got a single leftover byte (invalid |
| 1014 | // base64 but legal for skip of regular string type) |
| 1015 | if len > 1 then begin |
| 1016 | // Decode remainder |
| 1017 | Inc( size, Base64Utils.Decode( b, off, len, b, size)); // decoded in place |
| 1018 | end; |
| 1019 | |
| 1020 | // resize to final size and return the data |
| 1021 | SetLength( b, size); |
| 1022 | result := b; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 1023 | end; |
| 1024 | |
| 1025 | |
| 1026 | procedure TJSONProtocolImpl.ReadJSONObjectStart; |
| 1027 | begin |
| 1028 | FContext.Read; |
| 1029 | ReadJSONSyntaxChar( LBRACE[0]); |
| 1030 | PushContext( TJSONPairContext.Create( Self)); |
| 1031 | end; |
| 1032 | |
| 1033 | |
| 1034 | procedure TJSONProtocolImpl.ReadJSONObjectEnd; |
| 1035 | begin |
| 1036 | ReadJSONSyntaxChar( RBRACE[0]); |
| 1037 | PopContext; |
| 1038 | end; |
| 1039 | |
| 1040 | |
| 1041 | procedure TJSONProtocolImpl.ReadJSONArrayStart; |
| 1042 | begin |
| 1043 | FContext.Read; |
| 1044 | ReadJSONSyntaxChar( LBRACKET[0]); |
| 1045 | PushContext( TJSONListContext.Create( Self)); |
| 1046 | end; |
| 1047 | |
| 1048 | |
| 1049 | procedure TJSONProtocolImpl.ReadJSONArrayEnd; |
| 1050 | begin |
| 1051 | ReadJSONSyntaxChar( RBRACKET[0]); |
| 1052 | PopContext; |
| 1053 | end; |
| 1054 | |
| 1055 | |
| 1056 | function TJSONProtocolImpl.ReadMessageBegin: IMessage; |
| 1057 | begin |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame] | 1058 | ResetContextStack; // THRIFT-1473 |
| 1059 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 1060 | result := TMessageImpl.Create; |
| 1061 | ReadJSONArrayStart; |
| 1062 | |
| 1063 | if ReadJSONInteger <> VERSION |
| 1064 | then raise TProtocolException.Create( TProtocolException.BAD_VERSION, 'Message contained bad version.'); |
| 1065 | |
| 1066 | result.Name := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( FALSE)); |
| 1067 | result.Type_ := TMessageType( ReadJSONInteger); |
| 1068 | result.SeqID := ReadJSONInteger; |
| 1069 | end; |
| 1070 | |
| 1071 | |
| 1072 | procedure TJSONProtocolImpl.ReadMessageEnd; |
| 1073 | begin |
| 1074 | ReadJSONArrayEnd; |
| 1075 | end; |
| 1076 | |
| 1077 | |
| 1078 | function TJSONProtocolImpl.ReadStructBegin : IStruct ; |
| 1079 | begin |
| 1080 | ReadJSONObjectStart; |
| 1081 | result := TStructImpl.Create(''); |
| 1082 | end; |
| 1083 | |
| 1084 | |
| 1085 | procedure TJSONProtocolImpl.ReadStructEnd; |
| 1086 | begin |
| 1087 | ReadJSONObjectEnd; |
| 1088 | end; |
| 1089 | |
| 1090 | |
| 1091 | function TJSONProtocolImpl.ReadFieldBegin : IField; |
| 1092 | var ch : Byte; |
| 1093 | str : string; |
| 1094 | begin |
| 1095 | result := TFieldImpl.Create; |
| 1096 | ch := FReader.Peek; |
| 1097 | if ch = RBRACE[0] |
| 1098 | then result.Type_ := TType.Stop |
| 1099 | else begin |
| 1100 | result.ID := ReadJSONInteger; |
| 1101 | ReadJSONObjectStart; |
| 1102 | |
| 1103 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( FALSE)); |
| 1104 | result.Type_ := GetTypeIDForTypeName( str); |
| 1105 | end; |
| 1106 | end; |
| 1107 | |
| 1108 | |
| 1109 | procedure TJSONProtocolImpl.ReadFieldEnd; |
| 1110 | begin |
| 1111 | ReadJSONObjectEnd; |
| 1112 | end; |
| 1113 | |
| 1114 | |
| 1115 | function TJSONProtocolImpl.ReadMapBegin : IMap; |
| 1116 | var str : string; |
| 1117 | begin |
| 1118 | result := TMapImpl.Create; |
| 1119 | ReadJSONArrayStart; |
| 1120 | |
| 1121 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1122 | result.KeyType := GetTypeIDForTypeName( str); |
| 1123 | |
| 1124 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1125 | result.ValueType := GetTypeIDForTypeName( str); |
| 1126 | |
| 1127 | result.Count := ReadJSONInteger; |
| 1128 | ReadJSONObjectStart; |
| 1129 | end; |
| 1130 | |
| 1131 | |
| 1132 | procedure TJSONProtocolImpl.ReadMapEnd; |
| 1133 | begin |
| 1134 | ReadJSONObjectEnd; |
| 1135 | ReadJSONArrayEnd; |
| 1136 | end; |
| 1137 | |
| 1138 | |
| 1139 | function TJSONProtocolImpl.ReadListBegin : IList; |
| 1140 | var str : string; |
| 1141 | begin |
| 1142 | result := TListImpl.Create; |
| 1143 | ReadJSONArrayStart; |
| 1144 | |
| 1145 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1146 | result.ElementType := GetTypeIDForTypeName( str); |
| 1147 | result.Count := ReadJSONInteger; |
| 1148 | end; |
| 1149 | |
| 1150 | |
| 1151 | procedure TJSONProtocolImpl.ReadListEnd; |
| 1152 | begin |
| 1153 | ReadJSONArrayEnd; |
| 1154 | end; |
| 1155 | |
| 1156 | |
| 1157 | function TJSONProtocolImpl.ReadSetBegin : ISet; |
| 1158 | var str : string; |
| 1159 | begin |
| 1160 | result := TSetImpl.Create; |
| 1161 | ReadJSONArrayStart; |
| 1162 | |
| 1163 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1164 | result.ElementType := GetTypeIDForTypeName( str); |
| 1165 | result.Count := ReadJSONInteger; |
| 1166 | end; |
| 1167 | |
| 1168 | |
| 1169 | procedure TJSONProtocolImpl.ReadSetEnd; |
| 1170 | begin |
| 1171 | ReadJSONArrayEnd; |
| 1172 | end; |
| 1173 | |
| 1174 | |
| 1175 | function TJSONProtocolImpl.ReadBool : Boolean; |
| 1176 | begin |
| 1177 | result := (ReadJSONInteger <> 0); |
| 1178 | end; |
| 1179 | |
| 1180 | |
| 1181 | function TJSONProtocolImpl.ReadByte : ShortInt; |
| 1182 | begin |
| 1183 | result := ReadJSONInteger; |
| 1184 | end; |
| 1185 | |
| 1186 | |
| 1187 | function TJSONProtocolImpl.ReadI16 : SmallInt; |
| 1188 | begin |
| 1189 | result := ReadJSONInteger; |
| 1190 | end; |
| 1191 | |
| 1192 | |
| 1193 | function TJSONProtocolImpl.ReadI32 : LongInt; |
| 1194 | begin |
| 1195 | result := ReadJSONInteger; |
| 1196 | end; |
| 1197 | |
| 1198 | |
| 1199 | function TJSONProtocolImpl.ReadI64 : Int64; |
| 1200 | begin |
| 1201 | result := ReadJSONInteger; |
| 1202 | end; |
| 1203 | |
| 1204 | |
| 1205 | function TJSONProtocolImpl.ReadDouble : Double; |
| 1206 | begin |
| 1207 | result := ReadJSONDouble; |
| 1208 | end; |
| 1209 | |
| 1210 | |
| 1211 | function TJSONProtocolImpl.ReadString : string; |
| 1212 | begin |
| 1213 | result := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( FALSE)); |
| 1214 | end; |
| 1215 | |
| 1216 | |
| 1217 | function TJSONProtocolImpl.ReadBinary : TBytes; |
| 1218 | begin |
| 1219 | result := ReadJSONBase64; |
| 1220 | end; |
| 1221 | |
| 1222 | |
| 1223 | //--- init code --- |
| 1224 | |
| 1225 | procedure InitBytes( var b : TBytes; aData : array of Byte); |
| 1226 | begin |
| 1227 | SetLength( b, Length(aData)); |
| 1228 | Move( aData, b[0], Length(b)); |
| 1229 | end; |
| 1230 | |
| 1231 | initialization |
| 1232 | InitBytes( COMMA, [Byte(',')]); |
| 1233 | InitBytes( COLON, [Byte(':')]); |
| 1234 | InitBytes( LBRACE, [Byte('{')]); |
| 1235 | InitBytes( RBRACE, [Byte('}')]); |
| 1236 | InitBytes( LBRACKET, [Byte('[')]); |
| 1237 | InitBytes( RBRACKET, [Byte(']')]); |
| 1238 | InitBytes( QUOTE, [Byte('"')]); |
| 1239 | InitBytes( BACKSLASH, [Byte('\')]); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 1240 | InitBytes( ESCSEQ, [Byte('\'),Byte('u'),Byte('0'),Byte('0')]); |
| 1241 | end. |