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 |
| 27 | Classes, |
| 28 | SysUtils, |
| 29 | Math, |
| 30 | IdCoderMIME, |
| 31 | Generics.Collections, |
| 32 | Thrift.Transport, |
| 33 | Thrift.Protocol; |
| 34 | |
| 35 | type |
| 36 | IJSONProtocol = interface( IProtocol) |
| 37 | ['{F0DAFDBD-692A-4B71-9736-F5D485A2178F}'] |
| 38 | // Read a byte that must match b; otherwise an exception is thrown. |
| 39 | procedure ReadJSONSyntaxChar( b : Byte); |
| 40 | end; |
| 41 | |
| 42 | // JSON protocol implementation for thrift. |
| 43 | // This is a full-featured protocol supporting Write and Read. |
| 44 | // Please see the C++ class header for a detailed description of the protocol's wire format. |
| 45 | // Adapted from the C# version. |
| 46 | TJSONProtocolImpl = class( TProtocolImpl, IJSONProtocol) |
| 47 | public |
| 48 | type |
| 49 | TFactory = class( TInterfacedObject, IProtocolFactory) |
| 50 | public |
| 51 | function GetProtocol( trans: ITransport): IProtocol; |
| 52 | end; |
| 53 | |
| 54 | private |
| 55 | class function GetTypeNameForTypeID(typeID : TType) : string; |
| 56 | class function GetTypeIDForTypeName( const name : string) : TType; |
| 57 | |
| 58 | protected |
| 59 | type |
| 60 | // Base class for tracking JSON contexts that may require |
| 61 | // inserting/Reading additional JSON syntax characters. |
| 62 | // This base context does nothing. |
| 63 | TJSONBaseContext = class |
| 64 | protected |
| 65 | FProto : IJSONProtocol; |
| 66 | public |
| 67 | constructor Create( const aProto : IJSONProtocol); |
| 68 | procedure Write; virtual; |
| 69 | procedure Read; virtual; |
| 70 | function EscapeNumbers : Boolean; virtual; |
| 71 | end; |
| 72 | |
| 73 | // Context for JSON lists. |
| 74 | // Will insert/Read commas before each item except for the first one. |
| 75 | TJSONListContext = class( TJSONBaseContext) |
| 76 | private |
| 77 | FFirst : Boolean; |
| 78 | public |
| 79 | constructor Create( const aProto : IJSONProtocol); |
| 80 | procedure Write; override; |
| 81 | procedure Read; override; |
| 82 | end; |
| 83 | |
| 84 | // Context for JSON records. Will insert/Read colons before the value portion of each record |
| 85 | // pair, and commas before each key except the first. In addition, will indicate that numbers |
| 86 | // in the key position need to be escaped in quotes (since JSON keys must be strings). |
| 87 | TJSONPairContext = class( TJSONBaseContext) |
| 88 | private |
| 89 | FFirst, FColon : Boolean; |
| 90 | public |
| 91 | constructor Create( const aProto : IJSONProtocol); |
| 92 | procedure Write; override; |
| 93 | procedure Read; override; |
| 94 | function EscapeNumbers : Boolean; override; |
| 95 | end; |
| 96 | |
| 97 | // Holds up to one byte from the transport |
| 98 | TLookaheadReader = class |
| 99 | protected |
| 100 | FProto : IJSONProtocol; |
| 101 | constructor Create( const aProto : IJSONProtocol); |
| 102 | |
| 103 | private |
| 104 | FHasData : Boolean; |
| 105 | FData : TBytes; |
| 106 | |
| 107 | public |
| 108 | // Return and consume the next byte to be Read, either taking it from the |
| 109 | // data buffer if present or getting it from the transport otherwise. |
| 110 | function Read : Byte; |
| 111 | |
| 112 | // Return the next byte to be Read without consuming, filling the data |
| 113 | // buffer if it has not been filled alReady. |
| 114 | function Peek : Byte; |
| 115 | end; |
| 116 | |
| 117 | protected |
| 118 | // Stack of nested contexts that we may be in |
| 119 | FContextStack : TStack<TJSONBaseContext>; |
| 120 | |
| 121 | // Current context that we are in |
| 122 | FContext : TJSONBaseContext; |
| 123 | |
| 124 | // Reader that manages a 1-byte buffer |
| 125 | FReader : TLookaheadReader; |
| 126 | |
| 127 | // Push/pop a new JSON context onto/from the stack. |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame^] | 128 | procedure ResetContextStack; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 129 | procedure PushContext( aCtx : TJSONBaseContext); |
| 130 | procedure PopContext; |
| 131 | |
| 132 | public |
| 133 | // TJSONProtocolImpl Constructor |
| 134 | constructor Create( aTrans : ITransport); |
| 135 | destructor Destroy; override; |
| 136 | |
| 137 | protected |
| 138 | // IJSONProtocol |
| 139 | // Read a byte that must match b; otherwise an exception is thrown. |
| 140 | procedure ReadJSONSyntaxChar( b : Byte); |
| 141 | |
| 142 | private |
| 143 | // Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its corresponding hex value |
| 144 | class function HexVal( ch : Byte) : Byte; |
| 145 | |
| 146 | // Convert a byte containing a hex value to its corresponding hex character |
| 147 | class function HexChar( val : Byte) : Byte; |
| 148 | |
| 149 | // Write the bytes in array buf as a JSON characters, escaping as needed |
| 150 | procedure WriteJSONString( const b : TBytes); overload; |
| 151 | procedure WriteJSONString( str : string); overload; |
| 152 | |
| 153 | // Write out number as a JSON value. If the context dictates so, it will be |
| 154 | // wrapped in quotes to output as a JSON string. |
| 155 | procedure WriteJSONInteger( num : Int64); |
| 156 | |
| 157 | // Write out a double as a JSON value. If it is NaN or infinity or if the |
| 158 | // context dictates escaping, Write out as JSON string. |
| 159 | procedure WriteJSONDouble( const num : Double); |
| 160 | |
| 161 | // Write out contents of byte array b as a JSON string with base-64 encoded data |
| 162 | procedure WriteJSONBase64( const b : TBytes); |
| 163 | |
| 164 | procedure WriteJSONObjectStart; |
| 165 | procedure WriteJSONObjectEnd; |
| 166 | procedure WriteJSONArrayStart; |
| 167 | procedure WriteJSONArrayEnd; |
| 168 | |
| 169 | public |
| 170 | // IProtocol |
| 171 | procedure WriteMessageBegin( aMsg : IMessage); override; |
| 172 | procedure WriteMessageEnd; override; |
| 173 | procedure WriteStructBegin(struc: IStruct); override; |
| 174 | procedure WriteStructEnd; override; |
| 175 | procedure WriteFieldBegin(field: IField); override; |
| 176 | procedure WriteFieldEnd; override; |
| 177 | procedure WriteFieldStop; override; |
| 178 | procedure WriteMapBegin(map: IMap); override; |
| 179 | procedure WriteMapEnd; override; |
| 180 | procedure WriteListBegin( list: IList); override; |
| 181 | procedure WriteListEnd(); override; |
| 182 | procedure WriteSetBegin( set_: ISet ); override; |
| 183 | procedure WriteSetEnd(); override; |
| 184 | procedure WriteBool( b: Boolean); override; |
| 185 | procedure WriteByte( b: ShortInt); override; |
| 186 | procedure WriteI16( i16: SmallInt); override; |
| 187 | procedure WriteI32( i32: Integer); override; |
| 188 | procedure WriteI64( i64: Int64); override; |
| 189 | procedure WriteDouble( d: Double); override; |
| 190 | procedure WriteString( const s: string ); override; |
| 191 | procedure WriteBinary( const b: TBytes); override; |
| 192 | // |
| 193 | function ReadMessageBegin: IMessage; override; |
| 194 | procedure ReadMessageEnd(); override; |
| 195 | function ReadStructBegin: IStruct; override; |
| 196 | procedure ReadStructEnd; override; |
| 197 | function ReadFieldBegin: IField; override; |
| 198 | procedure ReadFieldEnd(); override; |
| 199 | function ReadMapBegin: IMap; override; |
| 200 | procedure ReadMapEnd(); override; |
| 201 | function ReadListBegin: IList; override; |
| 202 | procedure ReadListEnd(); override; |
| 203 | function ReadSetBegin: ISet; override; |
| 204 | procedure ReadSetEnd(); override; |
| 205 | function ReadBool: Boolean; override; |
| 206 | function ReadByte: ShortInt; override; |
| 207 | function ReadI16: SmallInt; override; |
| 208 | function ReadI32: Integer; override; |
| 209 | function ReadI64: Int64; override; |
| 210 | function ReadDouble:Double; override; |
| 211 | function ReadString : string; override; |
| 212 | function ReadBinary: TBytes; override; |
| 213 | |
| 214 | |
| 215 | private |
| 216 | // Reading methods. |
| 217 | |
| 218 | // Read in a JSON string, unescaping as appropriate. |
| 219 | // Skip Reading from the context if skipContext is true. |
| 220 | function ReadJSONString( skipContext : Boolean) : TBytes; |
| 221 | |
| 222 | // Return true if the given byte could be a valid part of a JSON number. |
| 223 | function IsJSONNumeric( b : Byte) : Boolean; |
| 224 | |
| 225 | // Read in a sequence of characters that are all valid in JSON numbers. Does |
| 226 | // not do a complete regex check to validate that this is actually a number. |
| 227 | function ReadJSONNumericChars : String; |
| 228 | |
| 229 | // Read in a JSON number. If the context dictates, Read in enclosing quotes. |
| 230 | function ReadJSONInteger : Int64; |
| 231 | |
| 232 | // Read in a JSON double value. Throw if the value is not wrapped in quotes |
| 233 | // when expected or if wrapped in quotes when not expected. |
| 234 | function ReadJSONDouble : Double; |
| 235 | |
| 236 | // Read in a JSON string containing base-64 encoded data and decode it. |
| 237 | function ReadJSONBase64 : TBytes; |
| 238 | |
| 239 | procedure ReadJSONObjectStart; |
| 240 | procedure ReadJSONObjectEnd; |
| 241 | procedure ReadJSONArrayStart; |
| 242 | procedure ReadJSONArrayEnd; |
| 243 | end; |
| 244 | |
| 245 | |
| 246 | implementation |
| 247 | |
| 248 | var |
| 249 | COMMA : TBytes; |
| 250 | COLON : TBytes; |
| 251 | LBRACE : TBytes; |
| 252 | RBRACE : TBytes; |
| 253 | LBRACKET : TBytes; |
| 254 | RBRACKET : TBytes; |
| 255 | QUOTE : TBytes; |
| 256 | BACKSLASH : TBytes; |
| 257 | ZERO : TBytes; |
| 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 | |
| 267 | ESCAPE_CHARS = '"\btnfr'; |
| 268 | ESCAPE_CHAR_VALS = '"\'#8#9#10#12#13; |
| 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 | |
| 293 | function TJSONProtocolImpl.TFactory.GetProtocol( trans: ITransport): IProtocol; |
| 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; |
| 338 | FProto := aProto; |
| 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 |
| 371 | else FProto.Transport.Write( COMMA); |
| 372 | end; |
| 373 | |
| 374 | |
| 375 | procedure TJSONProtocolImpl.TJSONListContext.Read; |
| 376 | begin |
| 377 | if FFirst |
| 378 | then FFirst := FALSE |
| 379 | else FProto.ReadJSONSyntaxChar( COMMA[0]); |
| 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 |
| 399 | then FProto.Transport.Write( COLON) |
| 400 | else FProto.Transport.Write( COMMA); |
| 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 |
| 414 | then FProto.ReadJSONSyntaxChar( COLON[0]) |
| 415 | else FProto.ReadJSONSyntaxChar( COMMA[0]); |
| 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; |
| 430 | FProto := aProto; |
| 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); |
| 441 | FProto.Transport.ReadAll( FData, 0, 1); |
| 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); |
| 451 | FProto.Transport.ReadAll( FData, 0, 1); |
| 452 | FHasData := TRUE; |
| 453 | end; |
| 454 | result := FData[0]; |
| 455 | end; |
| 456 | |
| 457 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 458 | constructor TJSONProtocolImpl.Create( aTrans : ITransport); |
| 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 | |
| 490 | procedure TJSONProtocolImpl.PushContext( aCtx : TJSONBaseContext); |
| 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 | |
| 531 | procedure TJSONProtocolImpl.WriteJSONString( str : string); |
| 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 | |
| 578 | procedure TJSONProtocolImpl.WriteJSONInteger( num : Int64); |
| 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); |
| 626 | var str : string; |
| 627 | tmp : TBytes; |
| 628 | i : Integer; |
| 629 | begin |
| 630 | FContext.Write; |
| 631 | Transport.Write( QUOTE); |
| 632 | |
| 633 | // First base64-encode b, then write the resulting 8-bit chars |
| 634 | // Unfortunately, EncodeBytes() returns a string of 16-bit (wide) chars |
| 635 | // And for the sake of efficiency, we want to write everything at once |
| 636 | str := TIdEncoderMIME.EncodeBytes(b); |
| 637 | ASSERT( SizeOf(str[1]) = SizeOf(Word)); |
| 638 | SetLength( tmp, Length(str)); |
| 639 | for i := 1 to Length(str) do begin |
| 640 | ASSERT( Hi(Word(str[i])) = 0); // base64 consists of a well-defined set of 8-bit chars only |
| 641 | tmp[i-1] := Lo(Word(str[i])); // extract the lower byte |
| 642 | end; |
| 643 | Transport.Write( tmp); // now write all the data |
| 644 | |
| 645 | Transport.Write( QUOTE); |
| 646 | end; |
| 647 | |
| 648 | |
| 649 | procedure TJSONProtocolImpl.WriteJSONObjectStart; |
| 650 | begin |
| 651 | FContext.Write; |
| 652 | Transport.Write( LBRACE); |
| 653 | PushContext( TJSONPairContext.Create( Self)); |
| 654 | end; |
| 655 | |
| 656 | |
| 657 | procedure TJSONProtocolImpl.WriteJSONObjectEnd; |
| 658 | begin |
| 659 | PopContext; |
| 660 | Transport.Write( RBRACE); |
| 661 | end; |
| 662 | |
| 663 | |
| 664 | procedure TJSONProtocolImpl.WriteJSONArrayStart; |
| 665 | begin |
| 666 | FContext.Write; |
| 667 | Transport.Write( LBRACKET); |
| 668 | PushContext( TJSONListContext.Create( Self)); |
| 669 | end; |
| 670 | |
| 671 | |
| 672 | procedure TJSONProtocolImpl.WriteJSONArrayEnd; |
| 673 | begin |
| 674 | PopContext; |
| 675 | Transport.Write( RBRACKET); |
| 676 | end; |
| 677 | |
| 678 | |
| 679 | procedure TJSONProtocolImpl.WriteMessageBegin( aMsg : IMessage); |
| 680 | begin |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame^] | 681 | ResetContextStack; // THRIFT-1473 |
| 682 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 683 | WriteJSONArrayStart; |
| 684 | WriteJSONInteger(VERSION); |
| 685 | |
| 686 | WriteJSONString( SysUtils.TEncoding.UTF8.GetBytes( aMsg.Name)); |
| 687 | |
| 688 | WriteJSONInteger( LongInt( aMsg.Type_)); |
| 689 | WriteJSONInteger( aMsg.SeqID); |
| 690 | end; |
| 691 | |
| 692 | procedure TJSONProtocolImpl.WriteMessageEnd; |
| 693 | begin |
| 694 | WriteJSONArrayEnd; |
| 695 | end; |
| 696 | |
| 697 | |
| 698 | procedure TJSONProtocolImpl.WriteStructBegin( struc: IStruct); |
| 699 | begin |
| 700 | WriteJSONObjectStart; |
| 701 | end; |
| 702 | |
| 703 | |
| 704 | procedure TJSONProtocolImpl.WriteStructEnd; |
| 705 | begin |
| 706 | WriteJSONObjectEnd; |
| 707 | end; |
| 708 | |
| 709 | |
| 710 | procedure TJSONProtocolImpl.WriteFieldBegin( field : IField); |
| 711 | begin |
| 712 | WriteJSONInteger(field.ID); |
| 713 | WriteJSONObjectStart; |
| 714 | WriteJSONString( GetTypeNameForTypeID(field.Type_)); |
| 715 | end; |
| 716 | |
| 717 | |
| 718 | procedure TJSONProtocolImpl.WriteFieldEnd; |
| 719 | begin |
| 720 | WriteJSONObjectEnd; |
| 721 | end; |
| 722 | |
| 723 | |
| 724 | procedure TJSONProtocolImpl.WriteFieldStop; |
| 725 | begin |
| 726 | // nothing to do |
| 727 | end; |
| 728 | |
| 729 | procedure TJSONProtocolImpl.WriteMapBegin( map: IMap); |
| 730 | begin |
| 731 | WriteJSONArrayStart; |
| 732 | WriteJSONString( GetTypeNameForTypeID( map.KeyType)); |
| 733 | WriteJSONString( GetTypeNameForTypeID( map.ValueType)); |
| 734 | WriteJSONInteger( map.Count); |
| 735 | WriteJSONObjectStart; |
| 736 | end; |
| 737 | |
| 738 | |
| 739 | procedure TJSONProtocolImpl.WriteMapEnd; |
| 740 | begin |
| 741 | WriteJSONObjectEnd; |
| 742 | WriteJSONArrayEnd; |
| 743 | end; |
| 744 | |
| 745 | |
| 746 | procedure TJSONProtocolImpl.WriteListBegin( list: IList); |
| 747 | begin |
| 748 | WriteJSONArrayStart; |
| 749 | WriteJSONString( GetTypeNameForTypeID( list.ElementType)); |
| 750 | WriteJSONInteger(list.Count); |
| 751 | end; |
| 752 | |
| 753 | |
| 754 | procedure TJSONProtocolImpl.WriteListEnd; |
| 755 | begin |
| 756 | WriteJSONArrayEnd; |
| 757 | end; |
| 758 | |
| 759 | |
| 760 | procedure TJSONProtocolImpl.WriteSetBegin( set_: ISet); |
| 761 | begin |
| 762 | WriteJSONArrayStart; |
| 763 | WriteJSONString( GetTypeNameForTypeID( set_.ElementType)); |
| 764 | WriteJSONInteger( set_.Count); |
| 765 | end; |
| 766 | |
| 767 | |
| 768 | procedure TJSONProtocolImpl.WriteSetEnd; |
| 769 | begin |
| 770 | WriteJSONArrayEnd; |
| 771 | end; |
| 772 | |
| 773 | procedure TJSONProtocolImpl.WriteBool( b: Boolean); |
| 774 | begin |
| 775 | if b |
| 776 | then WriteJSONInteger( 1) |
| 777 | else WriteJSONInteger( 0); |
| 778 | end; |
| 779 | |
| 780 | procedure TJSONProtocolImpl.WriteByte( b: ShortInt); |
| 781 | begin |
| 782 | WriteJSONInteger( b); |
| 783 | end; |
| 784 | |
| 785 | procedure TJSONProtocolImpl.WriteI16( i16: SmallInt); |
| 786 | begin |
| 787 | WriteJSONInteger( i16); |
| 788 | end; |
| 789 | |
| 790 | procedure TJSONProtocolImpl.WriteI32( i32: Integer); |
| 791 | begin |
| 792 | WriteJSONInteger( i32); |
| 793 | end; |
| 794 | |
| 795 | procedure TJSONProtocolImpl.WriteI64( i64: Int64); |
| 796 | begin |
| 797 | WriteJSONInteger(i64); |
| 798 | end; |
| 799 | |
| 800 | procedure TJSONProtocolImpl.WriteDouble( d: Double); |
| 801 | begin |
| 802 | WriteJSONDouble( d); |
| 803 | end; |
| 804 | |
| 805 | procedure TJSONProtocolImpl.WriteString( const s: string ); |
| 806 | begin |
| 807 | WriteJSONString( SysUtils.TEncoding.UTF8.GetBytes( s)); |
| 808 | end; |
| 809 | |
| 810 | procedure TJSONProtocolImpl.WriteBinary( const b: TBytes); |
| 811 | begin |
| 812 | WriteJSONBase64( b); |
| 813 | end; |
| 814 | |
| 815 | |
| 816 | function TJSONProtocolImpl.ReadJSONString( skipContext : Boolean) : TBytes; |
| 817 | var buffer : TMemoryStream; |
| 818 | ch : Byte; |
| 819 | off : Integer; |
| 820 | tmp : TBytes; |
| 821 | begin |
| 822 | buffer := TMemoryStream.Create; |
| 823 | try |
| 824 | if not skipContext |
| 825 | then FContext.Read; |
| 826 | |
| 827 | ReadJSONSyntaxChar( QUOTE[0]); |
| 828 | |
| 829 | while TRUE do begin |
| 830 | ch := FReader.Read; |
| 831 | |
| 832 | if (ch = QUOTE[0]) |
| 833 | then Break; |
| 834 | |
| 835 | if (ch = ESCSEQ[0]) |
| 836 | then begin |
| 837 | ch := FReader.Read; |
| 838 | if (ch = ESCSEQ[1]) |
| 839 | then begin |
| 840 | ReadJSONSyntaxChar( ZERO[0]); |
| 841 | ReadJSONSyntaxChar( ZERO[0]); |
| 842 | SetLength( tmp, 2); |
| 843 | Transport.ReadAll( tmp, 0, 2); |
| 844 | ch := (HexVal(tmp[0]) shl 4) + HexVal(tmp[1]); |
| 845 | end |
| 846 | else begin |
| 847 | off := Pos( Char(ch), ESCAPE_CHARS); |
| 848 | if off < 1 |
| 849 | then raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Expected control char'); |
| 850 | ch := Byte( ESCAPE_CHAR_VALS[off]); |
| 851 | end; |
| 852 | end; |
| 853 | buffer.Write( ch, 1); |
| 854 | end; |
| 855 | |
| 856 | SetLength( result, buffer.Size); |
Jake Farrell | a2a9ee9 | 2011-12-15 20:50:31 +0000 | [diff] [blame] | 857 | if buffer.Size > 0 then Move( buffer.Memory^, result[0], Length(result)); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 858 | |
| 859 | finally |
| 860 | buffer.Free; |
| 861 | end; |
| 862 | end; |
| 863 | |
| 864 | |
| 865 | function TJSONProtocolImpl.IsJSONNumeric( b : Byte) : Boolean; |
| 866 | const NUMCHARS = ['+','-','.','0','1','2','3','4','5','6','7','8','9','E','e']; |
| 867 | begin |
| 868 | result := CharInSet( Char(b), NUMCHARS); |
| 869 | end; |
| 870 | |
| 871 | |
| 872 | function TJSONProtocolImpl.ReadJSONNumericChars : string; |
| 873 | var strbld : TThriftStringBuilder; |
| 874 | ch : Byte; |
| 875 | begin |
| 876 | strbld := TThriftStringBuilder.Create; |
| 877 | try |
| 878 | while TRUE do begin |
| 879 | ch := FReader.Peek; |
| 880 | if IsJSONNumeric(ch) |
| 881 | then strbld.Append( Char(FReader.Read)) |
| 882 | else Break; |
| 883 | end; |
| 884 | result := strbld.ToString; |
| 885 | |
| 886 | finally |
| 887 | strbld.Free; |
| 888 | end; |
| 889 | end; |
| 890 | |
| 891 | |
| 892 | function TJSONProtocolImpl.ReadJSONInteger : Int64; |
| 893 | var str : string; |
| 894 | begin |
| 895 | FContext.Read; |
| 896 | if FContext.EscapeNumbers |
| 897 | then ReadJSONSyntaxChar( QUOTE[0]); |
| 898 | |
| 899 | str := ReadJSONNumericChars; |
| 900 | |
| 901 | if FContext.EscapeNumbers |
| 902 | then ReadJSONSyntaxChar( QUOTE[0]); |
| 903 | |
| 904 | try |
| 905 | result := StrToInt64(str); |
| 906 | except |
| 907 | on e:Exception do begin |
| 908 | raise TProtocolException.Create( TProtocolException.INVALID_DATA, |
| 909 | 'Bad data encounted in numeric data ('+str+') ('+e.Message+')'); |
| 910 | end; |
| 911 | end; |
| 912 | end; |
| 913 | |
| 914 | |
| 915 | function TJSONProtocolImpl.ReadJSONDouble : Double; |
| 916 | var dub : Double; |
| 917 | str : string; |
| 918 | begin |
| 919 | FContext.Read; |
| 920 | |
| 921 | if FReader.Peek = QUOTE[0] |
| 922 | then begin |
| 923 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( TRUE)); |
| 924 | dub := StrToFloat( str, INVARIANT_CULTURE); |
| 925 | |
| 926 | if not FContext.EscapeNumbers() |
| 927 | and not Math.IsNaN(dub) |
| 928 | and not Math.IsInfinite(dub) |
| 929 | then begin |
| 930 | // Throw exception -- we should not be in a string in Self case |
| 931 | raise TProtocolException.Create( TProtocolException.INVALID_DATA, 'Numeric data unexpectedly quoted'); |
| 932 | end; |
| 933 | result := dub; |
| 934 | Exit; |
| 935 | end; |
| 936 | |
| 937 | // will throw - we should have had a quote if escapeNum == true |
| 938 | if FContext.EscapeNumbers |
| 939 | then ReadJSONSyntaxChar( QUOTE[0]); |
| 940 | |
| 941 | try |
| 942 | str := ReadJSONNumericChars; |
| 943 | result := StrToFloat( str, INVARIANT_CULTURE); |
| 944 | except |
| 945 | on e:Exception |
| 946 | do raise TProtocolException.Create( TProtocolException.INVALID_DATA, |
| 947 | 'Bad data encounted in numeric data ('+str+') ('+e.Message+')'); |
| 948 | end; |
| 949 | end; |
| 950 | |
| 951 | |
| 952 | function TJSONProtocolImpl.ReadJSONBase64 : TBytes; |
| 953 | var b : TBytes; |
| 954 | str : string; |
| 955 | begin |
| 956 | b := ReadJSONString(false); |
| 957 | |
| 958 | SetString( str, PAnsiChar(b), Length(b)); |
| 959 | result := TIdDecoderMIME.DecodeBytes( str); |
| 960 | end; |
| 961 | |
| 962 | |
| 963 | procedure TJSONProtocolImpl.ReadJSONObjectStart; |
| 964 | begin |
| 965 | FContext.Read; |
| 966 | ReadJSONSyntaxChar( LBRACE[0]); |
| 967 | PushContext( TJSONPairContext.Create( Self)); |
| 968 | end; |
| 969 | |
| 970 | |
| 971 | procedure TJSONProtocolImpl.ReadJSONObjectEnd; |
| 972 | begin |
| 973 | ReadJSONSyntaxChar( RBRACE[0]); |
| 974 | PopContext; |
| 975 | end; |
| 976 | |
| 977 | |
| 978 | procedure TJSONProtocolImpl.ReadJSONArrayStart; |
| 979 | begin |
| 980 | FContext.Read; |
| 981 | ReadJSONSyntaxChar( LBRACKET[0]); |
| 982 | PushContext( TJSONListContext.Create( Self)); |
| 983 | end; |
| 984 | |
| 985 | |
| 986 | procedure TJSONProtocolImpl.ReadJSONArrayEnd; |
| 987 | begin |
| 988 | ReadJSONSyntaxChar( RBRACKET[0]); |
| 989 | PopContext; |
| 990 | end; |
| 991 | |
| 992 | |
| 993 | function TJSONProtocolImpl.ReadMessageBegin: IMessage; |
| 994 | begin |
Roger Meier | 45a3726 | 2012-01-08 21:44:44 +0000 | [diff] [blame^] | 995 | ResetContextStack; // THRIFT-1473 |
| 996 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 997 | result := TMessageImpl.Create; |
| 998 | ReadJSONArrayStart; |
| 999 | |
| 1000 | if ReadJSONInteger <> VERSION |
| 1001 | then raise TProtocolException.Create( TProtocolException.BAD_VERSION, 'Message contained bad version.'); |
| 1002 | |
| 1003 | result.Name := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( FALSE)); |
| 1004 | result.Type_ := TMessageType( ReadJSONInteger); |
| 1005 | result.SeqID := ReadJSONInteger; |
| 1006 | end; |
| 1007 | |
| 1008 | |
| 1009 | procedure TJSONProtocolImpl.ReadMessageEnd; |
| 1010 | begin |
| 1011 | ReadJSONArrayEnd; |
| 1012 | end; |
| 1013 | |
| 1014 | |
| 1015 | function TJSONProtocolImpl.ReadStructBegin : IStruct ; |
| 1016 | begin |
| 1017 | ReadJSONObjectStart; |
| 1018 | result := TStructImpl.Create(''); |
| 1019 | end; |
| 1020 | |
| 1021 | |
| 1022 | procedure TJSONProtocolImpl.ReadStructEnd; |
| 1023 | begin |
| 1024 | ReadJSONObjectEnd; |
| 1025 | end; |
| 1026 | |
| 1027 | |
| 1028 | function TJSONProtocolImpl.ReadFieldBegin : IField; |
| 1029 | var ch : Byte; |
| 1030 | str : string; |
| 1031 | begin |
| 1032 | result := TFieldImpl.Create; |
| 1033 | ch := FReader.Peek; |
| 1034 | if ch = RBRACE[0] |
| 1035 | then result.Type_ := TType.Stop |
| 1036 | else begin |
| 1037 | result.ID := ReadJSONInteger; |
| 1038 | ReadJSONObjectStart; |
| 1039 | |
| 1040 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( FALSE)); |
| 1041 | result.Type_ := GetTypeIDForTypeName( str); |
| 1042 | end; |
| 1043 | end; |
| 1044 | |
| 1045 | |
| 1046 | procedure TJSONProtocolImpl.ReadFieldEnd; |
| 1047 | begin |
| 1048 | ReadJSONObjectEnd; |
| 1049 | end; |
| 1050 | |
| 1051 | |
| 1052 | function TJSONProtocolImpl.ReadMapBegin : IMap; |
| 1053 | var str : string; |
| 1054 | begin |
| 1055 | result := TMapImpl.Create; |
| 1056 | ReadJSONArrayStart; |
| 1057 | |
| 1058 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1059 | result.KeyType := GetTypeIDForTypeName( str); |
| 1060 | |
| 1061 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1062 | result.ValueType := GetTypeIDForTypeName( str); |
| 1063 | |
| 1064 | result.Count := ReadJSONInteger; |
| 1065 | ReadJSONObjectStart; |
| 1066 | end; |
| 1067 | |
| 1068 | |
| 1069 | procedure TJSONProtocolImpl.ReadMapEnd; |
| 1070 | begin |
| 1071 | ReadJSONObjectEnd; |
| 1072 | ReadJSONArrayEnd; |
| 1073 | end; |
| 1074 | |
| 1075 | |
| 1076 | function TJSONProtocolImpl.ReadListBegin : IList; |
| 1077 | var str : string; |
| 1078 | begin |
| 1079 | result := TListImpl.Create; |
| 1080 | ReadJSONArrayStart; |
| 1081 | |
| 1082 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1083 | result.ElementType := GetTypeIDForTypeName( str); |
| 1084 | result.Count := ReadJSONInteger; |
| 1085 | end; |
| 1086 | |
| 1087 | |
| 1088 | procedure TJSONProtocolImpl.ReadListEnd; |
| 1089 | begin |
| 1090 | ReadJSONArrayEnd; |
| 1091 | end; |
| 1092 | |
| 1093 | |
| 1094 | function TJSONProtocolImpl.ReadSetBegin : ISet; |
| 1095 | var str : string; |
| 1096 | begin |
| 1097 | result := TSetImpl.Create; |
| 1098 | ReadJSONArrayStart; |
| 1099 | |
| 1100 | str := SysUtils.TEncoding.UTF8.GetString( ReadJSONString(FALSE)); |
| 1101 | result.ElementType := GetTypeIDForTypeName( str); |
| 1102 | result.Count := ReadJSONInteger; |
| 1103 | end; |
| 1104 | |
| 1105 | |
| 1106 | procedure TJSONProtocolImpl.ReadSetEnd; |
| 1107 | begin |
| 1108 | ReadJSONArrayEnd; |
| 1109 | end; |
| 1110 | |
| 1111 | |
| 1112 | function TJSONProtocolImpl.ReadBool : Boolean; |
| 1113 | begin |
| 1114 | result := (ReadJSONInteger <> 0); |
| 1115 | end; |
| 1116 | |
| 1117 | |
| 1118 | function TJSONProtocolImpl.ReadByte : ShortInt; |
| 1119 | begin |
| 1120 | result := ReadJSONInteger; |
| 1121 | end; |
| 1122 | |
| 1123 | |
| 1124 | function TJSONProtocolImpl.ReadI16 : SmallInt; |
| 1125 | begin |
| 1126 | result := ReadJSONInteger; |
| 1127 | end; |
| 1128 | |
| 1129 | |
| 1130 | function TJSONProtocolImpl.ReadI32 : LongInt; |
| 1131 | begin |
| 1132 | result := ReadJSONInteger; |
| 1133 | end; |
| 1134 | |
| 1135 | |
| 1136 | function TJSONProtocolImpl.ReadI64 : Int64; |
| 1137 | begin |
| 1138 | result := ReadJSONInteger; |
| 1139 | end; |
| 1140 | |
| 1141 | |
| 1142 | function TJSONProtocolImpl.ReadDouble : Double; |
| 1143 | begin |
| 1144 | result := ReadJSONDouble; |
| 1145 | end; |
| 1146 | |
| 1147 | |
| 1148 | function TJSONProtocolImpl.ReadString : string; |
| 1149 | begin |
| 1150 | result := SysUtils.TEncoding.UTF8.GetString( ReadJSONString( FALSE)); |
| 1151 | end; |
| 1152 | |
| 1153 | |
| 1154 | function TJSONProtocolImpl.ReadBinary : TBytes; |
| 1155 | begin |
| 1156 | result := ReadJSONBase64; |
| 1157 | end; |
| 1158 | |
| 1159 | |
| 1160 | //--- init code --- |
| 1161 | |
| 1162 | procedure InitBytes( var b : TBytes; aData : array of Byte); |
| 1163 | begin |
| 1164 | SetLength( b, Length(aData)); |
| 1165 | Move( aData, b[0], Length(b)); |
| 1166 | end; |
| 1167 | |
| 1168 | initialization |
| 1169 | InitBytes( COMMA, [Byte(',')]); |
| 1170 | InitBytes( COLON, [Byte(':')]); |
| 1171 | InitBytes( LBRACE, [Byte('{')]); |
| 1172 | InitBytes( RBRACE, [Byte('}')]); |
| 1173 | InitBytes( LBRACKET, [Byte('[')]); |
| 1174 | InitBytes( RBRACKET, [Byte(']')]); |
| 1175 | InitBytes( QUOTE, [Byte('"')]); |
| 1176 | InitBytes( BACKSLASH, [Byte('\')]); |
| 1177 | InitBytes( ZERO, [Byte('0')]); |
| 1178 | InitBytes( ESCSEQ, [Byte('\'),Byte('u'),Byte('0'),Byte('0')]); |
| 1179 | end. |