brpc_protobuf_json_unittest.cpp 56.1 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
zhujiashun's avatar
zhujiashun committed
2 3 4 5 6 7 8

#include <sys/time.h>
#include <gtest/gtest.h>
#include <iostream>
#include <fstream>
#include <string>
#include <google/protobuf/text_format.h>
9 10
#include "butil/iobuf.h"
#include "butil/third_party/rapidjson/rapidjson.h"
11
#include "butil/time.h"
12 13 14 15
#include "butil/gperftools_profiler.h"
#include "json2pb/pb_to_json.h"
#include "json2pb/json_to_pb.h"
#include "json2pb/encode_decode.h"
zhujiashun's avatar
zhujiashun committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#include "message.pb.h"
#include "addressbook1.pb.h"
#include "addressbook.pb.h"
#include "addressbook_encode_decode.pb.h"
#include "addressbook_map.pb.h"

namespace {  // just for coding-style check

using addressbook::AddressBook;
using addressbook::Person;

class ProtobufJsonTest : public testing::Test {
protected:
    void SetUp() {}
    void TearDown() {}
};

inline int64_t gettimeofday_us() {
    timeval now;
    gettimeofday(&now, NULL);
    return now.tv_sec * 1000000L + now.tv_usec;
}

TEST_F(ProtobufJsonTest, json_to_pb_normal_case) {
    const int N = 1000;
    int64_t total_tm = 0;
    int64_t total_tm2 = 0;
    for (int i = 0; i < N; ++i) {
        std::string info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\"ext\":"
                            "{\"age\":1666666666, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":1},"
                            "\"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,"
                            "\"ext\":{\"age\":1666666660, \"databyte\":\"d2VsY29tZQ==\","
                            "\"enumtype\":2},\"uid\":\"someone0\"}], \"judge\":false,"
                            "\"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
        std::string error; 
  
        JsonContextBody data;
        const int64_t tm1 = gettimeofday_us();
        const bool ret = json2pb::JsonToProtoMessage(info3, &data, &error);
        const int64_t tm2 = gettimeofday_us();
        total_tm += tm2 - tm1;
        ASSERT_TRUE(ret);
    
        std::string info4;  
        std::string error1;
        const int64_t tm3 = gettimeofday_us();
        bool ret2 = json2pb::ProtoMessageToJson(data, &info4, &error1);
        const int64_t tm4 = gettimeofday_us();
        ASSERT_TRUE(ret2);
        total_tm2 += tm4 - tm3;
#ifndef RAPIDJSON_VERSION_0_1
        ASSERT_STREQ("{\"data\":[1,2,3,4,5,6,7,8,9,10],"
                     "\"judge\":false,\"spur\":2.0,\"content\":[{\"uid\":\"someone\","
                     "\"distance\":1.0,\"ext\":{\"age\":1666666666,\"databyte\":\"d2VsY29tZQ==\","
                     "\"enumtype\":\"HOME\"}},\{\"uid\":\"someone0\",\"distance\":10.0,\"ext\":"
                     "{\"age\":1666666660,\"databyte\":\"d2VsY29tZQ==\",\"enumtype\":\"WORK\"}}]}", 
                      info4.data());
#else
        ASSERT_STREQ("{\"data\":[1,2,3,4,5,6,7,8,9,10],"
                     "\"judge\":false,\"spur\":2,\"content\":[{\"uid\":\"someone\","
                     "\"distance\":1,\"ext\":{\"age\":1666666666,\"databyte\":\"d2VsY29tZQ==\","
                     "\"enumtype\":\"HOME\"}},\{\"uid\":\"someone0\",\"distance\":10,\"ext\":"
                     "{\"age\":1666666660,\"databyte\":\"d2VsY29tZQ==\",\"enumtype\":\"WORK\"}}]}", 
                     info4.data());
#endif
    }
    std::cout << "json2pb=" << total_tm / N
              << "us pb2json=" << total_tm2 / N << "us"
              << std::endl;
}

TEST_F(ProtobufJsonTest, json_base64_string_to_pb_types_case) {
    std::string info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\"ext\":"
                        "{\"age\":1666666666, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":1},"
                        "\"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,"
                        "\"ext\":{\"age\":1666666660, \"databyte\":\"d2VsY29tZTA=\","
                        "\"enumtype\":2},\"uid\":\"someone0\"}], \"judge\":false,"
                        "\"spur\":2}";  
    std::string error; 

    JsonContextBody data;
    json2pb::Json2PbOptions options_j2pb;
    options_j2pb.base64_to_bytes = true;
    const bool ret = json2pb::JsonToProtoMessage(info3, &data, options_j2pb, &error);
    ASSERT_TRUE(ret);
    ASSERT_TRUE(data.content_size() == 2);
    ASSERT_EQ(data.content(0).ext().databyte(), "welcome");
    ASSERT_EQ(data.content(1).ext().databyte(), "welcome0");

    std::string info4;  
    std::string error1;
    json2pb::Pb2JsonOptions options_pb2j;
    options_pb2j.bytes_to_base64 = true;
    bool ret2 = json2pb::ProtoMessageToJson(data, &info4, options_pb2j, &error1);
    ASSERT_TRUE(ret2);
#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"judge\":false,\"spur\":2.0,\"content\":[{\"uid\":\"someone\","
                 "\"distance\":1.0,\"ext\":{\"age\":1666666666,\"databyte\":\"d2VsY29tZQ==\","
                 "\"enumtype\":\"HOME\"}},\{\"uid\":\"someone0\",\"distance\":10.0,\"ext\":"
                 "{\"age\":1666666660,\"databyte\":\"d2VsY29tZTA=\",\"enumtype\":\"WORK\"}}]}", 
                  info4.data());
#else
    ASSERT_STREQ("{\"judge\":false,\"spur\":2,\"content\":[{\"uid\":\"someone\","
                 "\"distance\":1,\"ext\":{\"age\":1666666666,\"databyte\":\"d2VsY29tZQ==\","
                 "\"enumtype\":\"HOME\"}},\{\"uid\":\"someone0\",\"distance\":10,\"ext\":"
                 "{\"age\":1666666660,\"databyte\":\"d2VsY29tZTA=\",\"enumtype\":\"WORK\"}}]}", 
                 info4.data());
#endif
}

TEST_F(ProtobufJsonTest, json_to_pb_map_case) {
    std::string json = "{\"addr\":\"baidu.com\","
                       "\"numbers\":{\"tel\":123456,\"cell\":654321},"
                       "\"contacts\":{\"email\":\"frank@baidu.com\","
                       "               \"office\":\"Shanghai\"},"
                       "\"friends\":{\"John\":[{\"school\":\"SJTU\",\"year\":2007}]}}";
    std::string error;
    AddressNoMap ab1;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab1, &error));
    ASSERT_EQ("baidu.com", ab1.addr());

    AddressIntMap ab2;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab2, &error));
    ASSERT_EQ("baidu.com", ab2.addr());
    ASSERT_EQ("tel", ab2.numbers(0).key());
    ASSERT_EQ(123456, ab2.numbers(0).value());
    ASSERT_EQ("cell", ab2.numbers(1).key());
    ASSERT_EQ(654321, ab2.numbers(1).value());
    
    AddressStringMap ab3;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab3, &error));
    ASSERT_EQ("baidu.com", ab3.addr());
    ASSERT_EQ("email", ab3.contacts(0).key());
    ASSERT_EQ("frank@baidu.com", ab3.contacts(0).value());
    ASSERT_EQ("office", ab3.contacts(1).key());
    ASSERT_EQ("Shanghai", ab3.contacts(1).value());

    AddressComplex ab4;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab4, &error)) << error;
    ASSERT_EQ("baidu.com", ab4.addr());
    ASSERT_EQ("John", ab4.friends(0).key());
    ASSERT_EQ("SJTU", ab4.friends(0).value(0).school());
    ASSERT_EQ(2007, ab4.friends(0).value(0).year());

    std::string old_json = "{\"addr\":\"baidu.com\","
                           "\"numbers\":[{\"key\":\"tel\",\"value\":123456},"
                           "             {\"key\":\"cell\",\"value\":654321}]}";
    ab2.Clear();
    ASSERT_TRUE(json2pb::JsonToProtoMessage(old_json, &ab2, &error)) << error;
    ASSERT_EQ("baidu.com", ab2.addr());
    ASSERT_EQ("tel", ab2.numbers(0).key());
    ASSERT_EQ(123456, ab2.numbers(0).value());
    ASSERT_EQ("cell", ab2.numbers(1).key());
    ASSERT_EQ(654321, ab2.numbers(1).value());
}

TEST_F(ProtobufJsonTest, json_to_pb_encode_decode) {
    std::string info3 = "{\"@Content_Test%@\":[{\"Distance_info_\":1,\
                         \"_ext%T_\":{\"Aa_ge(\":1666666666, \"databyte(std::string)\":\
zhujiashun's avatar
zhujiashun committed
175
                         \"d2VsY29tZQ==\", \"enum--type\":\"HOME\"},\"uid*\":\"welcome\"}], \
zhujiashun's avatar
zhujiashun committed
176 177 178 179 180 181 182 183 184 185 186 187
                         \"judge\":false, \"spur\":2, \"data:array\":[]}";  
    printf("----------test json to pb------------\n\n");
    std::string error; 
    JsonContextBodyEncDec data; 
    ASSERT_TRUE(json2pb::JsonToProtoMessage(info3, &data, &error));

    std::string info4;  
    std::string error1;
    ASSERT_TRUE(json2pb::ProtoMessageToJson(data, &info4, &error1));
#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"judge\":false,\"spur\":2.0,"
                 "\"@Content_Test%@\":[{\"uid*\":\"welcome\",\"Distance_info_\":1.0,"
zhujiashun's avatar
zhujiashun committed
188
                 "\"_ext%T_\":{\"Aa_ge(\":1666666666,\"databyte(std::string)\":\"d2VsY29tZQ==\","
zhujiashun's avatar
zhujiashun committed
189 190 191 192
                 "\"enum--type\":\"HOME\"}}]}", info4.data());
#else
    ASSERT_STREQ("{\"judge\":false,\"spur\":2,"
                 "\"@Content_Test%@\":[{\"uid*\":\"welcome\",\"Distance_info_\":1,"
zhujiashun's avatar
zhujiashun committed
193
                 "\"_ext%T_\":{\"Aa_ge(\":1666666666,\"databyte(std::string)\":\"d2VsY29tZQ==\","
zhujiashun's avatar
zhujiashun committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
                 "\"enum--type\":\"HOME\"}}]}", info4.data());
#endif
}

TEST_F(ProtobufJsonTest, json_to_pb_unicode_case) {
    AddressBook address_book;

    Person* person = address_book.add_person();
 
    person->set_id(100);
    
    char name[255 * 1024];
    for (int j = 0; j < 255; j++) {
        for (int i = 0; i < 1024; i++) {
            name[j*1024 + i] = i + 1;
        }
    }
    name[255 * 1024 - 1] = '\0';
    person->set_name(name);
    person->set_data(-240000000);
    person->set_data32(6);
    person->set_data64(-1820000000);
    person->set_datadouble(123.456);
    person->set_datadouble_scientific(1.23456789e+08);
    person->set_datafloat_scientific(1.23456789e+08);
    person->set_datafloat(8.6123);
    person->set_datau32(60);
    person->set_datau64(960);
    person->set_databool(0);
    person->set_databyte("welcome to china");
    person->set_datafix32(1);
    person->set_datafix64(666);
    person->set_datasfix32(120);
    person->set_datasfix64(-802);

    std::string info1;
    std::string error;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(*person, &text);

    printf("----------test pb to json------------\n\n");
    bool ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);
    ASSERT_TRUE(ret);
    AddressBook address_book_test;
    ret = json2pb::JsonToProtoMessage(info1, &address_book_test, &error);
    ASSERT_TRUE(ret);
    std::string info2;
    ret = json2pb::ProtoMessageToJson(address_book_test, &info2, &error);
    ASSERT_TRUE(ret);
    ASSERT_TRUE(!info1.compare(info2));
246 247
    butil::IOBuf buf;
    butil::IOBufAsZeroCopyOutputStream stream(&buf); 
zhujiashun's avatar
zhujiashun committed
248 249
    bool res = json2pb::ProtoMessageToJson(address_book, &stream, NULL);
    ASSERT_TRUE(res);
250
    butil::IOBufAsZeroCopyInputStream stream2(buf); 
zhujiashun's avatar
zhujiashun committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    AddressBook address_book_test3;
    ret = json2pb::JsonToProtoMessage(&stream2, &address_book_test3, &error);
    ASSERT_TRUE(ret);
    std::string info3;
    ret = json2pb::ProtoMessageToJson(address_book_test3, &info3, &error);
    ASSERT_TRUE(ret);
    ASSERT_TRUE(!info2.compare(info3));
}

TEST_F(ProtobufJsonTest, json_to_pb_edge_case) {
    std::string info3 = "{\"judge\":false, \"spur\":2.0e1}"; 
    
    std::string error; 
    JsonContextBody data;
    bool ret = json2pb::JsonToProtoMessage(info3, &data, &error);
    ASSERT_TRUE(ret);

    std::string info4;  
    std::string error1;
    ret = json2pb::ProtoMessageToJson(data, &info4, &error1);
    ASSERT_TRUE(ret);
    
    info3 = "{\"judge\":false, \"spur\":-2, \"data\":[], \"info\":[],\"content\":[]}"; 
    error.clear(); 

    JsonContextBody data1;
    ret = json2pb::JsonToProtoMessage(info3, &data1, &error);
    ASSERT_TRUE(ret);
   
    info4.clear();  
    error1.clear();
    ret = json2pb::ProtoMessageToJson(data1, &info4, &error1);
    ASSERT_TRUE(ret);

    info3 = "{\"judge\":false, \"spur\":\"NaN\"}"; 
    error.clear(); 

    JsonContextBody data2;
    ret = json2pb::JsonToProtoMessage(info3, &data2, &error);
    ASSERT_TRUE(ret);
   
    info4.clear();  
    error1.clear();
    ret = json2pb::ProtoMessageToJson(data2, &info4, &error1);
    ASSERT_TRUE(ret);

    info3 = "{\"judge\":false, \"spur\":\"Infinity\"}"; 
    error.clear(); 

    JsonContextBody data3;
    ret = json2pb::JsonToProtoMessage(info3, &data3, &error);
    ASSERT_TRUE(ret);
   
    info4.clear();  
    error1.clear();
    ret = json2pb::ProtoMessageToJson(data3, &info4, &error1);
    ASSERT_TRUE(ret);

    info3 = "{\"judge\":false, \"spur\":\"-inFiNITY\"}"; 
    error.clear(); 

    JsonContextBody data4;
    ret = json2pb::JsonToProtoMessage(info3, &data4, &error);
    ASSERT_TRUE(ret);
   
    info4.clear();  
    error1.clear();
    ret = json2pb::ProtoMessageToJson(data4, &info4, &error1);
    ASSERT_TRUE(ret);

    info3 = "{\"judge\":false, \"spur\":2.0, \"content\":[{\"distance\":2.5, "
zhujiashun's avatar
zhujiashun committed
322
            "\"ext\":{\"databyte\":\"d2VsY29tZQ==\", \"enumtype\":\"MOBILE\"}}]}"; 
zhujiashun's avatar
zhujiashun committed
323 324 325 326 327 328 329 330 331 332 333 334
    error.clear(); 

    JsonContextBody data5;
    ret = json2pb::JsonToProtoMessage(info3, &data5, &error);
    ASSERT_TRUE(ret);
   
    info4.clear();  
    error1.clear();
    ret = json2pb::ProtoMessageToJson(data5, &info4, &error1);
    ASSERT_TRUE(ret);
    
    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
zhujiashun's avatar
zhujiashun committed
335
              \"ext\":{\"age\":1666666666, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":1},\
zhujiashun's avatar
zhujiashun committed
336
              \"uid\":\"someone\"},{\"distance\":2.3,\"unknown_member\":20,\
zhujiashun's avatar
zhujiashun committed
337
              \"ext\":{\"age\":1666666660, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":\"Test\"},\
zhujiashun's avatar
zhujiashun committed
338 339 340 341 342 343 344 345 346 347
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    error.clear(); 

    JsonContextBody data9;
    ret = json2pb::JsonToProtoMessage(info3, &data9, &error);
    ASSERT_TRUE(ret); 
    ASSERT_STREQ("Invalid value `\"Test\"' for optional field `Ext.enumtype' which SHOULD be enum", error.data());
    
    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
zhujiashun's avatar
zhujiashun committed
348
              \"ext\":{\"age\":1666666666, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":1},\
zhujiashun's avatar
zhujiashun committed
349
              \"uid\":\"someone\"},{\"distance\":5,\"unknown_member\":20,\
zhujiashun's avatar
zhujiashun committed
350
              \"ext\":{\"age\":1666666660, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":15},\
zhujiashun's avatar
zhujiashun committed
351 352 353 354 355 356 357 358 359 360
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    error.clear(); 

    JsonContextBody data10;
    ret = json2pb::JsonToProtoMessage(info3, &data10, &error);
    ASSERT_TRUE(ret); 
    ASSERT_STREQ("Invalid value `15' for optional field `Ext.enumtype' which SHOULD be enum", error.data());

    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
zhujiashun's avatar
zhujiashun committed
361
              \"ext\":{\"age\":1666666666, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":1},\
zhujiashun's avatar
zhujiashun committed
362
              \"uid\":\"someone\"},{\"distance\":5,\"unknown_member\":20,\
zhujiashun's avatar
zhujiashun committed
363
              \"ext\":{\"age\":1666666660, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":15},\
zhujiashun's avatar
zhujiashun committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"type\":[\"123\"]}";  
    error.clear(); 

    JsonContextBody data11;
    ret = json2pb::JsonToProtoMessage(info3, &data11, &error);
    ASSERT_TRUE(ret);   
    ASSERT_STREQ("Invalid value `array' for optional field `JsonContextBody.type' which SHOULD be INT64, Invalid value `15' for optional field `Ext.enumtype' which SHOULD be enum", 
                 error.data());
}

TEST_F(ProtobufJsonTest, json_to_pb_expected_failed_case) {
    std::string info3 = "{\"content\":[{\"unknown_member\":2,\"ext\":{\"age\":1666666666, \
                          \"databyte\":\"welcome\", \"enumtype\":1},\"uid\":\"someone\"},\
                          {\"unknown_member\":20,\"ext\":{\"age\":1666666660, \"databyte\":\
                          \"welcome0\", \"enumtype\":2},\"uid\":\"someone0\"}], \
                          \"judge\":false, \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    std::string error; 

    JsonContextBody data;
    bool ret = json2pb::JsonToProtoMessage(info3, &data, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Missing required field: Content.distance", error.data());
    
    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\"ext\":{\"age\":1666666666, \
              \"enumtype\":1},\"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,\
              \"ext\":{\"age\":1666666660, \"databyte\":\"welcome0\", \"enumtype\":2},\
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    error.clear(); 
  
    JsonContextBody data2;
    ret = json2pb::JsonToProtoMessage(info3, &data2, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Missing required field: Ext.databyte", error.data());

    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
              \"ext\":{\"age\":1666666666, \"databyte\":\"welcome\", \"enumtype\":1},\
              \"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,\
              \"ext\":{\"age\":1666666660, \"databyte\":\"welcome0\", \"enumtype\":2},\
              \"uid\":\"someone0\"}], \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    error.clear(); 

    JsonContextBody data3;
    ret = json2pb::JsonToProtoMessage(info3, &data, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Missing required field: JsonContextBody.judge", error.data());

    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
              \"ext\":{\"age\":1666666666, \"databyte\":\"welcome\", \"enumtype\":1},\
              \"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,\
              \"ext\":{\"age\":1666666660, \"databyte\":\"welcome0\", \"enumtype\":2},\
              \"uid\":\"someone0\"}], \"judge\":\"false\", \
              \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    error.clear(); 
  
    JsonContextBody data4;
    ret = json2pb::JsonToProtoMessage(info3, &data4, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Invalid value `\"false\"' for field `JsonContextBody.judge' which SHOULD be BOOL", error.data());

    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
              \"ext\":{\"age\":1666666666, \"databyte\":\"welcome\", \"enumtype\":1},\
              \"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,\
              \"ext\":{\"age\":1666666660, \"databyte\":\"welcome0\", \"enumtype\":2},\
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"data\":[\"1\",\"2\",\"3\",\"4\"]}";  
    error.clear(); 

    JsonContextBody data5;
    ret = json2pb::JsonToProtoMessage(info3, &data5, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Invalid value `\"1\"' for field `JsonContextBody.data' which SHOULD be INT32", error.data());

    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\
              \"ext\":{\"age\":1666666666, \"databyte\":\"welcome\", \"enumtype\":1},\
              \"uid\":\"someone\"},{\"distance\":10,\"unknown_member\":20,\
              \"ext\":{\"age\":1666666660, \"databyte\":\"welcome0\", \"enumtype\":2},\
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10], \"info\":2}";  
    error.clear(); 

    JsonContextBody data6;
    ret = json2pb::JsonToProtoMessage(info3, &data6, &error);
    ASSERT_FALSE(ret); 
    ASSERT_STREQ("Invalid value for repeated field: JsonContextBody.info", error.data());
 
    info3 = "{\"judge\":false, \"spur\":\"NaNa\"}"; 
    error.clear(); 

    JsonContextBody data7;
    ret = json2pb::JsonToProtoMessage(info3, &data7, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Invalid value `\"NaNa\"' for field `JsonContextBody.spur' which SHOULD be d", 
                 error.data());

    info3 = "{\"judge\":false, \"spur\":\"Infinty\"}"; 
    error.clear(); 

    JsonContextBody data8;
    ret = json2pb::JsonToProtoMessage(info3, &data8, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Invalid value `\"Infinty\"' for field `JsonContextBody.spur' which SHOULD be d", 
                 error.data());
    
    info3 = "{\"content\":[{\"distance\":1,\"unknown_member\":2,\"ext\":{\"age\":1666666666, \
              \"enumtype\":1},\"uid\":23},{\"distance\":10,\"unknown_member\":20,\
              \"ext\":{\"age\":1666666660, \"databyte\":\"welcome0\", \"enumtype\":2},\
              \"uid\":\"someone0\"}], \"judge\":false, \
              \"spur\":2, \"data\":[1,2,3,4,5,6,7,8,9,10]}";  
    error.clear(); 
  
    JsonContextBody data9;
    ret = json2pb::JsonToProtoMessage(info3, &data9, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Invalid value `23' for optional field `Content.uid' which SHOULD be string, Missing required field: Ext.databyte", error.data());
}

TEST_F(ProtobufJsonTest, json_to_pb_perf_case) {
    
    std::string info3 = "{\"content\":[{\"distance\":1.0,\
zhujiashun's avatar
zhujiashun committed
485
                          \"ext\":{\"age\":1666666666, \"databyte\":\"d2VsY29tZQ==\", \"enumtype\":1},\
zhujiashun's avatar
zhujiashun committed
486 487 488 489 490 491 492
                          \"uid\":\"welcome\"}], \"judge\":false, \"spur\":2.0, \"data\":[]}";  

    printf("----------test json to pb performance------------\n\n");

    std::string error; 
  
    ProfilerStart("json_to_pb_perf.prof");
493
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    bool res;
    float avg_time1 = 0;
    float avg_time2 = 0;
    const int times = 100000;
    for (int i = 0; i < times; i++) { 
        JsonContextBody data;
        timer.start();
        res = json2pb::JsonToProtoMessage(info3, &data, &error);
        timer.stop();
        avg_time1 += timer.u_elapsed();
        ASSERT_TRUE(res);

        std::string info4;  
        std::string error1;
        timer.start();
        res = json2pb::ProtoMessageToJson(data, &info4, &error1);
        timer.stop();
        avg_time2 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time1 /= times;
    avg_time2 /= times;
    ProfilerStop();
    printf("avg time to convert json to pb is %fus\n", avg_time1);
    printf("avg time to convert pb to json is %fus\n", avg_time2);
}

TEST_F(ProtobufJsonTest, json_to_pb_encode_decode_perf_case) {
    std::string info3 = "{\"@Content_Test%@\":[{\"Distance_info_\":1,\
                          \"_ext%T_\":{\"Aa_ge(\":1666666666, \"databyte(std::string)\":\
                          \"welcome\", \"enum--type\":1},\"uid*\":\"welcome\"}], \
                          \"judge\":false, \"spur\":2, \"data:array\":[]}";  
    printf("----------test json to pb encode/decode performance------------\n\n");
    std::string error; 
    
    ProfilerStart("json_to_pb_encode_decode_perf.prof");
530
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    bool res;
    float avg_time1 = 0;
    float avg_time2 = 0;
    const int times = 100000;
    for (int i = 0; i < times; i++) { 
        JsonContextBody data; 
        timer.start();
        res = json2pb::JsonToProtoMessage(info3, &data, &error);
        timer.stop();
        avg_time1 += timer.u_elapsed();
        ASSERT_TRUE(res);

        std::string info4;  
        std::string error1;
        timer.start();
        res = json2pb::ProtoMessageToJson(data, &info4, &error1);
        timer.stop();
        avg_time2 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time1 /= times;
    avg_time2 /= times;
    ProfilerStop();
    printf("avg time to convert json to pb is %fus\n", avg_time1);
    printf("avg time to convert pb to json is %fus\n", avg_time2);
}

TEST_F(ProtobufJsonTest, json_to_pb_complex_perf_case) {
    std::ifstream in("jsonout", std::ios::in);
    std::ostringstream tmp;
    tmp << in.rdbuf();
562
    butil::IOBuf buf;
zhujiashun's avatar
zhujiashun committed
563 564 565 566 567 568 569
    buf.append(tmp.str());
    in.close();

    printf("----------test json to pb performance------------\n\n");

    std::string error; 
  
570
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
571 572 573 574

    bool res;
    float avg_time1 = 0;
    const int times = 10000;
zhujiashun's avatar
zhujiashun committed
575 576
    json2pb::Json2PbOptions options;
    options.base64_to_bytes = false;
zhujiashun's avatar
zhujiashun committed
577 578 579
    ProfilerStart("json_to_pb_complex_perf.prof");
    for (int i = 0; i < times; i++) { 
        gss::message::gss_us_res_t data;
580
        butil::IOBufAsZeroCopyInputStream stream(buf); 
zhujiashun's avatar
zhujiashun committed
581
        timer.start();
zhujiashun's avatar
zhujiashun committed
582
        res = json2pb::JsonToProtoMessage(&stream, &data, options, &error);
zhujiashun's avatar
zhujiashun committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
        timer.stop();
        avg_time1 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    ProfilerStop();
    avg_time1 /= times;
    printf("avg time to convert json to pb is %fus\n", avg_time1);
}

TEST_F(ProtobufJsonTest, json_to_pb_to_string_complex_perf_case) {
    std::ifstream in("jsonout", std::ios::in);
    std::ostringstream tmp;
    tmp << in.rdbuf();
    std::string info3 = tmp.str();
    in.close();

    printf("----------test json to pb performance------------\n\n");

    std::string error; 
  
603
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
604 605 606
    bool res;
    float avg_time1 = 0;
    const int times = 10000;
zhujiashun's avatar
zhujiashun committed
607 608
    json2pb::Json2PbOptions options;
    options.base64_to_bytes = false;
zhujiashun's avatar
zhujiashun committed
609 610 611 612
    ProfilerStart("json_to_pb_to_string_complex_perf.prof");
    for (int i = 0; i < times; i++) { 
        gss::message::gss_us_res_t data;
        timer.start();
zhujiashun's avatar
zhujiashun committed
613
        res = json2pb::JsonToProtoMessage(info3, &data, options, &error);
zhujiashun's avatar
zhujiashun committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
        timer.stop();
        avg_time1 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time1 /= times;
    ProfilerStop();
    printf("avg time to convert json to pb is %fus\n", avg_time1);
}

TEST_F(ProtobufJsonTest, pb_to_json_normal_case) {
    AddressBook address_book;

    Person* person = address_book.add_person();
 
    person->set_id(100);
    person->set_name("baidu");
    person->set_email("welcome@baidu.com");  

    Person::PhoneNumber* phone_number = person->add_phone();
    phone_number->set_number("number123");
    phone_number->set_type(Person::HOME);
 
    person->set_data(-240000000);
    person->set_data32(6);
    person->set_data64(-1820000000);
    person->set_datadouble(123.456);
    person->set_datadouble_scientific(1.23456789e+08);
    person->set_datafloat_scientific(1.23456789e+08);
    person->set_datafloat(8.6123);
    person->set_datau32(60);
    person->set_datau64(960);
    person->set_databool(0);
zhujiashun's avatar
zhujiashun committed
646
    person->set_databyte("welcome");
zhujiashun's avatar
zhujiashun committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660
    person->set_datafix32(1);
    person->set_datafix64(666);
    person->set_datasfix32(120);
    person->set_datasfix64(-802);

    std::string info1;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(*person, &text);

    printf("text:%s\n", text.data());

    printf("----------test pb to json------------\n\n");
zhujiashun's avatar
zhujiashun committed
661 662 663
    json2pb::Pb2JsonOptions option;
    option.bytes_to_base64 = true;
    bool ret = json2pb::ProtoMessageToJson(address_book, &info1, option, NULL);
zhujiashun's avatar
zhujiashun committed
664 665 666 667 668 669 670
    ASSERT_TRUE(ret);

#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
zhujiashun's avatar
zhujiashun committed
671
                 "\"databool\":false,\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,"
zhujiashun's avatar
zhujiashun committed
672 673 674 675 676 677 678 679
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,\"databool\":false,"
zhujiashun's avatar
zhujiashun committed
680
                 "\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,\"datafix64\":666,"
zhujiashun's avatar
zhujiashun committed
681 682 683 684 685
                 "\"datasfix32\":120,\"datasfix64\":-802,\"datafloat_scientific\":123456792,"
                 "\"datadouble_scientific\":123456789}]}", info1.data());
#endif

    info1.clear();
zhujiashun's avatar
zhujiashun committed
686 687 688 689 690
    {
        json2pb::Pb2JsonOptions option;
        option.bytes_to_base64 = true;
        ret = ProtoMessageToJson(address_book, &info1, option, NULL);
    }
zhujiashun's avatar
zhujiashun committed
691 692 693 694 695 696 697
    ASSERT_TRUE(ret);

#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
zhujiashun's avatar
zhujiashun committed
698
                 "\"databool\":false,\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,"
zhujiashun's avatar
zhujiashun committed
699 700 701 702 703 704 705 706
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,\"databool\":false,"
zhujiashun's avatar
zhujiashun committed
707
                 "\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,\"datafix64\":666,"
zhujiashun's avatar
zhujiashun committed
708 709 710 711 712
                 "\"datasfix32\":120,\"datasfix64\":-802,\"datafloat_scientific\":123456792,"
                 "\"datadouble_scientific\":123456789}]}", info1.data());
#endif
    
    info1.clear();
zhujiashun's avatar
zhujiashun committed
713 714 715 716 717 718
    {
        json2pb::Pb2JsonOptions option;
        option.bytes_to_base64 = true;
        option.enum_option = json2pb::OUTPUT_ENUM_BY_NUMBER;
        ret = ProtoMessageToJson(address_book, &info1, option, NULL);
    }
zhujiashun's avatar
zhujiashun committed
719 720 721 722 723 724 725
    ASSERT_TRUE(ret);

#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":1}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
zhujiashun's avatar
zhujiashun committed
726
                 "\"databool\":false,\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,"
zhujiashun's avatar
zhujiashun committed
727 728 729 730 731 732 733 734
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":1}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,\"databool\":false,"
zhujiashun's avatar
zhujiashun committed
735
                 "\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,\"datafix64\":666,"
zhujiashun's avatar
zhujiashun committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
                 "\"datasfix32\":120,\"datasfix64\":-802,\"datafloat_scientific\":123456792,"
                 "\"datadouble_scientific\":123456789}]}", info1.data());
#endif

    printf("----------test json to pb------------\n\n");

    const int N = 1000;
    int64_t total_tm = 0;
    int64_t total_tm2 = 0;
    for (int i = 0; i < N; ++i) {

        std::string info3;
        AddressBook data1;
        std::string error1;
        const int64_t tm1 = gettimeofday_us();
        bool ret1 = json2pb::JsonToProtoMessage(info1, &data1, &error1); 
        const int64_t tm2 = gettimeofday_us();
        total_tm += tm2 - tm1;
        ASSERT_TRUE(ret1);
        
        std::string error2;
        const int64_t tm3 = gettimeofday_us();
        ret1 = json2pb::ProtoMessageToJson(data1, &info3, &error2);
        const int64_t tm4 = gettimeofday_us();
        ASSERT_TRUE(ret1);
        total_tm2 += tm4 - tm3;
#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
zhujiashun's avatar
zhujiashun committed
767
                 "\"databool\":false,\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,"
zhujiashun's avatar
zhujiashun committed
768 769 770 771 772 773 774 775
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info3.data());
#else
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu\",\"id\":100,\"email\":\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,\"databool\":false,"
zhujiashun's avatar
zhujiashun committed
776
                 "\"databyte\":\"d2VsY29tZQ==\",\"datafix32\":1,\"datafix64\":666,"
zhujiashun's avatar
zhujiashun committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
                 "\"datasfix32\":120,\"datasfix64\":-802,\"datafloat_scientific\":123456792,"
                 "\"datadouble_scientific\":123456789}]}", info3.data());
#endif
}
    std::cout << "json2pb=" << total_tm / N
              << "us pb2json=" << total_tm2 / N << "us"
              << std::endl;
}

TEST_F(ProtobufJsonTest, pb_to_json_map_case) {
    std::string json = "{\"addr\":\"baidu.com\","
                       "\"numbers\":{\"tel\":123456,\"cell\":654321},"
                       "\"contacts\":{\"email\":\"frank@baidu.com\","
                       "               \"office\":\"Shanghai\"},"
                       "\"friends\":{\"John\":[{\"school\":\"SJTU\",\"year\":2007}]}}";
    std::string output;
    std::string error;
    AddressNoMap ab1;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab1, &error));
    ASSERT_TRUE(json2pb::ProtoMessageToJson(ab1, &output, &error));
    ASSERT_TRUE(output.find("\"addr\":\"baidu.com\"") != std::string::npos);

    output.clear();
    AddressIntMap ab2;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab2, &error));
    ASSERT_TRUE(json2pb::ProtoMessageToJson(ab2, &output, &error));
    ASSERT_TRUE(output.find("\"addr\":\"baidu.com\"") != std::string::npos);
    ASSERT_TRUE(output.find("\"tel\":123456") != std::string::npos);
    ASSERT_TRUE(output.find("\"cell\":654321") != std::string::npos);
    
    output.clear();
    AddressStringMap ab3;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab3, &error));
    ASSERT_TRUE(json2pb::ProtoMessageToJson(ab3, &output, &error));
    ASSERT_TRUE(output.find("\"addr\":\"baidu.com\"") != std::string::npos);
    ASSERT_TRUE(output.find("\"email\":\"frank@baidu.com\"") != std::string::npos);
    ASSERT_TRUE(output.find("\"office\":\"Shanghai\"") != std::string::npos);

    output.clear();
    AddressComplex ab4;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &ab4, &error));
    ASSERT_TRUE(json2pb::ProtoMessageToJson(ab4, &output, &error));
    ASSERT_TRUE(output.find("\"addr\":\"baidu.com\"") != std::string::npos);
    ASSERT_TRUE(output.find("\"friends\":{\"John\":[{\"school\":\"SJTU\","
                            "\"year\":2007}]}") != std::string::npos);
}

TEST_F(ProtobufJsonTest, pb_to_json_encode_decode) {
    JsonContextBodyEncDec json_data;
    json_data.set_type(80000);
    json_data.add_data_z058_array(200);
    json_data.add_data_z058_array(300);
    json_data.add_info("this is json data's info");
    json_data.add_info("this is a test");
    json_data.set_judge(true);
    json_data.set_spur(3.45);
    
    ContentEncDec * content = json_data.add__z064_content_test_z037__z064_();
    content->set_uid_z042_("content info");
    content->set_distance_info_(1234.56);
    
    ExtEncDec* ext = content->mutable__ext_z037_t_(); 
    ext->set_aa_ge_z040_(160000);
    ext->set_databyte_z040_std_z058__z058_string_z041_("databyte"); 
    ext->set_enum_z045__z045_type(ExtEncDec_PhoneTypeEncDec_WORK);

    std::string info1;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(json_data, &text);

    printf("text:%s\n", text.data());

    printf("----------test pb to json------------\n\n");
zhujiashun's avatar
zhujiashun committed
852 853 854
    json2pb::Pb2JsonOptions option;
    option.bytes_to_base64 = true;
    ASSERT_TRUE(ProtoMessageToJson(json_data, &info1, option, NULL));
zhujiashun's avatar
zhujiashun committed
855 856 857 858
#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
                 "\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
                 "[{\"uid*\":\"content info\",\"Distance_info_\":1234.56005859375,\"_ext%T_\":"
zhujiashun's avatar
zhujiashun committed
859
                 "{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
zhujiashun's avatar
zhujiashun committed
860 861 862 863 864 865
                 "\"enum--type\":\"WORK\"}}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
                 "\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
                 "[{\"uid*\":\"content info\",\"Distance_info_\":1234.560059,\"_ext%T_\":"
zhujiashun's avatar
zhujiashun committed
866
                 "{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
zhujiashun's avatar
zhujiashun committed
867 868 869 870 871 872 873 874 875 876 877 878 879
                 "\"enum--type\":\"WORK\"}}]}", 
                 info1.data());
#endif
    printf("----------test json to pb------------\n\n");
    
    std::string info3;
    JsonContextBody data1;
    json2pb::JsonToProtoMessage(info1, &data1, NULL); 
    json2pb::ProtoMessageToJson(data1, &info3, NULL);
#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
                 "\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
                 "[{\"uid*\":\"content info\",\"Distance_info_\":1234.56005859375,\"_ext%T_\":"
zhujiashun's avatar
zhujiashun committed
880
                 "{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
zhujiashun's avatar
zhujiashun committed
881 882 883 884 885 886
                 "\"enum--type\":\"WORK\"}}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"info\":[\"this is json data's info\",\"this is a test\"],\"type\":80000,"
                 "\"data:array\":[200,300],\"judge\":true,\"spur\":3.45,\"@Content_Test%@\":"
                 "[{\"uid*\":\"content info\",\"Distance_info_\":1234.560059,\"_ext%T_\":"
zhujiashun's avatar
zhujiashun committed
887
                 "{\"Aa_ge(\":160000,\"databyte(std::string)\":\"ZGF0YWJ5dGU=\","
zhujiashun's avatar
zhujiashun committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
                 "\"enum--type\":\"WORK\"}}]}", 
                 info1.data());
#endif
}

TEST_F(ProtobufJsonTest, pb_to_json_control_char_case) {
    AddressBook address_book;

    Person* person = address_book.add_person();
 
    person->set_id(100);
    char ch = 0x01;
    char* name = new char[17];
    memcpy(name, "baidu ", 6);
    name[6] = ch;
    char c = 0x08;
    char t = 0x1A;
    memcpy(name + 7, "test", 4);
    name[11] = c;
    name[12] = t;
    memcpy(name + 13, "end", 3);
    name[16] = '\0';
    person->set_name(name);
    printf("name is %s\n", name);
    person->set_email("welcome@baidu.com");  

    Person::PhoneNumber* phone_number = person->add_phone();
    phone_number->set_number("number123");
    phone_number->set_type(Person::HOME);
 
    person->set_data(-240000000);
    person->set_data32(6);
    person->set_data64(-1820000000);
    person->set_datadouble(123.456);
    person->set_datadouble_scientific(1.23456789e+08);
    person->set_datafloat_scientific(1.23456789e+08);
    person->set_datafloat(8.6123);
    person->set_datau32(60);
    person->set_datau64(960);
    person->set_databool(0);
    person->set_databyte("welcome to china");
    person->set_datafix32(1);
    person->set_datafix64(666);
    person->set_datasfix32(120);
    person->set_datasfix64(-802);

    std::string info1;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(*person, &text);

    printf("text:%s\n", text.data());

zhujiashun's avatar
zhujiashun committed
942
    bool ret = false;
zhujiashun's avatar
zhujiashun committed
943
    printf("----------test pb to json------------\n\n");
zhujiashun's avatar
zhujiashun committed
944 945 946 947 948 949
    {
        json2pb::Pb2JsonOptions option;
        option.bytes_to_base64 = false;
        ret = ProtoMessageToJson(address_book, &info1, option, NULL);
        ASSERT_TRUE(ret);
    }
zhujiashun's avatar
zhujiashun committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu \\u0001test\\b\\u001Aend\",\"id\":100,\"email\":"
                 "\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
                 "\"databool\":false,\"databyte\":\"welcome to china\",\"datafix32\":1,"
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu \\u0001test\\b\\u001Aend\",\"id\":100,\"email\":"
                 "\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,\"databool\":false,"
                 "\"databyte\":\"welcome to china\",\"datafix32\":1,\"datafix64\":666,"
                 "\"datasfix32\":120,\"datasfix64\":-802,\"datafloat_scientific\":123456792,"
                 "\"datadouble_scientific\":123456789}]}", info1.data());
#endif

    info1.clear();
zhujiashun's avatar
zhujiashun committed
973 974 975 976 977 978
    {
        json2pb::Pb2JsonOptions option;
        option.bytes_to_base64 = true;
        ret = ProtoMessageToJson(address_book, &info1, option, NULL);
        ASSERT_TRUE(ret);
    }
zhujiashun's avatar
zhujiashun committed
979 980 981 982 983 984 985

#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu \\u0001test\\b\\u001Aend\",\"id\":100,\"email\":"
                 "\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
zhujiashun's avatar
zhujiashun committed
986
                 "\"databool\":false,\"databyte\":\"d2VsY29tZSB0byBjaGluYQ==\",\"datafix32\":1,"
zhujiashun's avatar
zhujiashun committed
987 988 989 990 991 992 993 994 995
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info1.data());
#else
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu \\u0001test\\b\\u001Aend\",\"id\":100,\"email\":"
                 "\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":\"HOME\"}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,\"databool\":false,"
zhujiashun's avatar
zhujiashun committed
996
                 "\"databyte\":\"d2VsY29tZSB0byBjaGluYQ==\",\"datafix32\":1,\"datafix64\":666,"
zhujiashun's avatar
zhujiashun committed
997 998 999 1000 1001
                 "\"datasfix32\":120,\"datasfix64\":-802,\"datafloat_scientific\":123456792,"
                 "\"datadouble_scientific\":123456789}]}", info1.data());
#endif
    
    info1.clear();
zhujiashun's avatar
zhujiashun committed
1002 1003 1004 1005 1006 1007 1008
    {
        json2pb::Pb2JsonOptions option;
        option.enum_option = json2pb::OUTPUT_ENUM_BY_NUMBER;
        option.bytes_to_base64 = false;
        ret = ProtoMessageToJson(address_book, &info1, option, NULL);
        ASSERT_TRUE(ret);
    }
zhujiashun's avatar
zhujiashun committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

#ifndef RAPIDJSON_VERSION_0_1
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu \\u0001test\\b\\u001Aend\",\"id\":100,\"email\":"
                 "\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":1}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919128418,\"datau32\":60,\"datau64\":960,"
                 "\"databool\":false,\"databyte\":\"welcome to china\",\"datafix32\":1,"
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792.0,\"datadouble_scientific\":123456789.0}]}", 
                 info1.data());
#else
    std::cout << info1.data() << std::endl;
    ASSERT_STREQ("{\"person\":[{\"name\":\"baidu \\u0001test\\b\\u001Aend\",\"id\":100,\"email\":"
                 "\"welcome@baidu.com\","
                 "\"phone\":[{\"number\":\"number123\",\"type\":1}],\"data\":-240000000,"
                 "\"data32\":6,\"data64\":-1820000000,\"datadouble\":123.456,"
                 "\"datafloat\":8.612299919,\"datau32\":60,\"datau64\":960,"
                 "\"databool\":false,\"databyte\":\"welcome to china\",\"datafix32\":1,"
                 "\"datafix64\":666,\"datasfix32\":120,\"datasfix64\":-802,"
                 "\"datafloat_scientific\":123456792,\"datadouble_scientific\":123456789}]}", 
                 info1.data());
#endif
}

TEST_F(ProtobufJsonTest, pb_to_json_unicode_case) {
    AddressBook address_book;

    Person* person = address_book.add_person();
 
    person->set_id(100);
    
    char name[255*1024];
    for (int j = 0; j < 1024; j++) {
        for (int i = 0; i < 255; i++) {
            name[j*255 + i] = i + 1;
        }
    }
    name[255*1024 - 1] = '\0';
    person->set_name(name);
    person->set_data(-240000000);
    person->set_data32(6);
    person->set_data64(-1820000000);
    person->set_datadouble(123.456);
    person->set_datadouble_scientific(1.23456789e+08);
    person->set_datafloat_scientific(1.23456789e+08);
    person->set_datafloat(8.6123);
    person->set_datau32(60);
    person->set_datau64(960);
    person->set_databool(0);
    person->set_databyte("welcome to china");
    person->set_datafix32(1);
    person->set_datafix64(666);
    person->set_datasfix32(120);
    person->set_datasfix64(-802);

    std::string info1;
    std::string error;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(*person, &text);

    printf("----------test pb to json------------\n\n");
    bool ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);
    ASSERT_TRUE(ret);
1075 1076
    butil::IOBuf buf;
    butil::IOBufAsZeroCopyOutputStream stream(&buf); 
zhujiashun's avatar
zhujiashun committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    bool res = json2pb::ProtoMessageToJson(address_book, &stream, NULL);
    ASSERT_TRUE(res);
    ASSERT_TRUE(!info1.compare(buf.to_string()));
}
                 
TEST_F(ProtobufJsonTest, pb_to_json_edge_case) {
    AddressBook address_book;
    
    std::string info1;
    std::string error;
    bool ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);
    ASSERT_TRUE(ret);
   
    info1.clear();
    Person* person = address_book.add_person();
      
    person->set_id(100);
    person->set_name("baidu");

    Person::PhoneNumber* phone_number = person->add_phone();
    phone_number->set_number("1234556");

    person->set_datadouble(-345.67);
    person->set_datafloat(8.6123);

    ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);

    ASSERT_TRUE(ret);
    ASSERT_TRUE(error.empty());

    std::string info3;
    AddressBook data1;
    std::string error1;
    bool ret1 = json2pb::JsonToProtoMessage(info1, &data1, &error1); 
    ASSERT_TRUE(ret1);
    ASSERT_TRUE(error1.empty());

    std::string error2;
    bool ret2 = json2pb::ProtoMessageToJson(data1, &info3, &error2);
    ASSERT_TRUE(ret2);
    ASSERT_TRUE(error2.empty());
}

TEST_F(ProtobufJsonTest, pb_to_json_expected_failed_case) {
    AddressBook address_book;
    std::string info1; 
    std::string error;

    Person* person = address_book.add_person();
 
    person->set_name("baidu");
    person->set_email("welcome@baidu.com");  
    
    bool ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Missing required field: addressbook.Person.id", error.data()); 
     
    address_book.clear_person();
    person = address_book.add_person();

    person->set_id(2);
    person->set_email("welcome@baidu.com");  
    
    ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Missing required field: addressbook.Person.name", error.data()); 

    address_book.clear_person();
    person = address_book.add_person();

    person->set_id(2);
    person->set_name("name");
    person->set_email("welcome@baidu.com");  
    
    ret = json2pb::ProtoMessageToJson(address_book, &info1, &error);
    ASSERT_FALSE(ret);
    ASSERT_STREQ("Missing required field: addressbook.Person.datadouble", error.data()); 
}

TEST_F(ProtobufJsonTest, pb_to_json_perf_case) {
    AddressBook address_book;

    Person* person = address_book.add_person();
 
    person->set_id(100);
    person->set_name("baidu");
    person->set_email("welcome@baidu.com");  

    Person::PhoneNumber* phone_number = person->add_phone();
    phone_number->set_number("number123");
    phone_number->set_type(Person::HOME);
 
    person->set_data(-240000000);

    person->set_data32(6);

    person->set_data64(-1820000000);

    person->set_datadouble(123.456);

    person->set_datadouble_scientific(1.23456789e+08);

    person->set_datafloat_scientific(1.23456789e+08);

    person->set_datafloat(8.6123);

    person->set_datau32(60);

    person->set_datau64(960);

    person->set_databool(0);

    person->set_databyte("welcome to china");

    person->set_datafix32(1);

    person->set_datafix64(666);

    person->set_datasfix32(120);

    person->set_datasfix64(-802);

    std::string info1;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(*person, &text);

    printf("text:%s\n", text.data());

    printf("----------test pb to json performance------------\n\n");
    ProfilerStart("pb_to_json_perf.prof");
1209
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    bool res;
    float avg_time1 = 0;
    float avg_time2 = 0;
    const int times = 100000;
    ASSERT_TRUE(json2pb::ProtoMessageToJson(address_book, &info1, NULL));
    for (int i = 0; i < times; i++) { 
        std::string info3;
        AddressBook data1;
        timer.start();
        res = json2pb::JsonToProtoMessage(info1, &data1, NULL); 
        timer.stop();
        avg_time1 += timer.u_elapsed();
        ASSERT_TRUE(res);
        
        timer.start();
        res = json2pb::ProtoMessageToJson(data1, &info3, NULL);
        timer.stop();
        avg_time2 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time1 /= times;
    avg_time2 /= times;
    ProfilerStop();
    printf("avg time to convert json to pb is %fus\n", avg_time1);
    printf("avg time to convert pb to json is %fus\n", avg_time2);
}

TEST_F(ProtobufJsonTest, pb_to_json_encode_decode_perf_case) {
    JsonContextBodyEncDec json_data;
    json_data.set_type(80000);
    json_data.add_data_z058_array(200);
    json_data.add_data_z058_array(300);
    json_data.add_info("this is json data's info");
    json_data.add_info("this is a test");
    json_data.set_judge(true);
    json_data.set_spur(3.45);
    
    ContentEncDec * content = json_data.add__z064_content_test_z037__z064_();
    content->set_uid_z042_("content info");
    content->set_distance_info_(1234.56);
    
    ExtEncDec* ext = content->mutable__ext_z037_t_(); 
    ext->set_aa_ge_z040_(160000);
    ext->set_databyte_z040_std_z058__z058_string_z041_("databyte"); 
    ext->set_enum_z045__z045_type(ExtEncDec_PhoneTypeEncDec_WORK);

    std::string info1;

    google::protobuf::TextFormat::Printer printer;
    std::string text;
    printer.PrintToString(json_data, &text);

    printf("text:%s\n", text.data());
    
    ASSERT_TRUE(json2pb::ProtoMessageToJson(json_data, &info1, NULL));

    printf("----------test pb to json encode decode performance------------\n\n");
    ProfilerStart("pb_to_json_encode_decode_perf.prof");
1268
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    bool res;
    float avg_time1 = 0;
    float avg_time2 = 0;
    const int times = 100000;
    for (int i = 0; i < times; i++) { 
        std::string info3;
        JsonContextBody json_body;
        timer.start();
        res = json2pb::JsonToProtoMessage(info1, &json_body, NULL); 
        timer.stop();
        avg_time1 += timer.u_elapsed();
        ASSERT_TRUE(res);
        
        timer.start();
        res = json2pb::ProtoMessageToJson(json_body, &info3, NULL);
        timer.stop();
        avg_time2 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time1 /= times;
    avg_time2 /= times;
    ProfilerStop();
    printf("avg time to convert json to pb is %fus\n", avg_time1);
    printf("avg time to convert pb to json is %fus\n", avg_time2);
}

TEST_F(ProtobufJsonTest, pb_to_json_complex_perf_case) {
    
    std::ifstream in("jsonout", std::ios::in);
    std::ostringstream tmp;
    tmp << in.rdbuf();
    std::string info3 = tmp.str();
    in.close();

    printf("----------test pb to json performance------------\n\n");

    std::string error; 
  
1307
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
1308 1309 1310 1311 1312
    bool res;
    float avg_time1 = 0;
    float avg_time2 = 0;
    const int times = 10000;
    gss::message::gss_us_res_t data;
zhujiashun's avatar
zhujiashun committed
1313 1314
    json2pb::Json2PbOptions option;
    option.base64_to_bytes = false;
zhujiashun's avatar
zhujiashun committed
1315
    timer.start();
zhujiashun's avatar
zhujiashun committed
1316
    res = JsonToProtoMessage(info3, &data, option, &error);
zhujiashun's avatar
zhujiashun committed
1317 1318 1319 1320 1321 1322 1323
    timer.stop();
    avg_time1 += timer.u_elapsed();
    ASSERT_TRUE(res);
    ProfilerStart("pb_to_json_complex_perf.prof");
    for (int i = 0; i < times; i++) { 
        std::string error1;
        timer.start();
1324 1325
        butil::IOBuf buf;
        butil::IOBufAsZeroCopyOutputStream stream(&buf); 
zhujiashun's avatar
zhujiashun committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
        res = json2pb::ProtoMessageToJson(data, &stream, &error1);
        timer.stop();
        avg_time2 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time2 /= times;
    ProfilerStop();
    printf("avg time to convert pb to json is %fus\n", avg_time2);
}

TEST_F(ProtobufJsonTest, pb_to_json_to_string_complex_perf_case) {
    std::ifstream in("jsonout", std::ios::in);
    std::ostringstream tmp;
    tmp << in.rdbuf();
    std::string info3 = tmp.str();
    in.close();

    printf("----------test pb to json performance------------\n\n");

    std::string error; 
  
1347
    butil::Timer timer;
zhujiashun's avatar
zhujiashun committed
1348 1349 1350 1351 1352
    bool res;
    float avg_time1 = 0;
    float avg_time2 = 0;
    const int times = 10000;
    gss::message::gss_us_res_t data;
zhujiashun's avatar
zhujiashun committed
1353 1354
    json2pb::Json2PbOptions option;
    option.base64_to_bytes = false;
zhujiashun's avatar
zhujiashun committed
1355
    timer.start();
zhujiashun's avatar
zhujiashun committed
1356
    res = JsonToProtoMessage(info3, &data, option, &error);
zhujiashun's avatar
zhujiashun committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    timer.stop();
    avg_time1 += timer.u_elapsed();
    ASSERT_TRUE(res);
    ProfilerStart("pb_to_json_to_string_complex_perf.prof");
    for (int i = 0; i < times; i++) { 
        std::string info4;  
        std::string error1;
        timer.start();
        std::string inf4;
        res = json2pb::ProtoMessageToJson(data, &info4, &error1);
        timer.stop();
        avg_time2 += timer.u_elapsed();
        ASSERT_TRUE(res);
    }
    avg_time2 /= times;
    ProfilerStop();
    printf("avg time to convert pb to json is %fus\n", avg_time2);
}

TEST_F(ProtobufJsonTest, encode_decode_case) {
      
    std::string json_key = "abcdek123lske_slkejfl_l1kdle";
    std::string field_name;
    ASSERT_FALSE(json2pb::encode_name(json_key, field_name));
    ASSERT_TRUE(field_name.empty());
    std::string json_key_decode; 
    ASSERT_FALSE(json2pb::decode_name(field_name, json_key_decode));
    ASSERT_TRUE(json_key_decode.empty());
    
    json_key = "_Afledk2e*_+%leGi___hE_Z278_t#";
    field_name.clear();
    json2pb::encode_name(json_key, field_name);
    const char* encode_json_key = "_Afledk2e_Z042___Z043__Z037_leGi___hE_Z278_t_Z035_";
    ASSERT_TRUE(strcmp(field_name.data(), encode_json_key) == 0);
    json_key_decode.clear();
    json2pb::decode_name(field_name, json_key_decode);
    ASSERT_TRUE(strcmp(json_key_decode.data(), json_key.data()) == 0);
    
    json_key = "_ext%T_";
    field_name.clear();
    json2pb::encode_name(json_key, field_name);
    encode_json_key = "_ext_Z037_T_";
    ASSERT_TRUE(strcmp(field_name.data(), encode_json_key) == 0);
    json_key_decode.clear();
    json2pb::decode_name(field_name, json_key_decode);
    ASSERT_TRUE(strcmp(json_key_decode.data(), json_key.data()) == 0);

    std::string empty_key;
    std::string empty_result;
    ASSERT_FALSE(json2pb::encode_name(empty_key, empty_result));
    ASSERT_TRUE(empty_result.empty() == true);
    ASSERT_FALSE(json2pb::decode_name(empty_result, empty_key));
    ASSERT_TRUE(empty_key.empty() == true);
}
                 
TEST_F(ProtobufJsonTest, json_to_zero_copy_stream_normal_case) {
    Person person;
    person.set_name("hello");
    person.set_id(9);
    person.set_datadouble(2.2);
    person.set_datafloat(1);
1418 1419
    butil::IOBuf iobuf;
    butil::IOBufAsZeroCopyOutputStream wrapper(&iobuf);
zhujiashun's avatar
zhujiashun committed
1420 1421 1422 1423 1424 1425 1426
    std::string error;
    ASSERT_TRUE(json2pb::ProtoMessageToJson(person, &wrapper, &error)) << error;
    std::string out = iobuf.to_string();
    ASSERT_EQ("{\"name\":\"hello\",\"id\":9,\"datadouble\":2.2,\"datafloat\":1.0}", out);
}

TEST_F(ProtobufJsonTest, zero_copy_stream_to_json_normal_case) {
1427
    butil::IOBuf iobuf;
zhujiashun's avatar
zhujiashun committed
1428
    iobuf = "{\"name\":\"hello\",\"id\":9,\"datadouble\":2.2,\"datafloat\":1.0}";
1429
    butil::IOBufAsZeroCopyInputStream wrapper(iobuf);
zhujiashun's avatar
zhujiashun committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    Person person;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(&wrapper, &person));
    ASSERT_STREQ("hello", person.name().c_str());
    ASSERT_EQ(9, person.id());
    ASSERT_EQ(2.2, person.datadouble());
    ASSERT_EQ(1, person.datafloat());
}

TEST_F(ProtobufJsonTest, extension_case) {
    std::string json = "{\"name\":\"hello\",\"id\":9,\"datadouble\":2.2,\"datafloat\":1.0,\"hobby\":\"coding\"}";
    Person person;
    ASSERT_TRUE(json2pb::JsonToProtoMessage(json, &person));
    ASSERT_STREQ("coding", person.GetExtension(addressbook::hobby).data());
    std::string output;
    ASSERT_TRUE(json2pb::ProtoMessageToJson(person, &output));
    ASSERT_EQ("{\"hobby\":\"coding\",\"name\":\"hello\",\"id\":9,\"datadouble\":2.2,\"datafloat\":1.0}", output);
}

}