David Reiss | ea2cba8 | 2009-03-30 21:35:00 +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 | */ |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 19 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 20 | #include <thrift/protocol/TJSONProtocol.h> |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 21 | |
Jim King | 1654fe9 | 2016-06-19 19:46:01 -0400 | [diff] [blame^] | 22 | #include <boost/lexical_cast.hpp> |
| 23 | #include <boost/locale.hpp> |
| 24 | #include <boost/math/special_functions/fpclassify.hpp> |
| 25 | |
| 26 | #include <cmath> |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 27 | #include <limits> |
| 28 | #include <locale> |
| 29 | #include <sstream> |
Jim King | 1654fe9 | 2016-06-19 19:46:01 -0400 | [diff] [blame^] | 30 | #include <stdexcept> |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 31 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 32 | #include <thrift/protocol/TBase64Utils.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 33 | #include <thrift/transport/TTransportException.h> |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 34 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 35 | using namespace apache::thrift::transport; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 36 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 37 | namespace apache { |
| 38 | namespace thrift { |
| 39 | namespace protocol { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 40 | |
| 41 | // Static data |
| 42 | |
| 43 | static const uint8_t kJSONObjectStart = '{'; |
| 44 | static const uint8_t kJSONObjectEnd = '}'; |
| 45 | static const uint8_t kJSONArrayStart = '['; |
| 46 | static const uint8_t kJSONArrayEnd = ']'; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 47 | static const uint8_t kJSONPairSeparator = ':'; |
| 48 | static const uint8_t kJSONElemSeparator = ','; |
| 49 | static const uint8_t kJSONBackslash = '\\'; |
| 50 | static const uint8_t kJSONStringDelimiter = '"'; |
| 51 | static const uint8_t kJSONZeroChar = '0'; |
| 52 | static const uint8_t kJSONEscapeChar = 'u'; |
| 53 | |
| 54 | static const std::string kJSONEscapePrefix("\\u00"); |
| 55 | |
David Reiss | 6713e1b | 2009-01-15 23:56:19 +0000 | [diff] [blame] | 56 | static const uint32_t kThriftVersion1 = 1; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 57 | |
| 58 | static const std::string kThriftNan("NaN"); |
| 59 | static const std::string kThriftInfinity("Infinity"); |
| 60 | static const std::string kThriftNegativeInfinity("-Infinity"); |
| 61 | |
| 62 | static const std::string kTypeNameBool("tf"); |
| 63 | static const std::string kTypeNameByte("i8"); |
| 64 | static const std::string kTypeNameI16("i16"); |
| 65 | static const std::string kTypeNameI32("i32"); |
| 66 | static const std::string kTypeNameI64("i64"); |
| 67 | static const std::string kTypeNameDouble("dbl"); |
| 68 | static const std::string kTypeNameStruct("rec"); |
| 69 | static const std::string kTypeNameString("str"); |
| 70 | static const std::string kTypeNameMap("map"); |
| 71 | static const std::string kTypeNameList("lst"); |
| 72 | static const std::string kTypeNameSet("set"); |
| 73 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 74 | static const std::string& getTypeNameForTypeID(TType typeID) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 75 | switch (typeID) { |
| 76 | case T_BOOL: |
| 77 | return kTypeNameBool; |
| 78 | case T_BYTE: |
| 79 | return kTypeNameByte; |
| 80 | case T_I16: |
| 81 | return kTypeNameI16; |
| 82 | case T_I32: |
| 83 | return kTypeNameI32; |
| 84 | case T_I64: |
| 85 | return kTypeNameI64; |
| 86 | case T_DOUBLE: |
| 87 | return kTypeNameDouble; |
| 88 | case T_STRING: |
| 89 | return kTypeNameString; |
| 90 | case T_STRUCT: |
| 91 | return kTypeNameStruct; |
| 92 | case T_MAP: |
| 93 | return kTypeNameMap; |
| 94 | case T_SET: |
| 95 | return kTypeNameSet; |
| 96 | case T_LIST: |
| 97 | return kTypeNameList; |
| 98 | default: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 99 | throw TProtocolException(TProtocolException::NOT_IMPLEMENTED, "Unrecognized type"); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 103 | static TType getTypeIDForTypeName(const std::string& name) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 104 | TType result = T_STOP; // Sentinel value |
David Reiss | 2c9824c | 2008-03-02 00:20:47 +0000 | [diff] [blame] | 105 | if (name.length() > 1) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 106 | switch (name[0]) { |
| 107 | case 'd': |
| 108 | result = T_DOUBLE; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 109 | break; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 110 | case 'i': |
| 111 | switch (name[1]) { |
| 112 | case '8': |
| 113 | result = T_BYTE; |
| 114 | break; |
| 115 | case '1': |
| 116 | result = T_I16; |
| 117 | break; |
| 118 | case '3': |
| 119 | result = T_I32; |
| 120 | break; |
| 121 | case '6': |
| 122 | result = T_I64; |
| 123 | break; |
| 124 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 125 | break; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 126 | case 'l': |
| 127 | result = T_LIST; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 128 | break; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 129 | case 'm': |
| 130 | result = T_MAP; |
| 131 | break; |
| 132 | case 'r': |
| 133 | result = T_STRUCT; |
| 134 | break; |
| 135 | case 's': |
| 136 | if (name[1] == 't') { |
| 137 | result = T_STRING; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 138 | } else if (name[1] == 'e') { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 139 | result = T_SET; |
| 140 | } |
| 141 | break; |
| 142 | case 't': |
| 143 | result = T_BOOL; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 144 | break; |
| 145 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 146 | } |
| 147 | if (result == T_STOP) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | throw TProtocolException(TProtocolException::NOT_IMPLEMENTED, "Unrecognized type"); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 149 | } |
| 150 | return result; |
| 151 | } |
| 152 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 153 | // This table describes the handling for the first 0x30 characters |
| 154 | // 0 : escape using "\u00xx" notation |
| 155 | // 1 : just output index |
| 156 | // <other> : escape using "\<other>" notation |
| 157 | static const uint8_t kJSONCharTable[0x30] = { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 158 | // 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 159 | 0, |
| 160 | 0, |
| 161 | 0, |
| 162 | 0, |
| 163 | 0, |
| 164 | 0, |
| 165 | 0, |
| 166 | 0, |
| 167 | 'b', |
| 168 | 't', |
| 169 | 'n', |
| 170 | 0, |
| 171 | 'f', |
| 172 | 'r', |
| 173 | 0, |
| 174 | 0, // 0 |
| 175 | 0, |
| 176 | 0, |
| 177 | 0, |
| 178 | 0, |
| 179 | 0, |
| 180 | 0, |
| 181 | 0, |
| 182 | 0, |
| 183 | 0, |
| 184 | 0, |
| 185 | 0, |
| 186 | 0, |
| 187 | 0, |
| 188 | 0, |
| 189 | 0, |
| 190 | 0, // 1 |
| 191 | 1, |
| 192 | 1, |
| 193 | '"', |
| 194 | 1, |
| 195 | 1, |
| 196 | 1, |
| 197 | 1, |
| 198 | 1, |
| 199 | 1, |
| 200 | 1, |
| 201 | 1, |
| 202 | 1, |
| 203 | 1, |
| 204 | 1, |
| 205 | 1, |
| 206 | 1, // 2 |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 209 | // This string's characters must match up with the elements in kEscapeCharVals. |
| 210 | // I don't have '/' on this list even though it appears on www.json.org -- |
| 211 | // it is not in the RFC |
| 212 | const static std::string kEscapeChars("\"\\bfnrt"); |
| 213 | |
| 214 | // The elements of this array must match up with the sequence of characters in |
| 215 | // kEscapeChars |
| 216 | const static uint8_t kEscapeCharVals[7] = { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 217 | '"', |
| 218 | '\\', |
| 219 | '\b', |
| 220 | '\f', |
| 221 | '\n', |
| 222 | '\r', |
| 223 | '\t', |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 226 | // Static helper functions |
| 227 | |
| 228 | // Read 1 character from the transport trans and verify that it is the |
| 229 | // expected character ch. |
| 230 | // Throw a protocol exception if it is not. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 231 | static uint32_t readSyntaxChar(TJSONProtocol::LookaheadReader& reader, uint8_t ch) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 232 | uint8_t ch2 = reader.read(); |
| 233 | if (ch2 != ch) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 234 | throw TProtocolException(TProtocolException::INVALID_DATA, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 235 | "Expected \'" + std::string((char*)&ch, 1) + "\'; got \'" |
| 236 | + std::string((char*)&ch2, 1) + "\'."); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 237 | } |
| 238 | return 1; |
| 239 | } |
| 240 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 241 | // Return the integer value of a hex character ch. |
| 242 | // Throw a protocol exception if the character is not [0-9a-f]. |
| 243 | static uint8_t hexVal(uint8_t ch) { |
| 244 | if ((ch >= '0') && (ch <= '9')) { |
| 245 | return ch - '0'; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 246 | } else if ((ch >= 'a') && (ch <= 'f')) { |
Bryan Duxbury | 4a2bc1b | 2010-11-03 17:57:38 +0000 | [diff] [blame] | 247 | return ch - 'a' + 10; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 248 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 249 | throw TProtocolException(TProtocolException::INVALID_DATA, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 250 | "Expected hex val ([0-9a-f]); got \'" + std::string((char*)&ch, 1) |
| 251 | + "\'."); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | // Return the hex character representing the integer val. The value is masked |
| 256 | // to make sure it is in the correct range. |
| 257 | static uint8_t hexChar(uint8_t val) { |
| 258 | val &= 0x0F; |
| 259 | if (val < 10) { |
| 260 | return val + '0'; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 261 | } else { |
Bryan Duxbury | 4a2bc1b | 2010-11-03 17:57:38 +0000 | [diff] [blame] | 262 | return val - 10 + 'a'; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | // Return true if the character ch is in [-+0-9.Ee]; false otherwise |
| 267 | static bool isJSONNumeric(uint8_t ch) { |
| 268 | switch (ch) { |
| 269 | case '+': |
| 270 | case '-': |
| 271 | case '.': |
| 272 | case '0': |
| 273 | case '1': |
| 274 | case '2': |
| 275 | case '3': |
| 276 | case '4': |
| 277 | case '5': |
| 278 | case '6': |
| 279 | case '7': |
| 280 | case '8': |
| 281 | case '9': |
| 282 | case 'E': |
| 283 | case 'e': |
| 284 | return true; |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 289 | // Return true if the code unit is high surrogate |
| 290 | static bool isHighSurrogate(uint16_t val) { |
| 291 | return val >= 0xD800 && val <= 0xDBFF; |
| 292 | } |
| 293 | |
| 294 | // Return true if the code unit is low surrogate |
| 295 | static bool isLowSurrogate(uint16_t val) { |
| 296 | return val >= 0xDC00 && val <= 0xDFFF; |
| 297 | } |
| 298 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 299 | /** |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 300 | * Class to serve as base JSON context and as base class for other context |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 301 | * implementations |
| 302 | */ |
| 303 | class TJSONContext { |
| 304 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 305 | public: |
| 306 | TJSONContext(){}; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 307 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 308 | virtual ~TJSONContext(){}; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 309 | |
| 310 | /** |
| 311 | * Write context data to the transport. Default is to do nothing. |
| 312 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 313 | virtual uint32_t write(TTransport& trans) { |
| 314 | (void)trans; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | }; |
| 317 | |
| 318 | /** |
| 319 | * Read context data from the transport. Default is to do nothing. |
| 320 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 321 | virtual uint32_t read(TJSONProtocol::LookaheadReader& reader) { |
| 322 | (void)reader; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | }; |
| 325 | |
| 326 | /** |
| 327 | * Return true if numbers need to be escaped as strings in this context. |
| 328 | * Default behavior is to return false. |
| 329 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 330 | virtual bool escapeNum() { return false; } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | // Context class for object member key-value pairs |
| 334 | class JSONPairContext : public TJSONContext { |
| 335 | |
| 336 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 337 | JSONPairContext() : first_(true), colon_(true) {} |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 338 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 339 | uint32_t write(TTransport& trans) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 340 | if (first_) { |
| 341 | first_ = false; |
| 342 | colon_ = true; |
| 343 | return 0; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 344 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 345 | trans.write(colon_ ? &kJSONPairSeparator : &kJSONElemSeparator, 1); |
| 346 | colon_ = !colon_; |
| 347 | return 1; |
| 348 | } |
| 349 | } |
| 350 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 351 | uint32_t read(TJSONProtocol::LookaheadReader& reader) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 352 | if (first_) { |
| 353 | first_ = false; |
| 354 | colon_ = true; |
| 355 | return 0; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 356 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 357 | uint8_t ch = (colon_ ? kJSONPairSeparator : kJSONElemSeparator); |
| 358 | colon_ = !colon_; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 359 | return readSyntaxChar(reader, ch); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | // Numbers must be turned into strings if they are the key part of a pair |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 364 | virtual bool escapeNum() { return colon_; } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 365 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 366 | private: |
| 367 | bool first_; |
| 368 | bool colon_; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | // Context class for lists |
| 372 | class JSONListContext : public TJSONContext { |
| 373 | |
| 374 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 375 | JSONListContext() : first_(true) {} |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 376 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 377 | uint32_t write(TTransport& trans) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 378 | if (first_) { |
| 379 | first_ = false; |
| 380 | return 0; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 381 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 382 | trans.write(&kJSONElemSeparator, 1); |
| 383 | return 1; |
| 384 | } |
| 385 | } |
| 386 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 387 | uint32_t read(TJSONProtocol::LookaheadReader& reader) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 388 | if (first_) { |
| 389 | first_ = false; |
| 390 | return 0; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 391 | } else { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 392 | return readSyntaxChar(reader, kJSONElemSeparator); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 396 | private: |
| 397 | bool first_; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 400 | TJSONProtocol::TJSONProtocol(boost::shared_ptr<TTransport> ptrans) |
| 401 | : TVirtualProtocol<TJSONProtocol>(ptrans), |
| 402 | trans_(ptrans.get()), |
| 403 | context_(new TJSONContext()), |
| 404 | reader_(*ptrans) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 407 | TJSONProtocol::~TJSONProtocol() { |
| 408 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 409 | |
| 410 | void TJSONProtocol::pushContext(boost::shared_ptr<TJSONContext> c) { |
| 411 | contexts_.push(context_); |
| 412 | context_ = c; |
| 413 | } |
| 414 | |
| 415 | void TJSONProtocol::popContext() { |
| 416 | context_ = contexts_.top(); |
| 417 | contexts_.pop(); |
| 418 | } |
| 419 | |
| 420 | // Write the character ch as a JSON escape sequence ("\u00xx") |
| 421 | uint32_t TJSONProtocol::writeJSONEscapeChar(uint8_t ch) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 422 | trans_->write((const uint8_t*)kJSONEscapePrefix.c_str(), |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 423 | static_cast<uint32_t>(kJSONEscapePrefix.length())); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 424 | uint8_t outCh = hexChar(ch >> 4); |
| 425 | trans_->write(&outCh, 1); |
| 426 | outCh = hexChar(ch); |
| 427 | trans_->write(&outCh, 1); |
| 428 | return 6; |
| 429 | } |
| 430 | |
| 431 | // Write the character ch as part of a JSON string, escaping as appropriate. |
| 432 | uint32_t TJSONProtocol::writeJSONChar(uint8_t ch) { |
| 433 | if (ch >= 0x30) { |
| 434 | if (ch == kJSONBackslash) { // Only special character >= 0x30 is '\' |
| 435 | trans_->write(&kJSONBackslash, 1); |
| 436 | trans_->write(&kJSONBackslash, 1); |
| 437 | return 2; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 438 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 439 | trans_->write(&ch, 1); |
| 440 | return 1; |
| 441 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 442 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 443 | uint8_t outCh = kJSONCharTable[ch]; |
| 444 | // Check if regular character, backslash escaped, or JSON escaped |
| 445 | if (outCh == 1) { |
| 446 | trans_->write(&ch, 1); |
| 447 | return 1; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 448 | } else if (outCh > 1) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 449 | trans_->write(&kJSONBackslash, 1); |
| 450 | trans_->write(&outCh, 1); |
| 451 | return 2; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 452 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 453 | return writeJSONEscapeChar(ch); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Write out the contents of the string str as a JSON string, escaping |
| 459 | // characters as appropriate. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 460 | uint32_t TJSONProtocol::writeJSONString(const std::string& str) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 461 | uint32_t result = context_->write(*trans_); |
| 462 | result += 2; // For quotes |
| 463 | trans_->write(&kJSONStringDelimiter, 1); |
| 464 | std::string::const_iterator iter(str.begin()); |
| 465 | std::string::const_iterator end(str.end()); |
| 466 | while (iter != end) { |
| 467 | result += writeJSONChar(*iter++); |
| 468 | } |
| 469 | trans_->write(&kJSONStringDelimiter, 1); |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | // Write out the contents of the string as JSON string, base64-encoding |
| 474 | // the string's contents, and escaping as appropriate |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 475 | uint32_t TJSONProtocol::writeJSONBase64(const std::string& str) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 476 | uint32_t result = context_->write(*trans_); |
| 477 | result += 2; // For quotes |
| 478 | trans_->write(&kJSONStringDelimiter, 1); |
| 479 | uint8_t b[4]; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 480 | const uint8_t* bytes = (const uint8_t*)str.c_str(); |
| 481 | if (str.length() > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 482 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 483 | uint32_t len = static_cast<uint32_t>(str.length()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 484 | while (len >= 3) { |
| 485 | // Encode 3 bytes at a time |
| 486 | base64_encode(bytes, 3, b); |
| 487 | trans_->write(b, 4); |
| 488 | result += 4; |
| 489 | bytes += 3; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 490 | len -= 3; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 491 | } |
| 492 | if (len) { // Handle remainder |
| 493 | base64_encode(bytes, len, b); |
| 494 | trans_->write(b, len + 1); |
| 495 | result += len + 1; |
| 496 | } |
| 497 | trans_->write(&kJSONStringDelimiter, 1); |
| 498 | return result; |
| 499 | } |
| 500 | |
| 501 | // Convert the given integer type to a JSON number, or a string |
| 502 | // if the context requires it (eg: key in a map pair). |
| 503 | template <typename NumberType> |
| 504 | uint32_t TJSONProtocol::writeJSONInteger(NumberType num) { |
| 505 | uint32_t result = context_->write(*trans_); |
| 506 | std::string val(boost::lexical_cast<std::string>(num)); |
| 507 | bool escapeNum = context_->escapeNum(); |
| 508 | if (escapeNum) { |
| 509 | trans_->write(&kJSONStringDelimiter, 1); |
| 510 | result += 1; |
| 511 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 512 | if (val.length() > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 513 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 514 | trans_->write((const uint8_t*)val.c_str(), static_cast<uint32_t>(val.length())); |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 515 | result += static_cast<uint32_t>(val.length()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 516 | if (escapeNum) { |
| 517 | trans_->write(&kJSONStringDelimiter, 1); |
| 518 | result += 1; |
| 519 | } |
| 520 | return result; |
| 521 | } |
| 522 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 523 | namespace { |
| 524 | std::string doubleToString(double d) { |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 525 | std::ostringstream str; |
| 526 | str.imbue(std::locale::classic()); |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 527 | const double max_digits10 = 2 + std::numeric_limits<double>::digits10; |
| 528 | str.precision(max_digits10); |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 529 | str << d; |
| 530 | return str.str(); |
| 531 | } |
| 532 | } |
| 533 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 534 | // Convert the given double to a JSON string, which is either the number, |
| 535 | // "NaN" or "Infinity" or "-Infinity". |
| 536 | uint32_t TJSONProtocol::writeJSONDouble(double num) { |
| 537 | uint32_t result = context_->write(*trans_); |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 538 | std::string val; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 539 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 540 | bool special = false; |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 541 | switch (boost::math::fpclassify(num)) { |
| 542 | case FP_INFINITE: |
| 543 | if (boost::math::signbit(num)) { |
| 544 | val = kThriftNegativeInfinity; |
| 545 | } else { |
| 546 | val = kThriftInfinity; |
| 547 | } |
| 548 | special = true; |
| 549 | break; |
| 550 | case FP_NAN: |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 551 | val = kThriftNan; |
| 552 | special = true; |
| 553 | break; |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 554 | default: |
| 555 | val = doubleToString(num); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | |
| 559 | bool escapeNum = special || context_->escapeNum(); |
| 560 | if (escapeNum) { |
| 561 | trans_->write(&kJSONStringDelimiter, 1); |
| 562 | result += 1; |
| 563 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 564 | if (val.length() > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 565 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 566 | trans_->write((const uint8_t*)val.c_str(), static_cast<uint32_t>(val.length())); |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 567 | result += static_cast<uint32_t>(val.length()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 568 | if (escapeNum) { |
| 569 | trans_->write(&kJSONStringDelimiter, 1); |
| 570 | result += 1; |
| 571 | } |
| 572 | return result; |
| 573 | } |
| 574 | |
| 575 | uint32_t TJSONProtocol::writeJSONObjectStart() { |
| 576 | uint32_t result = context_->write(*trans_); |
| 577 | trans_->write(&kJSONObjectStart, 1); |
| 578 | pushContext(boost::shared_ptr<TJSONContext>(new JSONPairContext())); |
| 579 | return result + 1; |
| 580 | } |
| 581 | |
| 582 | uint32_t TJSONProtocol::writeJSONObjectEnd() { |
| 583 | popContext(); |
| 584 | trans_->write(&kJSONObjectEnd, 1); |
| 585 | return 1; |
| 586 | } |
| 587 | |
| 588 | uint32_t TJSONProtocol::writeJSONArrayStart() { |
| 589 | uint32_t result = context_->write(*trans_); |
| 590 | trans_->write(&kJSONArrayStart, 1); |
| 591 | pushContext(boost::shared_ptr<TJSONContext>(new JSONListContext())); |
| 592 | return result + 1; |
| 593 | } |
| 594 | |
| 595 | uint32_t TJSONProtocol::writeJSONArrayEnd() { |
| 596 | popContext(); |
| 597 | trans_->write(&kJSONArrayEnd, 1); |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | uint32_t TJSONProtocol::writeMessageBegin(const std::string& name, |
| 602 | const TMessageType messageType, |
| 603 | const int32_t seqid) { |
| 604 | uint32_t result = writeJSONArrayStart(); |
| 605 | result += writeJSONInteger(kThriftVersion1); |
| 606 | result += writeJSONString(name); |
| 607 | result += writeJSONInteger(messageType); |
| 608 | result += writeJSONInteger(seqid); |
| 609 | return result; |
| 610 | } |
| 611 | |
| 612 | uint32_t TJSONProtocol::writeMessageEnd() { |
| 613 | return writeJSONArrayEnd(); |
| 614 | } |
| 615 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 616 | uint32_t TJSONProtocol::writeStructBegin(const char* name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 617 | (void)name; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 618 | return writeJSONObjectStart(); |
| 619 | } |
| 620 | |
| 621 | uint32_t TJSONProtocol::writeStructEnd() { |
| 622 | return writeJSONObjectEnd(); |
| 623 | } |
| 624 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 625 | uint32_t TJSONProtocol::writeFieldBegin(const char* name, |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 626 | const TType fieldType, |
| 627 | const int16_t fieldId) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 628 | (void)name; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 629 | uint32_t result = writeJSONInteger(fieldId); |
| 630 | result += writeJSONObjectStart(); |
| 631 | result += writeJSONString(getTypeNameForTypeID(fieldType)); |
| 632 | return result; |
| 633 | } |
| 634 | |
| 635 | uint32_t TJSONProtocol::writeFieldEnd() { |
| 636 | return writeJSONObjectEnd(); |
| 637 | } |
| 638 | |
| 639 | uint32_t TJSONProtocol::writeFieldStop() { |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | uint32_t TJSONProtocol::writeMapBegin(const TType keyType, |
| 644 | const TType valType, |
| 645 | const uint32_t size) { |
| 646 | uint32_t result = writeJSONArrayStart(); |
| 647 | result += writeJSONString(getTypeNameForTypeID(keyType)); |
| 648 | result += writeJSONString(getTypeNameForTypeID(valType)); |
| 649 | result += writeJSONInteger((int64_t)size); |
| 650 | result += writeJSONObjectStart(); |
| 651 | return result; |
| 652 | } |
| 653 | |
| 654 | uint32_t TJSONProtocol::writeMapEnd() { |
dtmuller | 4300b72 | 2016-07-15 10:05:43 +0200 | [diff] [blame] | 655 | uint32_t result = writeJSONObjectEnd(); |
| 656 | result += writeJSONArrayEnd(); |
| 657 | return result; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 660 | uint32_t TJSONProtocol::writeListBegin(const TType elemType, const uint32_t size) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 661 | uint32_t result = writeJSONArrayStart(); |
| 662 | result += writeJSONString(getTypeNameForTypeID(elemType)); |
| 663 | result += writeJSONInteger((int64_t)size); |
| 664 | return result; |
| 665 | } |
| 666 | |
| 667 | uint32_t TJSONProtocol::writeListEnd() { |
| 668 | return writeJSONArrayEnd(); |
| 669 | } |
| 670 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 671 | uint32_t TJSONProtocol::writeSetBegin(const TType elemType, const uint32_t size) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 672 | uint32_t result = writeJSONArrayStart(); |
| 673 | result += writeJSONString(getTypeNameForTypeID(elemType)); |
| 674 | result += writeJSONInteger((int64_t)size); |
| 675 | return result; |
| 676 | } |
| 677 | |
| 678 | uint32_t TJSONProtocol::writeSetEnd() { |
| 679 | return writeJSONArrayEnd(); |
| 680 | } |
| 681 | |
| 682 | uint32_t TJSONProtocol::writeBool(const bool value) { |
| 683 | return writeJSONInteger(value); |
| 684 | } |
| 685 | |
| 686 | uint32_t TJSONProtocol::writeByte(const int8_t byte) { |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 687 | // writeByte() must be handled specially because boost::lexical cast sees |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 688 | // int8_t as a text type instead of an integer type |
| 689 | return writeJSONInteger((int16_t)byte); |
| 690 | } |
| 691 | |
| 692 | uint32_t TJSONProtocol::writeI16(const int16_t i16) { |
| 693 | return writeJSONInteger(i16); |
| 694 | } |
| 695 | |
| 696 | uint32_t TJSONProtocol::writeI32(const int32_t i32) { |
| 697 | return writeJSONInteger(i32); |
| 698 | } |
| 699 | |
| 700 | uint32_t TJSONProtocol::writeI64(const int64_t i64) { |
| 701 | return writeJSONInteger(i64); |
| 702 | } |
| 703 | |
| 704 | uint32_t TJSONProtocol::writeDouble(const double dub) { |
| 705 | return writeJSONDouble(dub); |
| 706 | } |
| 707 | |
| 708 | uint32_t TJSONProtocol::writeString(const std::string& str) { |
| 709 | return writeJSONString(str); |
| 710 | } |
| 711 | |
| 712 | uint32_t TJSONProtocol::writeBinary(const std::string& str) { |
| 713 | return writeJSONBase64(str); |
| 714 | } |
| 715 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 716 | /** |
| 717 | * Reading functions |
| 718 | */ |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 719 | |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 720 | // Reads 1 byte and verifies that it matches ch. |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 721 | uint32_t TJSONProtocol::readJSONSyntaxChar(uint8_t ch) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 722 | return readSyntaxChar(reader_, ch); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Decodes the four hex parts of a JSON escaped string character and returns |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 726 | // the UTF-16 code unit via out. |
| 727 | uint32_t TJSONProtocol::readJSONEscapeChar(uint16_t* out) { |
| 728 | uint8_t b[4]; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 729 | b[0] = reader_.read(); |
| 730 | b[1] = reader_.read(); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 731 | b[2] = reader_.read(); |
| 732 | b[3] = reader_.read(); |
| 733 | |
| 734 | *out = (hexVal(b[0]) << 12) |
| 735 | + (hexVal(b[1]) << 8) + (hexVal(b[2]) << 4) + hexVal(b[3]); |
| 736 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 737 | return 4; |
| 738 | } |
| 739 | |
| 740 | // Decodes a JSON string, including unescaping, and returns the string via str |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 741 | uint32_t TJSONProtocol::readJSONString(std::string& str, bool skipContext) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 742 | uint32_t result = (skipContext ? 0 : context_->read(reader_)); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 743 | result += readJSONSyntaxChar(kJSONStringDelimiter); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 744 | std::vector<uint16_t> codeunits; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 745 | uint8_t ch; |
David Reiss | 9163073 | 2008-02-28 21:11:39 +0000 | [diff] [blame] | 746 | str.clear(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 747 | while (true) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 748 | ch = reader_.read(); |
| 749 | ++result; |
| 750 | if (ch == kJSONStringDelimiter) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 751 | break; |
| 752 | } |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 753 | if (ch == kJSONBackslash) { |
| 754 | ch = reader_.read(); |
| 755 | ++result; |
| 756 | if (ch == kJSONEscapeChar) { |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 757 | uint16_t cp; |
| 758 | result += readJSONEscapeChar(&cp); |
| 759 | if (isHighSurrogate(cp)) { |
| 760 | codeunits.push_back(cp); |
| 761 | } else { |
| 762 | if (isLowSurrogate(cp) |
| 763 | && codeunits.empty()) { |
| 764 | throw TProtocolException(TProtocolException::INVALID_DATA, |
| 765 | "Missing UTF-16 high surrogate pair."); |
| 766 | } |
| 767 | codeunits.push_back(cp); |
| 768 | codeunits.push_back(0); |
| 769 | str += boost::locale::conv::utf_to_utf<char>(codeunits.data()); |
| 770 | codeunits.clear(); |
| 771 | } |
| 772 | continue; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 773 | } else { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 774 | size_t pos = kEscapeChars.find(ch); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 775 | if (pos == std::string::npos) { |
| 776 | throw TProtocolException(TProtocolException::INVALID_DATA, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 777 | "Expected control char, got '" + std::string((const char*)&ch, 1) |
| 778 | + "'."); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 779 | } |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 780 | ch = kEscapeCharVals[pos]; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 783 | if (!codeunits.empty()) { |
| 784 | throw TProtocolException(TProtocolException::INVALID_DATA, |
| 785 | "Missing UTF-16 low surrogate pair."); |
| 786 | } |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 787 | str += ch; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 788 | } |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 789 | |
| 790 | if (!codeunits.empty()) { |
| 791 | throw TProtocolException(TProtocolException::INVALID_DATA, |
| 792 | "Missing UTF-16 low surrogate pair."); |
| 793 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 794 | return result; |
| 795 | } |
| 796 | |
| 797 | // Reads a block of base64 characters, decoding it, and returns via str |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 798 | uint32_t TJSONProtocol::readJSONBase64(std::string& str) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 799 | std::string tmp; |
| 800 | uint32_t result = readJSONString(tmp); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 801 | uint8_t* b = (uint8_t*)tmp.c_str(); |
| 802 | if (tmp.length() > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 803 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 804 | uint32_t len = static_cast<uint32_t>(tmp.length()); |
David Reiss | 9163073 | 2008-02-28 21:11:39 +0000 | [diff] [blame] | 805 | str.clear(); |
Nobuaki Sukegawa | a175437 | 2015-10-10 10:44:07 +0900 | [diff] [blame] | 806 | // Ignore padding |
Nobuaki Sukegawa | dfb6896 | 2015-12-09 22:09:26 +0900 | [diff] [blame] | 807 | if (len >= 2) { |
| 808 | uint32_t bound = len - 2; |
| 809 | for (uint32_t i = len - 1; i >= bound && b[i] == '='; --i) { |
| 810 | --len; |
| 811 | } |
Nobuaki Sukegawa | a175437 | 2015-10-10 10:44:07 +0900 | [diff] [blame] | 812 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 813 | while (len >= 4) { |
| 814 | base64_decode(b, 4); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 815 | str.append((const char*)b, 3); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 816 | b += 4; |
| 817 | len -= 4; |
| 818 | } |
| 819 | // Don't decode if we hit the end or got a single leftover byte (invalid |
| 820 | // base64 but legal for skip of regular string type) |
| 821 | if (len > 1) { |
| 822 | base64_decode(b, len); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 823 | str.append((const char*)b, len - 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 824 | } |
| 825 | return result; |
| 826 | } |
| 827 | |
| 828 | // Reads a sequence of characters, stopping at the first one that is not |
| 829 | // a valid JSON numeric character. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 830 | uint32_t TJSONProtocol::readJSONNumericChars(std::string& str) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 831 | uint32_t result = 0; |
David Reiss | 9163073 | 2008-02-28 21:11:39 +0000 | [diff] [blame] | 832 | str.clear(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 833 | while (true) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 834 | uint8_t ch = reader_.peek(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 835 | if (!isJSONNumeric(ch)) { |
| 836 | break; |
| 837 | } |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 838 | reader_.read(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 839 | str += ch; |
| 840 | ++result; |
| 841 | } |
| 842 | return result; |
| 843 | } |
| 844 | |
| 845 | // Reads a sequence of characters and assembles them into a number, |
| 846 | // returning them via num |
| 847 | template <typename NumberType> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 848 | uint32_t TJSONProtocol::readJSONInteger(NumberType& num) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 849 | uint32_t result = context_->read(reader_); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 850 | if (context_->escapeNum()) { |
| 851 | result += readJSONSyntaxChar(kJSONStringDelimiter); |
| 852 | } |
| 853 | std::string str; |
| 854 | result += readJSONNumericChars(str); |
| 855 | try { |
| 856 | num = boost::lexical_cast<NumberType>(str); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 857 | } catch (boost::bad_lexical_cast e) { |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 858 | throw TProtocolException(TProtocolException::INVALID_DATA, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 859 | "Expected numeric value; got \"" + str + "\""); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 860 | } |
| 861 | if (context_->escapeNum()) { |
| 862 | result += readJSONSyntaxChar(kJSONStringDelimiter); |
| 863 | } |
| 864 | return result; |
| 865 | } |
| 866 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 867 | namespace { |
| 868 | double stringToDouble(const std::string& s) { |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 869 | double d; |
| 870 | std::istringstream str(s); |
| 871 | str.imbue(std::locale::classic()); |
| 872 | str >> d; |
| 873 | if (str.bad() || !str.eof()) |
| 874 | throw std::runtime_error(s); |
| 875 | return d; |
| 876 | } |
| 877 | } |
| 878 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 879 | // Reads a JSON number or string and interprets it as a double. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 880 | uint32_t TJSONProtocol::readJSONDouble(double& num) { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 881 | uint32_t result = context_->read(reader_); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 882 | std::string str; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 883 | if (reader_.peek() == kJSONStringDelimiter) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 884 | result += readJSONString(str, true); |
| 885 | // Check for NaN, Infinity and -Infinity |
| 886 | if (str == kThriftNan) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 887 | num = HUGE_VAL / HUGE_VAL; // generates NaN |
| 888 | } else if (str == kThriftInfinity) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 889 | num = HUGE_VAL; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 890 | } else if (str == kThriftNegativeInfinity) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 891 | num = -HUGE_VAL; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 892 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 893 | if (!context_->escapeNum()) { |
| 894 | // Throw exception -- we should not be in a string in this case |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 895 | throw TProtocolException(TProtocolException::INVALID_DATA, |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 896 | "Numeric data unexpectedly quoted"); |
| 897 | } |
| 898 | try { |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 899 | num = stringToDouble(str); |
| 900 | } catch (std::runtime_error e) { |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 901 | throw TProtocolException(TProtocolException::INVALID_DATA, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 902 | "Expected numeric value; got \"" + str + "\""); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 905 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 906 | if (context_->escapeNum()) { |
| 907 | // This will throw - we should have had a quote if escapeNum == true |
| 908 | readJSONSyntaxChar(kJSONStringDelimiter); |
| 909 | } |
| 910 | result += readJSONNumericChars(str); |
| 911 | try { |
Konrad Grochowski | 12b06e4 | 2015-02-21 13:48:56 +0100 | [diff] [blame] | 912 | num = stringToDouble(str); |
| 913 | } catch (std::runtime_error e) { |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 914 | throw TProtocolException(TProtocolException::INVALID_DATA, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 915 | "Expected numeric value; got \"" + str + "\""); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | return result; |
| 919 | } |
| 920 | |
| 921 | uint32_t TJSONProtocol::readJSONObjectStart() { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 922 | uint32_t result = context_->read(reader_); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 923 | result += readJSONSyntaxChar(kJSONObjectStart); |
| 924 | pushContext(boost::shared_ptr<TJSONContext>(new JSONPairContext())); |
| 925 | return result; |
| 926 | } |
| 927 | |
| 928 | uint32_t TJSONProtocol::readJSONObjectEnd() { |
| 929 | uint32_t result = readJSONSyntaxChar(kJSONObjectEnd); |
| 930 | popContext(); |
| 931 | return result; |
| 932 | } |
| 933 | |
| 934 | uint32_t TJSONProtocol::readJSONArrayStart() { |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 935 | uint32_t result = context_->read(reader_); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 936 | result += readJSONSyntaxChar(kJSONArrayStart); |
| 937 | pushContext(boost::shared_ptr<TJSONContext>(new JSONListContext())); |
| 938 | return result; |
| 939 | } |
| 940 | |
| 941 | uint32_t TJSONProtocol::readJSONArrayEnd() { |
| 942 | uint32_t result = readJSONSyntaxChar(kJSONArrayEnd); |
| 943 | popContext(); |
| 944 | return result; |
| 945 | } |
| 946 | |
| 947 | uint32_t TJSONProtocol::readMessageBegin(std::string& name, |
| 948 | TMessageType& messageType, |
| 949 | int32_t& seqid) { |
| 950 | uint32_t result = readJSONArrayStart(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 951 | uint64_t tmpVal = 0; |
| 952 | result += readJSONInteger(tmpVal); |
| 953 | if (tmpVal != kThriftVersion1) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 954 | throw TProtocolException(TProtocolException::BAD_VERSION, "Message contained bad version."); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 955 | } |
| 956 | result += readJSONString(name); |
| 957 | result += readJSONInteger(tmpVal); |
| 958 | messageType = (TMessageType)tmpVal; |
| 959 | result += readJSONInteger(tmpVal); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 960 | if (tmpVal > static_cast<uint64_t>((std::numeric_limits<int32_t>::max)())) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 961 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 962 | seqid = static_cast<int32_t>(tmpVal); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 963 | return result; |
| 964 | } |
| 965 | |
| 966 | uint32_t TJSONProtocol::readMessageEnd() { |
| 967 | return readJSONArrayEnd(); |
| 968 | } |
| 969 | |
| 970 | uint32_t TJSONProtocol::readStructBegin(std::string& name) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 971 | (void)name; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 972 | return readJSONObjectStart(); |
| 973 | } |
| 974 | |
| 975 | uint32_t TJSONProtocol::readStructEnd() { |
| 976 | return readJSONObjectEnd(); |
| 977 | } |
| 978 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 979 | uint32_t TJSONProtocol::readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId) { |
| 980 | (void)name; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 981 | uint32_t result = 0; |
David Reiss | 1e62ab4 | 2008-02-21 22:37:45 +0000 | [diff] [blame] | 982 | // Check if we hit the end of the list |
| 983 | uint8_t ch = reader_.peek(); |
| 984 | if (ch == kJSONObjectEnd) { |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 985 | fieldType = apache::thrift::protocol::T_STOP; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 986 | } else { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 987 | uint64_t tmpVal = 0; |
| 988 | std::string tmpStr; |
| 989 | result += readJSONInteger(tmpVal); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 990 | if (tmpVal > static_cast<uint32_t>((std::numeric_limits<int16_t>::max)())) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 991 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 992 | fieldId = static_cast<int16_t>(tmpVal); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 993 | result += readJSONObjectStart(); |
| 994 | result += readJSONString(tmpStr); |
| 995 | fieldType = getTypeIDForTypeName(tmpStr); |
| 996 | } |
| 997 | return result; |
| 998 | } |
| 999 | |
| 1000 | uint32_t TJSONProtocol::readFieldEnd() { |
| 1001 | return readJSONObjectEnd(); |
| 1002 | } |
| 1003 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1004 | uint32_t TJSONProtocol::readMapBegin(TType& keyType, TType& valType, uint32_t& size) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1005 | uint64_t tmpVal = 0; |
| 1006 | std::string tmpStr; |
| 1007 | uint32_t result = readJSONArrayStart(); |
| 1008 | result += readJSONString(tmpStr); |
| 1009 | keyType = getTypeIDForTypeName(tmpStr); |
| 1010 | result += readJSONString(tmpStr); |
| 1011 | valType = getTypeIDForTypeName(tmpStr); |
| 1012 | result += readJSONInteger(tmpVal); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1013 | if (tmpVal > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 1014 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 1015 | size = static_cast<uint32_t>(tmpVal); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1016 | result += readJSONObjectStart(); |
| 1017 | return result; |
| 1018 | } |
| 1019 | |
| 1020 | uint32_t TJSONProtocol::readMapEnd() { |
dtmuller | 4300b72 | 2016-07-15 10:05:43 +0200 | [diff] [blame] | 1021 | uint32_t result = readJSONObjectEnd(); |
| 1022 | result += readJSONArrayEnd(); |
| 1023 | return result; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1026 | uint32_t TJSONProtocol::readListBegin(TType& elemType, uint32_t& size) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1027 | uint64_t tmpVal = 0; |
| 1028 | std::string tmpStr; |
| 1029 | uint32_t result = readJSONArrayStart(); |
| 1030 | result += readJSONString(tmpStr); |
| 1031 | elemType = getTypeIDForTypeName(tmpStr); |
| 1032 | result += readJSONInteger(tmpVal); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1033 | if (tmpVal > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 1034 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 1035 | size = static_cast<uint32_t>(tmpVal); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1036 | return result; |
| 1037 | } |
| 1038 | |
| 1039 | uint32_t TJSONProtocol::readListEnd() { |
| 1040 | return readJSONArrayEnd(); |
| 1041 | } |
| 1042 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1043 | uint32_t TJSONProtocol::readSetBegin(TType& elemType, uint32_t& size) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1044 | uint64_t tmpVal = 0; |
| 1045 | std::string tmpStr; |
| 1046 | uint32_t result = readJSONArrayStart(); |
| 1047 | result += readJSONString(tmpStr); |
| 1048 | elemType = getTypeIDForTypeName(tmpStr); |
| 1049 | result += readJSONInteger(tmpVal); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1050 | if (tmpVal > (std::numeric_limits<uint32_t>::max)()) |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 1051 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 1052 | size = static_cast<uint32_t>(tmpVal); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1053 | return result; |
| 1054 | } |
| 1055 | |
| 1056 | uint32_t TJSONProtocol::readSetEnd() { |
| 1057 | return readJSONArrayEnd(); |
| 1058 | } |
| 1059 | |
| 1060 | uint32_t TJSONProtocol::readBool(bool& value) { |
| 1061 | return readJSONInteger(value); |
| 1062 | } |
| 1063 | |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 1064 | // readByte() must be handled properly because boost::lexical cast sees int8_t |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1065 | // as a text type instead of an integer type |
| 1066 | uint32_t TJSONProtocol::readByte(int8_t& byte) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1067 | int16_t tmp = (int16_t)byte; |
| 1068 | uint32_t result = readJSONInteger(tmp); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1069 | assert(tmp < 256); |
| 1070 | byte = (int8_t)tmp; |
| 1071 | return result; |
| 1072 | } |
| 1073 | |
| 1074 | uint32_t TJSONProtocol::readI16(int16_t& i16) { |
| 1075 | return readJSONInteger(i16); |
| 1076 | } |
| 1077 | |
| 1078 | uint32_t TJSONProtocol::readI32(int32_t& i32) { |
| 1079 | return readJSONInteger(i32); |
| 1080 | } |
| 1081 | |
| 1082 | uint32_t TJSONProtocol::readI64(int64_t& i64) { |
| 1083 | return readJSONInteger(i64); |
| 1084 | } |
| 1085 | |
| 1086 | uint32_t TJSONProtocol::readDouble(double& dub) { |
| 1087 | return readJSONDouble(dub); |
| 1088 | } |
| 1089 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1090 | uint32_t TJSONProtocol::readString(std::string& str) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1091 | return readJSONString(str); |
| 1092 | } |
| 1093 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1094 | uint32_t TJSONProtocol::readBinary(std::string& str) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 1095 | return readJSONBase64(str); |
| 1096 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | } // apache::thrift::protocol |