File indexing completed on 2026-04-17 08:35:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef _THRIFT_PROTOCOL_TJSONPROTOCOL_H_
0021 #define _THRIFT_PROTOCOL_TJSONPROTOCOL_H_ 1
0022
0023 #include <thrift/protocol/TVirtualProtocol.h>
0024
0025 #include <stack>
0026
0027 namespace apache {
0028 namespace thrift {
0029 namespace protocol {
0030
0031
0032 class TJSONContext;
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> {
0098 public:
0099 TJSONProtocol(std::shared_ptr<TTransport> ptrans);
0100
0101 ~TJSONProtocol() override;
0102
0103 private:
0104 void pushContext(std::shared_ptr<TJSONContext> c);
0105
0106 void popContext();
0107
0108 uint32_t writeJSONEscapeChar(uint8_t ch);
0109
0110 uint32_t writeJSONChar(uint8_t ch);
0111
0112 uint32_t writeJSONString(const std::string& str);
0113
0114 uint32_t writeJSONBase64(const std::string& str);
0115
0116 template <typename NumberType>
0117 uint32_t writeJSONInteger(NumberType num);
0118
0119 uint32_t writeJSONDouble(double num);
0120
0121 uint32_t writeJSONObjectStart();
0122
0123 uint32_t writeJSONObjectEnd();
0124
0125 uint32_t writeJSONArrayStart();
0126
0127 uint32_t writeJSONArrayEnd();
0128
0129 uint32_t readJSONSyntaxChar(uint8_t ch);
0130
0131 uint32_t readJSONEscapeChar(uint16_t* out);
0132
0133 uint32_t readJSONString(std::string& str, bool skipContext = false);
0134
0135 uint32_t readJSONBase64(std::string& str);
0136
0137 uint32_t readJSONNumericChars(std::string& str);
0138
0139 template <typename NumberType>
0140 uint32_t readJSONInteger(NumberType& num);
0141
0142 uint32_t readJSONDouble(double& num);
0143
0144 uint32_t readJSONObjectStart();
0145
0146 uint32_t readJSONObjectEnd();
0147
0148 uint32_t readJSONArrayStart();
0149
0150 uint32_t readJSONArrayEnd();
0151
0152 public:
0153
0154
0155
0156
0157 uint32_t writeMessageBegin(const std::string& name,
0158 const TMessageType messageType,
0159 const int32_t seqid);
0160
0161 uint32_t writeMessageEnd();
0162
0163 uint32_t writeStructBegin(const char* name);
0164
0165 uint32_t writeStructEnd();
0166
0167 uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
0168
0169 uint32_t writeFieldEnd();
0170
0171 uint32_t writeFieldStop();
0172
0173 uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
0174
0175 uint32_t writeMapEnd();
0176
0177 uint32_t writeListBegin(const TType elemType, const uint32_t size);
0178
0179 uint32_t writeListEnd();
0180
0181 uint32_t writeSetBegin(const TType elemType, const uint32_t size);
0182
0183 uint32_t writeSetEnd();
0184
0185 uint32_t writeBool(const bool value);
0186
0187 uint32_t writeByte(const int8_t byte);
0188
0189 uint32_t writeI16(const int16_t i16);
0190
0191 uint32_t writeI32(const int32_t i32);
0192
0193 uint32_t writeI64(const int64_t i64);
0194
0195 uint32_t writeDouble(const double dub);
0196
0197 uint32_t writeString(const std::string& str);
0198
0199 uint32_t writeBinary(const std::string& str);
0200
0201 uint32_t writeUUID(const TUuid& uuid);
0202
0203
0204
0205
0206
0207 uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
0208
0209 uint32_t readMessageEnd();
0210
0211 uint32_t readStructBegin(std::string& name);
0212
0213 uint32_t readStructEnd();
0214
0215 uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
0216
0217 uint32_t readFieldEnd();
0218
0219 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
0220
0221 uint32_t readMapEnd();
0222
0223 uint32_t readListBegin(TType& elemType, uint32_t& size);
0224
0225 uint32_t readListEnd();
0226
0227 uint32_t readSetBegin(TType& elemType, uint32_t& size);
0228
0229 uint32_t readSetEnd();
0230
0231 uint32_t readBool(bool& value);
0232
0233
0234 using TVirtualProtocol<TJSONProtocol>::readBool;
0235
0236 uint32_t readByte(int8_t& byte);
0237
0238 uint32_t readI16(int16_t& i16);
0239
0240 uint32_t readI32(int32_t& i32);
0241
0242 uint32_t readI64(int64_t& i64);
0243
0244 uint32_t readDouble(double& dub);
0245
0246 uint32_t readString(std::string& str);
0247
0248 uint32_t readBinary(std::string& str);
0249
0250 uint32_t readUUID(TUuid& uuid);
0251
0252 int getMinSerializedSize(TType type) override;
0253
0254 void checkReadBytesAvailable(TSet& set) override
0255 {
0256 trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
0257 }
0258
0259 void checkReadBytesAvailable(TList& list) override
0260 {
0261 trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
0262 }
0263
0264 void checkReadBytesAvailable(TMap& map) override
0265 {
0266 int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
0267 trans_->checkReadBytesAvailable(map.size_ * elmSize);
0268 }
0269
0270 class LookaheadReader {
0271
0272 public:
0273 LookaheadReader(TTransport& trans) : trans_(&trans), hasData_(false), data_(0) {}
0274
0275 uint8_t read() {
0276 if (hasData_) {
0277 hasData_ = false;
0278 } else {
0279 trans_->readAll(&data_, 1);
0280 }
0281 return data_;
0282 }
0283
0284 uint8_t peek() {
0285 if (!hasData_) {
0286 trans_->readAll(&data_, 1);
0287 }
0288 hasData_ = true;
0289 return data_;
0290 }
0291
0292 private:
0293 TTransport* trans_;
0294 bool hasData_;
0295 uint8_t data_;
0296 };
0297
0298 private:
0299 TTransport* trans_;
0300
0301 std::stack<std::shared_ptr<TJSONContext> > contexts_;
0302 std::shared_ptr<TJSONContext> context_;
0303 LookaheadReader reader_;
0304 };
0305
0306
0307
0308
0309 class TJSONProtocolFactory : public TProtocolFactory {
0310 public:
0311 TJSONProtocolFactory() = default;
0312
0313 ~TJSONProtocolFactory() override = default;
0314
0315 std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> trans) override {
0316 return std::shared_ptr<TProtocol>(new TJSONProtocol(trans));
0317 }
0318 };
0319 }
0320 }
0321 }
0322
0323
0324 #include <thrift/transport/TBufferTransports.h>
0325
0326 namespace apache {
0327 namespace thrift {
0328
0329 template <typename ThriftStruct>
0330 std::string ThriftJSONString(const ThriftStruct& ts) {
0331 using namespace apache::thrift::transport;
0332 using namespace apache::thrift::protocol;
0333 auto* buffer = new TMemoryBuffer;
0334 std::shared_ptr<TTransport> trans(buffer);
0335 TJSONProtocol protocol(trans);
0336
0337 ts.write(&protocol);
0338
0339 uint8_t* buf;
0340 uint32_t size;
0341 buffer->getBuffer(&buf, &size);
0342 return std::string((char*)buf, (unsigned int)size);
0343 }
0344 }
0345 }
0346
0347 #endif