├── CJsonObject.cpp ├── CJsonObject.hpp ├── README.md ├── cJSON.c ├── cJSON.h ├── requests.cpp ├── requests.h ├── utils.cpp └── utils.h /CJsonObject.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Project: neb 3 | * @file CJsonObject.cpp 4 | * @brief 5 | * @author bwarliao 6 | * @date: 2014-7-16 7 | * @note 8 | * Modify history: 9 | ******************************************************************************/ 10 | 11 | #include "CJsonObject.hpp" 12 | 13 | namespace requests 14 | { 15 | 16 | CJsonObject::CJsonObject() 17 | : m_pJsonData(NULL), m_pExternJsonDataRef(NULL), m_pKeyTravers(NULL) 18 | { 19 | // m_pJsonData = cJSON_CreateObject(); 20 | } 21 | 22 | CJsonObject::CJsonObject(const std::string& strJson) 23 | : m_pJsonData(NULL), m_pExternJsonDataRef(NULL), m_pKeyTravers(NULL) 24 | { 25 | Parse(strJson); 26 | } 27 | 28 | CJsonObject::CJsonObject(const CJsonObject* pJsonObject) 29 | : m_pJsonData(NULL), m_pExternJsonDataRef(NULL), m_pKeyTravers(NULL) 30 | { 31 | if (pJsonObject) 32 | { 33 | Parse(pJsonObject->ToString()); 34 | } 35 | } 36 | 37 | CJsonObject::CJsonObject(const CJsonObject& oJsonObject) 38 | : m_pJsonData(NULL), m_pExternJsonDataRef(NULL), m_pKeyTravers(NULL) 39 | { 40 | Parse(oJsonObject.ToString()); 41 | } 42 | 43 | CJsonObject::~CJsonObject() 44 | { 45 | Clear(); 46 | } 47 | 48 | CJsonObject& CJsonObject::operator=(const CJsonObject& oJsonObject) 49 | { 50 | Parse(oJsonObject.ToString().c_str()); 51 | return(*this); 52 | } 53 | 54 | bool CJsonObject::operator==(const CJsonObject& oJsonObject) const 55 | { 56 | return(this->ToString() == oJsonObject.ToString()); 57 | } 58 | 59 | bool CJsonObject::AddEmptySubObject(const std::string& strKey) 60 | { 61 | cJSON* pFocusData = NULL; 62 | if (m_pJsonData != NULL) 63 | { 64 | pFocusData = m_pJsonData; 65 | } 66 | else if (m_pExternJsonDataRef != NULL) 67 | { 68 | pFocusData = m_pExternJsonDataRef; 69 | } 70 | else 71 | { 72 | m_pJsonData = cJSON_CreateObject(); 73 | m_pKeyTravers = m_pJsonData; 74 | pFocusData = m_pJsonData; 75 | } 76 | 77 | if (pFocusData == NULL) 78 | { 79 | m_strErrMsg = "json data is null!"; 80 | return(false); 81 | } 82 | if (pFocusData->type != cJSON_Object) 83 | { 84 | m_strErrMsg = "not a json object! json array?"; 85 | return(false); 86 | } 87 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 88 | { 89 | m_strErrMsg = "key exists!"; 90 | return(false); 91 | } 92 | cJSON* pJsonStruct = cJSON_CreateObject(); 93 | if (pJsonStruct == NULL) 94 | { 95 | m_strErrMsg = std::string("create sub empty object error!"); 96 | return(false); 97 | } 98 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 99 | m_pKeyTravers = pFocusData; 100 | return(true); 101 | } 102 | 103 | bool CJsonObject::AddEmptySubArray(const std::string& strKey) 104 | { 105 | cJSON* pFocusData = NULL; 106 | if (m_pJsonData != NULL) 107 | { 108 | pFocusData = m_pJsonData; 109 | } 110 | else if (m_pExternJsonDataRef != NULL) 111 | { 112 | pFocusData = m_pExternJsonDataRef; 113 | } 114 | else 115 | { 116 | m_pJsonData = cJSON_CreateObject(); 117 | m_pKeyTravers = m_pJsonData; 118 | pFocusData = m_pJsonData; 119 | } 120 | 121 | if (pFocusData == NULL) 122 | { 123 | m_strErrMsg = "json data is null!"; 124 | return(false); 125 | } 126 | if (pFocusData->type != cJSON_Object) 127 | { 128 | m_strErrMsg = "not a json object! json array?"; 129 | return(false); 130 | } 131 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 132 | { 133 | m_strErrMsg = "key exists!"; 134 | return(false); 135 | } 136 | cJSON* pJsonStruct = cJSON_CreateArray(); 137 | if (pJsonStruct == NULL) 138 | { 139 | m_strErrMsg = std::string("create sub empty array error!"); 140 | return(false); 141 | } 142 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 143 | m_pKeyTravers = pFocusData; 144 | return(true); 145 | } 146 | 147 | bool CJsonObject::GetKey(std::string& strKey) 148 | { 149 | if (IsArray()) 150 | { 151 | return(false); 152 | } 153 | if (m_pKeyTravers == NULL) 154 | { 155 | if (m_pJsonData != NULL) 156 | { 157 | m_pKeyTravers = m_pJsonData; 158 | } 159 | else if (m_pExternJsonDataRef != NULL) 160 | { 161 | m_pKeyTravers = m_pExternJsonDataRef; 162 | } 163 | return(false); 164 | } 165 | else if (m_pKeyTravers == m_pJsonData || m_pKeyTravers == m_pExternJsonDataRef) 166 | { 167 | cJSON *c = m_pKeyTravers->child; 168 | if (c) 169 | { 170 | strKey = c->string; 171 | m_pKeyTravers = c->next; 172 | return(true); 173 | } 174 | else 175 | { 176 | return(false); 177 | } 178 | } 179 | else 180 | { 181 | strKey = m_pKeyTravers->string; 182 | m_pKeyTravers = m_pKeyTravers->next; 183 | return(true); 184 | } 185 | } 186 | 187 | void CJsonObject::ResetTraversing() 188 | { 189 | if (m_pJsonData != NULL) 190 | { 191 | m_pKeyTravers = m_pJsonData; 192 | } 193 | else 194 | { 195 | m_pKeyTravers = m_pExternJsonDataRef; 196 | } 197 | } 198 | 199 | CJsonObject& CJsonObject::operator[](const std::string& strKey) 200 | { 201 | std::map::iterator iter; 202 | iter = m_mapJsonObjectRef.find(strKey); 203 | if (iter == m_mapJsonObjectRef.end()) 204 | { 205 | cJSON* pJsonStruct = NULL; 206 | if (m_pJsonData != NULL) 207 | { 208 | if (m_pJsonData->type == cJSON_Object) 209 | { 210 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 211 | } 212 | } 213 | else if (m_pExternJsonDataRef != NULL) 214 | { 215 | if (m_pExternJsonDataRef->type == cJSON_Object) 216 | { 217 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 218 | } 219 | } 220 | if (pJsonStruct == NULL) 221 | { 222 | CJsonObject* pJsonObject = new CJsonObject(); 223 | m_mapJsonObjectRef.insert(std::pair(strKey, pJsonObject)); 224 | return(*pJsonObject); 225 | } 226 | else 227 | { 228 | CJsonObject* pJsonObject = new CJsonObject(pJsonStruct); 229 | m_mapJsonObjectRef.insert(std::pair(strKey, pJsonObject)); 230 | return(*pJsonObject); 231 | } 232 | } 233 | else 234 | { 235 | return(*(iter->second)); 236 | } 237 | } 238 | 239 | CJsonObject& CJsonObject::operator[](unsigned int uiWhich) 240 | { 241 | std::map::iterator iter; 242 | iter = m_mapJsonArrayRef.find(uiWhich); 243 | if (iter == m_mapJsonArrayRef.end()) 244 | { 245 | cJSON* pJsonStruct = NULL; 246 | if (m_pJsonData != NULL) 247 | { 248 | if (m_pJsonData->type == cJSON_Array) 249 | { 250 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, uiWhich); 251 | } 252 | } 253 | else if (m_pExternJsonDataRef != NULL) 254 | { 255 | if (m_pExternJsonDataRef->type == cJSON_Array) 256 | { 257 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, uiWhich); 258 | } 259 | } 260 | if (pJsonStruct == NULL) 261 | { 262 | CJsonObject* pJsonObject = new CJsonObject(); 263 | m_mapJsonArrayRef.insert(std::pair(uiWhich, pJsonObject)); 264 | return(*pJsonObject); 265 | } 266 | else 267 | { 268 | CJsonObject* pJsonObject = new CJsonObject(pJsonStruct); 269 | m_mapJsonArrayRef.insert(std::pair(uiWhich, pJsonObject)); 270 | return(*pJsonObject); 271 | } 272 | } 273 | else 274 | { 275 | return(*(iter->second)); 276 | } 277 | } 278 | 279 | std::string CJsonObject::operator()(const std::string& strKey) const 280 | { 281 | cJSON* pJsonStruct = NULL; 282 | if (m_pJsonData != NULL) 283 | { 284 | if (m_pJsonData->type == cJSON_Object) 285 | { 286 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 287 | } 288 | } 289 | else if (m_pExternJsonDataRef != NULL) 290 | { 291 | if(m_pExternJsonDataRef->type == cJSON_Object) 292 | { 293 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 294 | } 295 | } 296 | if (pJsonStruct == NULL) 297 | { 298 | return(std::string("")); 299 | } 300 | if (pJsonStruct->type == cJSON_String) 301 | { 302 | return(pJsonStruct->valuestring); 303 | } 304 | else if (pJsonStruct->type == cJSON_Int) 305 | { 306 | char szNumber[128] = {0}; 307 | if (pJsonStruct->sign == -1) 308 | { 309 | if (pJsonStruct->valueint <= (int64)INT_MAX && (int64)pJsonStruct->valueint >= (int64)INT_MIN) 310 | { 311 | _snprintf(szNumber, sizeof(szNumber), "%d", (int32)pJsonStruct->valueint); 312 | } 313 | else 314 | { 315 | _snprintf(szNumber, sizeof(szNumber), "%lld", (int64)pJsonStruct->valueint); 316 | } 317 | } 318 | else 319 | { 320 | if ((uint64)pJsonStruct->valueint <= (uint64)UINT_MAX) 321 | { 322 | _snprintf(szNumber, sizeof(szNumber), "%u", (uint32)pJsonStruct->valueint); 323 | } 324 | else 325 | { 326 | _snprintf(szNumber, sizeof(szNumber), "%llu", pJsonStruct->valueint); 327 | } 328 | } 329 | return(std::string(szNumber)); 330 | } 331 | else if (pJsonStruct->type == cJSON_Double) 332 | { 333 | char szNumber[128] = {0}; 334 | if (fabs(pJsonStruct->valuedouble) < 1.0e-6 || fabs(pJsonStruct->valuedouble) > 1.0e9) 335 | { 336 | _snprintf(szNumber, sizeof(szNumber), "%e", pJsonStruct->valuedouble); 337 | } 338 | else 339 | { 340 | _snprintf(szNumber, sizeof(szNumber), "%f", pJsonStruct->valuedouble); 341 | } 342 | } 343 | else if (pJsonStruct->type == cJSON_False) 344 | { 345 | return(std::string("false")); 346 | } 347 | else if (pJsonStruct->type == cJSON_True) 348 | { 349 | return(std::string("true")); 350 | } 351 | return(std::string("")); 352 | } 353 | 354 | std::string CJsonObject::operator()(unsigned int uiWhich) const 355 | { 356 | cJSON* pJsonStruct = NULL; 357 | if (m_pJsonData != NULL) 358 | { 359 | if (m_pJsonData->type == cJSON_Array) 360 | { 361 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, uiWhich); 362 | } 363 | } 364 | else if (m_pExternJsonDataRef != NULL) 365 | { 366 | if(m_pExternJsonDataRef->type == cJSON_Array) 367 | { 368 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, uiWhich); 369 | } 370 | } 371 | if (pJsonStruct == NULL) 372 | { 373 | return(std::string("")); 374 | } 375 | if (pJsonStruct->type == cJSON_String) 376 | { 377 | return(pJsonStruct->valuestring); 378 | } 379 | else if (pJsonStruct->type == cJSON_Int) 380 | { 381 | char szNumber[128] = {0}; 382 | if (pJsonStruct->sign == -1) 383 | { 384 | if (pJsonStruct->valueint <= (int64)INT_MAX && (int64)pJsonStruct->valueint >= (int64)INT_MIN) 385 | { 386 | _snprintf(szNumber, sizeof(szNumber), "%d", (int32)pJsonStruct->valueint); 387 | } 388 | else 389 | { 390 | _snprintf(szNumber, sizeof(szNumber), "%lld", (int64)pJsonStruct->valueint); 391 | } 392 | } 393 | else 394 | { 395 | if ((uint64)pJsonStruct->valueint <= (uint64)UINT_MAX) 396 | { 397 | _snprintf(szNumber, sizeof(szNumber), "%u", (uint32)pJsonStruct->valueint); 398 | } 399 | else 400 | { 401 | _snprintf(szNumber, sizeof(szNumber), "%llu", pJsonStruct->valueint); 402 | } 403 | } 404 | return(std::string(szNumber)); 405 | } 406 | else if (pJsonStruct->type == cJSON_Double) 407 | { 408 | char szNumber[128] = {0}; 409 | if (fabs(pJsonStruct->valuedouble) < 1.0e-6 || fabs(pJsonStruct->valuedouble) > 1.0e9) 410 | { 411 | _snprintf(szNumber, sizeof(szNumber), "%e", pJsonStruct->valuedouble); 412 | } 413 | else 414 | { 415 | _snprintf(szNumber, sizeof(szNumber), "%f", pJsonStruct->valuedouble); 416 | } 417 | } 418 | else if (pJsonStruct->type == cJSON_False) 419 | { 420 | return(std::string("false")); 421 | } 422 | else if (pJsonStruct->type == cJSON_True) 423 | { 424 | return(std::string("true")); 425 | } 426 | return(std::string("")); 427 | } 428 | 429 | bool CJsonObject::Parse(const std::string& strJson) 430 | { 431 | Clear(); 432 | m_pJsonData = cJSON_Parse(strJson.c_str()); 433 | m_pKeyTravers = m_pJsonData; 434 | if (m_pJsonData == NULL) 435 | { 436 | m_strErrMsg = std::string("prase json string error at ") + cJSON_GetErrorPtr(); 437 | return(false); 438 | } 439 | return(true); 440 | } 441 | 442 | void CJsonObject::Clear() 443 | { 444 | m_pExternJsonDataRef = NULL; 445 | m_pKeyTravers = NULL; 446 | if (m_pJsonData != NULL) 447 | { 448 | cJSON_Delete(m_pJsonData); 449 | m_pJsonData = NULL; 450 | } 451 | for (std::map::iterator iter = m_mapJsonArrayRef.begin(); 452 | iter != m_mapJsonArrayRef.end(); ++iter) 453 | { 454 | if (iter->second != NULL) 455 | { 456 | delete (iter->second); 457 | iter->second = NULL; 458 | } 459 | } 460 | m_mapJsonArrayRef.clear(); 461 | for (std::map::iterator iter = m_mapJsonObjectRef.begin(); 462 | iter != m_mapJsonObjectRef.end(); ++iter) 463 | { 464 | if (iter->second != NULL) 465 | { 466 | delete (iter->second); 467 | iter->second = NULL; 468 | } 469 | } 470 | m_mapJsonObjectRef.clear(); 471 | } 472 | 473 | bool CJsonObject::IsEmpty() const 474 | { 475 | if (m_pJsonData != NULL) 476 | { 477 | return(false); 478 | } 479 | else if (m_pExternJsonDataRef != NULL) 480 | { 481 | return(false); 482 | } 483 | return(true); 484 | } 485 | 486 | bool CJsonObject::IsArray() const 487 | { 488 | cJSON* pFocusData = NULL; 489 | if (m_pJsonData != NULL) 490 | { 491 | pFocusData = m_pJsonData; 492 | } 493 | else if (m_pExternJsonDataRef != NULL) 494 | { 495 | pFocusData = m_pExternJsonDataRef; 496 | } 497 | 498 | if (pFocusData == NULL) 499 | { 500 | return(false); 501 | } 502 | 503 | if (pFocusData->type == cJSON_Array) 504 | { 505 | return(true); 506 | } 507 | else 508 | { 509 | return(false); 510 | } 511 | } 512 | 513 | std::string CJsonObject::ToString() const 514 | { 515 | char* pJsonString = NULL; 516 | std::string strJsonData = ""; 517 | if (m_pJsonData != NULL) 518 | { 519 | pJsonString = cJSON_PrintUnformatted(m_pJsonData); 520 | } 521 | else if (m_pExternJsonDataRef != NULL) 522 | { 523 | pJsonString = cJSON_PrintUnformatted(m_pExternJsonDataRef); 524 | } 525 | if (pJsonString != NULL) 526 | { 527 | strJsonData = pJsonString; 528 | free(pJsonString); 529 | } 530 | return(strJsonData); 531 | } 532 | 533 | std::string CJsonObject::ToFormattedString() const 534 | { 535 | char* pJsonString = NULL; 536 | std::string strJsonData = ""; 537 | if (m_pJsonData != NULL) 538 | { 539 | pJsonString = cJSON_Print(m_pJsonData); 540 | } 541 | else if (m_pExternJsonDataRef != NULL) 542 | { 543 | pJsonString = cJSON_Print(m_pExternJsonDataRef); 544 | } 545 | if (pJsonString != NULL) 546 | { 547 | strJsonData = pJsonString; 548 | free(pJsonString); 549 | } 550 | return(strJsonData); 551 | } 552 | 553 | 554 | bool CJsonObject::Get(const std::string& strKey, CJsonObject& oJsonObject) const 555 | { 556 | cJSON* pJsonStruct = NULL; 557 | if (m_pJsonData != NULL) 558 | { 559 | if (m_pJsonData->type == cJSON_Object) 560 | { 561 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 562 | } 563 | } 564 | else if (m_pExternJsonDataRef != NULL) 565 | { 566 | if(m_pExternJsonDataRef->type == cJSON_Object) 567 | { 568 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 569 | } 570 | } 571 | if (pJsonStruct == NULL) 572 | { 573 | return(false); 574 | } 575 | char* pJsonString = cJSON_Print(pJsonStruct); 576 | std::string strJsonData = pJsonString; 577 | free(pJsonString); 578 | if (oJsonObject.Parse(strJsonData)) 579 | { 580 | return(true); 581 | } 582 | else 583 | { 584 | return(false); 585 | } 586 | } 587 | 588 | bool CJsonObject::Get(const std::string& strKey, std::string& strValue) const 589 | { 590 | cJSON* pJsonStruct = NULL; 591 | if (m_pJsonData != NULL) 592 | { 593 | if (m_pJsonData->type == cJSON_Object) 594 | { 595 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 596 | } 597 | } 598 | else if (m_pExternJsonDataRef != NULL) 599 | { 600 | if(m_pExternJsonDataRef->type == cJSON_Object) 601 | { 602 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 603 | } 604 | } 605 | if (pJsonStruct == NULL) 606 | { 607 | return(false); 608 | } 609 | if (pJsonStruct->type != cJSON_String) 610 | { 611 | return(false); 612 | } 613 | strValue = pJsonStruct->valuestring; 614 | return(true); 615 | } 616 | 617 | bool CJsonObject::Get(const std::string& strKey, int32& iValue) const 618 | { 619 | cJSON* pJsonStruct = NULL; 620 | if (m_pJsonData != NULL) 621 | { 622 | if (m_pJsonData->type == cJSON_Object) 623 | { 624 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 625 | } 626 | } 627 | else if (m_pExternJsonDataRef != NULL) 628 | { 629 | if(m_pExternJsonDataRef->type == cJSON_Object) 630 | { 631 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 632 | } 633 | } 634 | if (pJsonStruct == NULL) 635 | { 636 | return(false); 637 | } 638 | if (pJsonStruct->type == cJSON_Int) 639 | { 640 | iValue = (int32)(pJsonStruct->valueint); 641 | return(true); 642 | } 643 | else if (pJsonStruct->type == cJSON_Double) 644 | { 645 | iValue = (int32)(pJsonStruct->valuedouble); 646 | return(true); 647 | } 648 | return(false); 649 | } 650 | 651 | bool CJsonObject::Get(const std::string& strKey, uint32& uiValue) const 652 | { 653 | cJSON* pJsonStruct = NULL; 654 | if (m_pJsonData != NULL) 655 | { 656 | if (m_pJsonData->type == cJSON_Object) 657 | { 658 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 659 | } 660 | } 661 | else if (m_pExternJsonDataRef != NULL) 662 | { 663 | if(m_pExternJsonDataRef->type == cJSON_Object) 664 | { 665 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 666 | } 667 | } 668 | if (pJsonStruct == NULL) 669 | { 670 | return(false); 671 | } 672 | if (pJsonStruct->type == cJSON_Int) 673 | { 674 | uiValue = (uint32)(pJsonStruct->valueint); 675 | return(true); 676 | } 677 | else if (pJsonStruct->type == cJSON_Double) 678 | { 679 | uiValue = (uint32)(pJsonStruct->valuedouble); 680 | return(true); 681 | } 682 | return(false); 683 | } 684 | 685 | bool CJsonObject::Get(const std::string& strKey, int64& llValue) const 686 | { 687 | cJSON* pJsonStruct = NULL; 688 | if (m_pJsonData != NULL) 689 | { 690 | if (m_pJsonData->type == cJSON_Object) 691 | { 692 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 693 | } 694 | } 695 | else if (m_pExternJsonDataRef != NULL) 696 | { 697 | if(m_pExternJsonDataRef->type == cJSON_Object) 698 | { 699 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 700 | } 701 | } 702 | if (pJsonStruct == NULL) 703 | { 704 | return(false); 705 | } 706 | if (pJsonStruct->type == cJSON_Int) 707 | { 708 | llValue = (int64)(pJsonStruct->valueint); 709 | return(true); 710 | } 711 | else if (pJsonStruct->type == cJSON_Double) 712 | { 713 | llValue = (int64)(pJsonStruct->valuedouble); 714 | return(true); 715 | } 716 | return(false); 717 | } 718 | 719 | bool CJsonObject::Get(const std::string& strKey, uint64& ullValue) const 720 | { 721 | cJSON* pJsonStruct = NULL; 722 | if (m_pJsonData != NULL) 723 | { 724 | if (m_pJsonData->type == cJSON_Object) 725 | { 726 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 727 | } 728 | } 729 | else if (m_pExternJsonDataRef != NULL) 730 | { 731 | if(m_pExternJsonDataRef->type == cJSON_Object) 732 | { 733 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 734 | } 735 | } 736 | if (pJsonStruct == NULL) 737 | { 738 | return(false); 739 | } 740 | if (pJsonStruct->type == cJSON_Int) 741 | { 742 | ullValue = (uint64)(pJsonStruct->valueint); 743 | return(true); 744 | } 745 | else if (pJsonStruct->type == cJSON_Double) 746 | { 747 | ullValue = (uint64)(pJsonStruct->valuedouble); 748 | return(true); 749 | } 750 | return(false); 751 | } 752 | 753 | bool CJsonObject::Get(const std::string& strKey, bool& bValue) const 754 | { 755 | cJSON* pJsonStruct = NULL; 756 | if (m_pJsonData != NULL) 757 | { 758 | if (m_pJsonData->type == cJSON_Object) 759 | { 760 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 761 | } 762 | } 763 | else if (m_pExternJsonDataRef != NULL) 764 | { 765 | if(m_pExternJsonDataRef->type == cJSON_Object) 766 | { 767 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 768 | } 769 | } 770 | if (pJsonStruct == NULL) 771 | { 772 | return(false); 773 | } 774 | if (pJsonStruct->type > cJSON_True) 775 | { 776 | return(false); 777 | } 778 | bValue = pJsonStruct->type; 779 | return(true); 780 | } 781 | 782 | bool CJsonObject::Get(const std::string& strKey, float& fValue) const 783 | { 784 | cJSON* pJsonStruct = NULL; 785 | if (m_pJsonData != NULL) 786 | { 787 | if (m_pJsonData->type == cJSON_Object) 788 | { 789 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 790 | } 791 | } 792 | else if (m_pExternJsonDataRef != NULL) 793 | { 794 | if(m_pExternJsonDataRef->type == cJSON_Object) 795 | { 796 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 797 | } 798 | } 799 | if (pJsonStruct == NULL) 800 | { 801 | return(false); 802 | } 803 | if (pJsonStruct->type == cJSON_Double || pJsonStruct->type == cJSON_Int) 804 | { 805 | fValue = (float)(pJsonStruct->valuedouble); 806 | return(true); 807 | } 808 | return(false); 809 | } 810 | 811 | bool CJsonObject::Get(const std::string& strKey, double& dValue) const 812 | { 813 | cJSON* pJsonStruct = NULL; 814 | if (m_pJsonData != NULL) 815 | { 816 | if (m_pJsonData->type == cJSON_Object) 817 | { 818 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 819 | } 820 | } 821 | else if (m_pExternJsonDataRef != NULL) 822 | { 823 | if(m_pExternJsonDataRef->type == cJSON_Object) 824 | { 825 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 826 | } 827 | } 828 | if (pJsonStruct == NULL) 829 | { 830 | return(false); 831 | } 832 | if (pJsonStruct->type == cJSON_Double || pJsonStruct->type == cJSON_Int) 833 | { 834 | dValue = pJsonStruct->valuedouble; 835 | return(true); 836 | } 837 | return(false); 838 | } 839 | 840 | bool CJsonObject::IsNull(const std::string& strKey) const 841 | { 842 | cJSON* pJsonStruct = NULL; 843 | if (m_pJsonData != NULL) 844 | { 845 | if (m_pJsonData->type == cJSON_Object) 846 | { 847 | pJsonStruct = cJSON_GetObjectItem(m_pJsonData, strKey.c_str()); 848 | } 849 | } 850 | else if (m_pExternJsonDataRef != NULL) 851 | { 852 | if(m_pExternJsonDataRef->type == cJSON_Object) 853 | { 854 | pJsonStruct = cJSON_GetObjectItem(m_pExternJsonDataRef, strKey.c_str()); 855 | } 856 | } 857 | if (pJsonStruct == NULL) 858 | { 859 | return(false); 860 | } 861 | if (pJsonStruct->type != cJSON_NULL) 862 | { 863 | return(false); 864 | } 865 | return(true); 866 | } 867 | 868 | bool CJsonObject::Add(const std::string& strKey, const CJsonObject& oJsonObject) 869 | { 870 | cJSON* pFocusData = NULL; 871 | if (m_pJsonData != NULL) 872 | { 873 | pFocusData = m_pJsonData; 874 | } 875 | else if (m_pExternJsonDataRef != NULL) 876 | { 877 | pFocusData = m_pExternJsonDataRef; 878 | } 879 | else 880 | { 881 | m_pJsonData = cJSON_CreateObject(); 882 | m_pKeyTravers = m_pJsonData; 883 | pFocusData = m_pJsonData; 884 | } 885 | 886 | if (pFocusData == NULL) 887 | { 888 | m_strErrMsg = "json data is null!"; 889 | return(false); 890 | } 891 | if (pFocusData->type != cJSON_Object) 892 | { 893 | m_strErrMsg = "not a json object! json array?"; 894 | return(false); 895 | } 896 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 897 | { 898 | m_strErrMsg = "key exists!"; 899 | return(false); 900 | } 901 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str()); 902 | if (pJsonStruct == NULL) 903 | { 904 | m_strErrMsg = std::string("prase json string error at ") + cJSON_GetErrorPtr(); 905 | return(false); 906 | } 907 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 908 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 909 | { 910 | return(false); 911 | } 912 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 913 | if (iter != m_mapJsonObjectRef.end()) 914 | { 915 | if (iter->second != NULL) 916 | { 917 | delete (iter->second); 918 | iter->second = NULL; 919 | } 920 | m_mapJsonObjectRef.erase(iter); 921 | } 922 | m_pKeyTravers = pFocusData; 923 | return(true); 924 | } 925 | 926 | bool CJsonObject::Add(const std::string& strKey, const std::string& strValue) 927 | { 928 | cJSON* pFocusData = NULL; 929 | if (m_pJsonData != NULL) 930 | { 931 | pFocusData = m_pJsonData; 932 | } 933 | else if (m_pExternJsonDataRef != NULL) 934 | { 935 | pFocusData = m_pExternJsonDataRef; 936 | } 937 | else 938 | { 939 | m_pJsonData = cJSON_CreateObject(); 940 | m_pKeyTravers = m_pJsonData; 941 | pFocusData = m_pJsonData; 942 | } 943 | 944 | if (pFocusData == NULL) 945 | { 946 | m_strErrMsg = "json data is null!"; 947 | return(false); 948 | } 949 | if (pFocusData->type != cJSON_Object) 950 | { 951 | m_strErrMsg = "not a json object! json array?"; 952 | return(false); 953 | } 954 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 955 | { 956 | m_strErrMsg = "key exists!"; 957 | return(false); 958 | } 959 | cJSON* pJsonStruct = cJSON_CreateString(strValue.c_str()); 960 | if (pJsonStruct == NULL) 961 | { 962 | return(false); 963 | } 964 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 965 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 966 | { 967 | return(false); 968 | } 969 | m_pKeyTravers = pFocusData; 970 | return(true); 971 | } 972 | 973 | bool CJsonObject::Add(const std::string& strKey, int32 iValue) 974 | { 975 | cJSON* pFocusData = NULL; 976 | if (m_pJsonData != NULL) 977 | { 978 | pFocusData = m_pJsonData; 979 | } 980 | else if (m_pExternJsonDataRef != NULL) 981 | { 982 | pFocusData = m_pExternJsonDataRef; 983 | } 984 | else 985 | { 986 | m_pJsonData = cJSON_CreateObject(); 987 | m_pKeyTravers = m_pJsonData; 988 | pFocusData = m_pJsonData; 989 | } 990 | 991 | if (pFocusData == NULL) 992 | { 993 | m_strErrMsg = "json data is null!"; 994 | return(false); 995 | } 996 | if (pFocusData->type != cJSON_Object) 997 | { 998 | m_strErrMsg = "not a json object! json array?"; 999 | return(false); 1000 | } 1001 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1002 | { 1003 | m_strErrMsg = "key exists!"; 1004 | return(false); 1005 | } 1006 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)iValue, -1); 1007 | if (pJsonStruct == NULL) 1008 | { 1009 | return(false); 1010 | } 1011 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1012 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1013 | { 1014 | return(false); 1015 | } 1016 | m_pKeyTravers = pFocusData; 1017 | return(true); 1018 | } 1019 | 1020 | bool CJsonObject::Add(const std::string& strKey, uint32 uiValue) 1021 | { 1022 | cJSON* pFocusData = NULL; 1023 | if (m_pJsonData != NULL) 1024 | { 1025 | pFocusData = m_pJsonData; 1026 | } 1027 | else if (m_pExternJsonDataRef != NULL) 1028 | { 1029 | pFocusData = m_pExternJsonDataRef; 1030 | } 1031 | else 1032 | { 1033 | m_pJsonData = cJSON_CreateObject(); 1034 | m_pKeyTravers = m_pJsonData; 1035 | pFocusData = m_pJsonData; 1036 | } 1037 | 1038 | if (pFocusData == NULL) 1039 | { 1040 | m_strErrMsg = "json data is null!"; 1041 | return(false); 1042 | } 1043 | if (pFocusData->type != cJSON_Object) 1044 | { 1045 | m_strErrMsg = "not a json object! json array?"; 1046 | return(false); 1047 | } 1048 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1049 | { 1050 | m_strErrMsg = "key exists!"; 1051 | return(false); 1052 | } 1053 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)uiValue, 1); 1054 | if (pJsonStruct == NULL) 1055 | { 1056 | return(false); 1057 | } 1058 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1059 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1060 | { 1061 | return(false); 1062 | } 1063 | m_pKeyTravers = pFocusData; 1064 | return(true); 1065 | } 1066 | 1067 | bool CJsonObject::Add(const std::string& strKey, int64 llValue) 1068 | { 1069 | cJSON* pFocusData = NULL; 1070 | if (m_pJsonData != NULL) 1071 | { 1072 | pFocusData = m_pJsonData; 1073 | } 1074 | else if (m_pExternJsonDataRef != NULL) 1075 | { 1076 | pFocusData = m_pExternJsonDataRef; 1077 | } 1078 | else 1079 | { 1080 | m_pJsonData = cJSON_CreateObject(); 1081 | m_pKeyTravers = m_pJsonData; 1082 | pFocusData = m_pJsonData; 1083 | } 1084 | 1085 | if (pFocusData == NULL) 1086 | { 1087 | m_strErrMsg = "json data is null!"; 1088 | return(false); 1089 | } 1090 | if (pFocusData->type != cJSON_Object) 1091 | { 1092 | m_strErrMsg = "not a json object! json array?"; 1093 | return(false); 1094 | } 1095 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1096 | { 1097 | m_strErrMsg = "key exists!"; 1098 | return(false); 1099 | } 1100 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)llValue, -1); 1101 | if (pJsonStruct == NULL) 1102 | { 1103 | return(false); 1104 | } 1105 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1106 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1107 | { 1108 | return(false); 1109 | } 1110 | m_pKeyTravers = pFocusData; 1111 | return(true); 1112 | } 1113 | 1114 | bool CJsonObject::Add(const std::string& strKey, uint64 ullValue) 1115 | { 1116 | cJSON* pFocusData = NULL; 1117 | if (m_pJsonData != NULL) 1118 | { 1119 | pFocusData = m_pJsonData; 1120 | } 1121 | else if (m_pExternJsonDataRef != NULL) 1122 | { 1123 | pFocusData = m_pExternJsonDataRef; 1124 | } 1125 | else 1126 | { 1127 | m_pJsonData = cJSON_CreateObject(); 1128 | m_pKeyTravers = m_pJsonData; 1129 | pFocusData = m_pJsonData; 1130 | } 1131 | 1132 | if (pFocusData == NULL) 1133 | { 1134 | m_strErrMsg = "json data is null!"; 1135 | return(false); 1136 | } 1137 | if (pFocusData->type != cJSON_Object) 1138 | { 1139 | m_strErrMsg = "not a json object! json array?"; 1140 | return(false); 1141 | } 1142 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1143 | { 1144 | m_strErrMsg = "key exists!"; 1145 | return(false); 1146 | } 1147 | cJSON* pJsonStruct = cJSON_CreateInt(ullValue, 1); 1148 | if (pJsonStruct == NULL) 1149 | { 1150 | return(false); 1151 | } 1152 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1153 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1154 | { 1155 | return(false); 1156 | } 1157 | m_pKeyTravers = pFocusData; 1158 | return(true); 1159 | } 1160 | 1161 | bool CJsonObject::Add(const std::string& strKey, bool bValue, bool bValueAgain) 1162 | { 1163 | cJSON* pFocusData = NULL; 1164 | if (m_pJsonData != NULL) 1165 | { 1166 | pFocusData = m_pJsonData; 1167 | } 1168 | else if (m_pExternJsonDataRef != NULL) 1169 | { 1170 | pFocusData = m_pExternJsonDataRef; 1171 | } 1172 | else 1173 | { 1174 | m_pJsonData = cJSON_CreateObject(); 1175 | m_pKeyTravers = m_pJsonData; 1176 | pFocusData = m_pJsonData; 1177 | } 1178 | 1179 | if (pFocusData == NULL) 1180 | { 1181 | m_strErrMsg = "json data is null!"; 1182 | return(false); 1183 | } 1184 | if (pFocusData->type != cJSON_Object) 1185 | { 1186 | m_strErrMsg = "not a json object! json array?"; 1187 | return(false); 1188 | } 1189 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1190 | { 1191 | m_strErrMsg = "key exists!"; 1192 | return(false); 1193 | } 1194 | cJSON* pJsonStruct = cJSON_CreateBool(bValue); 1195 | if (pJsonStruct == NULL) 1196 | { 1197 | return(false); 1198 | } 1199 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1200 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1201 | { 1202 | return(false); 1203 | } 1204 | m_pKeyTravers = pFocusData; 1205 | return(true); 1206 | } 1207 | 1208 | bool CJsonObject::Add(const std::string& strKey, float fValue) 1209 | { 1210 | cJSON* pFocusData = NULL; 1211 | if (m_pJsonData != NULL) 1212 | { 1213 | pFocusData = m_pJsonData; 1214 | } 1215 | else if (m_pExternJsonDataRef != NULL) 1216 | { 1217 | pFocusData = m_pExternJsonDataRef; 1218 | } 1219 | else 1220 | { 1221 | m_pJsonData = cJSON_CreateObject(); 1222 | m_pKeyTravers = m_pJsonData; 1223 | pFocusData = m_pJsonData; 1224 | } 1225 | 1226 | if (pFocusData == NULL) 1227 | { 1228 | m_strErrMsg = "json data is null!"; 1229 | return(false); 1230 | } 1231 | if (pFocusData->type != cJSON_Object) 1232 | { 1233 | m_strErrMsg = "not a json object! json array?"; 1234 | return(false); 1235 | } 1236 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1237 | { 1238 | m_strErrMsg = "key exists!"; 1239 | return(false); 1240 | } 1241 | cJSON* pJsonStruct = cJSON_CreateDouble((double)fValue, -1); 1242 | if (pJsonStruct == NULL) 1243 | { 1244 | return(false); 1245 | } 1246 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1247 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1248 | { 1249 | return(false); 1250 | } 1251 | m_pKeyTravers = pFocusData; 1252 | return(true); 1253 | } 1254 | 1255 | bool CJsonObject::Add(const std::string& strKey, double dValue) 1256 | { 1257 | cJSON* pFocusData = NULL; 1258 | if (m_pJsonData != NULL) 1259 | { 1260 | pFocusData = m_pJsonData; 1261 | } 1262 | else if (m_pExternJsonDataRef != NULL) 1263 | { 1264 | pFocusData = m_pExternJsonDataRef; 1265 | } 1266 | else 1267 | { 1268 | m_pJsonData = cJSON_CreateObject(); 1269 | m_pKeyTravers = m_pJsonData; 1270 | pFocusData = m_pJsonData; 1271 | } 1272 | 1273 | if (pFocusData == NULL) 1274 | { 1275 | m_strErrMsg = "json data is null!"; 1276 | return(false); 1277 | } 1278 | if (pFocusData->type != cJSON_Object) 1279 | { 1280 | m_strErrMsg = "not a json object! json array?"; 1281 | return(false); 1282 | } 1283 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1284 | { 1285 | m_strErrMsg = "key exists!"; 1286 | return(false); 1287 | } 1288 | cJSON* pJsonStruct = cJSON_CreateDouble((double)dValue, -1); 1289 | if (pJsonStruct == NULL) 1290 | { 1291 | return(false); 1292 | } 1293 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1294 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1295 | { 1296 | return(false); 1297 | } 1298 | m_pKeyTravers = pFocusData; 1299 | return(true); 1300 | } 1301 | 1302 | bool CJsonObject::AddNull(const std::string& strKey) 1303 | { 1304 | cJSON* pFocusData = NULL; 1305 | if (m_pJsonData != NULL) 1306 | { 1307 | pFocusData = m_pJsonData; 1308 | } 1309 | else if (m_pExternJsonDataRef != NULL) 1310 | { 1311 | pFocusData = m_pExternJsonDataRef; 1312 | } 1313 | else 1314 | { 1315 | m_pJsonData = cJSON_CreateObject(); 1316 | m_pKeyTravers = m_pJsonData; 1317 | pFocusData = m_pJsonData; 1318 | } 1319 | 1320 | if (pFocusData == NULL) 1321 | { 1322 | m_strErrMsg = "json data is null!"; 1323 | return(false); 1324 | } 1325 | if (pFocusData->type != cJSON_Object) 1326 | { 1327 | m_strErrMsg = "not a json object! json array?"; 1328 | return(false); 1329 | } 1330 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) 1331 | { 1332 | m_strErrMsg = "key exists!"; 1333 | return(false); 1334 | } 1335 | cJSON* pJsonStruct = cJSON_CreateNull(); 1336 | if (pJsonStruct == NULL) 1337 | { 1338 | return(false); 1339 | } 1340 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); 1341 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1342 | { 1343 | return(false); 1344 | } 1345 | m_pKeyTravers = pFocusData; 1346 | return(true); 1347 | } 1348 | 1349 | bool CJsonObject::Delete(const std::string& strKey) 1350 | { 1351 | cJSON* pFocusData = NULL; 1352 | if (m_pJsonData == NULL) 1353 | { 1354 | pFocusData = m_pExternJsonDataRef; 1355 | } 1356 | else 1357 | { 1358 | pFocusData = m_pJsonData; 1359 | } 1360 | if (pFocusData == NULL) 1361 | { 1362 | m_strErrMsg = "json data is null!"; 1363 | return(false); 1364 | } 1365 | if (pFocusData->type != cJSON_Object) 1366 | { 1367 | m_strErrMsg = "not a json object! json array?"; 1368 | return(false); 1369 | } 1370 | cJSON_DeleteItemFromObject(pFocusData, strKey.c_str()); 1371 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1372 | if (iter != m_mapJsonObjectRef.end()) 1373 | { 1374 | if (iter->second != NULL) 1375 | { 1376 | delete (iter->second); 1377 | iter->second = NULL; 1378 | } 1379 | m_mapJsonObjectRef.erase(iter); 1380 | } 1381 | m_pKeyTravers = pFocusData; 1382 | return(true); 1383 | } 1384 | 1385 | bool CJsonObject::Replace(const std::string& strKey, const CJsonObject& oJsonObject) 1386 | { 1387 | cJSON* pFocusData = NULL; 1388 | if (m_pJsonData == NULL) 1389 | { 1390 | pFocusData = m_pExternJsonDataRef; 1391 | } 1392 | else 1393 | { 1394 | pFocusData = m_pJsonData; 1395 | } 1396 | if (pFocusData == NULL) 1397 | { 1398 | m_strErrMsg = "json data is null!"; 1399 | return(false); 1400 | } 1401 | if (pFocusData->type != cJSON_Object) 1402 | { 1403 | m_strErrMsg = "not a json object! json array?"; 1404 | return(false); 1405 | } 1406 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str()); 1407 | if (pJsonStruct == NULL) 1408 | { 1409 | m_strErrMsg = std::string("prase json string error at ") + cJSON_GetErrorPtr(); 1410 | return(false); 1411 | } 1412 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1413 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1414 | { 1415 | return(false); 1416 | } 1417 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1418 | if (iter != m_mapJsonObjectRef.end()) 1419 | { 1420 | if (iter->second != NULL) 1421 | { 1422 | delete (iter->second); 1423 | iter->second = NULL; 1424 | } 1425 | m_mapJsonObjectRef.erase(iter); 1426 | } 1427 | return(true); 1428 | } 1429 | 1430 | bool CJsonObject::Replace(const std::string& strKey, const std::string& strValue) 1431 | { 1432 | cJSON* pFocusData = NULL; 1433 | if (m_pJsonData == NULL) 1434 | { 1435 | pFocusData = m_pExternJsonDataRef; 1436 | } 1437 | else 1438 | { 1439 | pFocusData = m_pJsonData; 1440 | } 1441 | if (pFocusData == NULL) 1442 | { 1443 | m_strErrMsg = "json data is null!"; 1444 | return(false); 1445 | } 1446 | if (pFocusData->type != cJSON_Object) 1447 | { 1448 | m_strErrMsg = "not a json object! json array?"; 1449 | return(false); 1450 | } 1451 | cJSON* pJsonStruct = cJSON_CreateString(strValue.c_str()); 1452 | if (pJsonStruct == NULL) 1453 | { 1454 | return(false); 1455 | } 1456 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1457 | if (iter != m_mapJsonObjectRef.end()) 1458 | { 1459 | if (iter->second != NULL) 1460 | { 1461 | delete (iter->second); 1462 | iter->second = NULL; 1463 | } 1464 | m_mapJsonObjectRef.erase(iter); 1465 | } 1466 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1467 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1468 | { 1469 | return(false); 1470 | } 1471 | return(true); 1472 | } 1473 | 1474 | bool CJsonObject::Replace(const std::string& strKey, int32 iValue) 1475 | { 1476 | cJSON* pFocusData = NULL; 1477 | if (m_pJsonData == NULL) 1478 | { 1479 | pFocusData = m_pExternJsonDataRef; 1480 | } 1481 | else 1482 | { 1483 | pFocusData = m_pJsonData; 1484 | } 1485 | if (pFocusData == NULL) 1486 | { 1487 | m_strErrMsg = "json data is null!"; 1488 | return(false); 1489 | } 1490 | if (pFocusData->type != cJSON_Object) 1491 | { 1492 | m_strErrMsg = "not a json object! json array?"; 1493 | return(false); 1494 | } 1495 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)iValue, -1); 1496 | if (pJsonStruct == NULL) 1497 | { 1498 | return(false); 1499 | } 1500 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1501 | if (iter != m_mapJsonObjectRef.end()) 1502 | { 1503 | if (iter->second != NULL) 1504 | { 1505 | delete (iter->second); 1506 | iter->second = NULL; 1507 | } 1508 | m_mapJsonObjectRef.erase(iter); 1509 | } 1510 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1511 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1512 | { 1513 | return(false); 1514 | } 1515 | return(true); 1516 | } 1517 | 1518 | bool CJsonObject::Replace(const std::string& strKey, uint32 uiValue) 1519 | { 1520 | cJSON* pFocusData = NULL; 1521 | if (m_pJsonData == NULL) 1522 | { 1523 | pFocusData = m_pExternJsonDataRef; 1524 | } 1525 | else 1526 | { 1527 | pFocusData = m_pJsonData; 1528 | } 1529 | if (pFocusData == NULL) 1530 | { 1531 | m_strErrMsg = "json data is null!"; 1532 | return(false); 1533 | } 1534 | if (pFocusData->type != cJSON_Object) 1535 | { 1536 | m_strErrMsg = "not a json object! json array?"; 1537 | return(false); 1538 | } 1539 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)uiValue, 1); 1540 | if (pJsonStruct == NULL) 1541 | { 1542 | return(false); 1543 | } 1544 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1545 | if (iter != m_mapJsonObjectRef.end()) 1546 | { 1547 | if (iter->second != NULL) 1548 | { 1549 | delete (iter->second); 1550 | iter->second = NULL; 1551 | } 1552 | m_mapJsonObjectRef.erase(iter); 1553 | } 1554 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1555 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1556 | { 1557 | return(false); 1558 | } 1559 | return(true); 1560 | } 1561 | 1562 | bool CJsonObject::Replace(const std::string& strKey, int64 llValue) 1563 | { 1564 | cJSON* pFocusData = NULL; 1565 | if (m_pJsonData == NULL) 1566 | { 1567 | pFocusData = m_pExternJsonDataRef; 1568 | } 1569 | else 1570 | { 1571 | pFocusData = m_pJsonData; 1572 | } 1573 | if (pFocusData == NULL) 1574 | { 1575 | m_strErrMsg = "json data is null!"; 1576 | return(false); 1577 | } 1578 | if (pFocusData->type != cJSON_Object) 1579 | { 1580 | m_strErrMsg = "not a json object! json array?"; 1581 | return(false); 1582 | } 1583 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)llValue, -1); 1584 | if (pJsonStruct == NULL) 1585 | { 1586 | return(false); 1587 | } 1588 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1589 | if (iter != m_mapJsonObjectRef.end()) 1590 | { 1591 | if (iter->second != NULL) 1592 | { 1593 | delete (iter->second); 1594 | iter->second = NULL; 1595 | } 1596 | m_mapJsonObjectRef.erase(iter); 1597 | } 1598 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1599 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1600 | { 1601 | return(false); 1602 | } 1603 | return(true); 1604 | } 1605 | 1606 | bool CJsonObject::Replace(const std::string& strKey, uint64 ullValue) 1607 | { 1608 | cJSON* pFocusData = NULL; 1609 | if (m_pJsonData == NULL) 1610 | { 1611 | pFocusData = m_pExternJsonDataRef; 1612 | } 1613 | else 1614 | { 1615 | pFocusData = m_pJsonData; 1616 | } 1617 | if (pFocusData == NULL) 1618 | { 1619 | m_strErrMsg = "json data is null!"; 1620 | return(false); 1621 | } 1622 | if (pFocusData->type != cJSON_Object) 1623 | { 1624 | m_strErrMsg = "not a json object! json array?"; 1625 | return(false); 1626 | } 1627 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)ullValue, 1); 1628 | if (pJsonStruct == NULL) 1629 | { 1630 | return(false); 1631 | } 1632 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1633 | if (iter != m_mapJsonObjectRef.end()) 1634 | { 1635 | if (iter->second != NULL) 1636 | { 1637 | delete (iter->second); 1638 | iter->second = NULL; 1639 | } 1640 | m_mapJsonObjectRef.erase(iter); 1641 | } 1642 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1643 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1644 | { 1645 | return(false); 1646 | } 1647 | return(true); 1648 | } 1649 | 1650 | bool CJsonObject::Replace(const std::string& strKey, bool bValue, bool bValueAgain) 1651 | { 1652 | cJSON* pFocusData = NULL; 1653 | if (m_pJsonData == NULL) 1654 | { 1655 | pFocusData = m_pExternJsonDataRef; 1656 | } 1657 | else 1658 | { 1659 | pFocusData = m_pJsonData; 1660 | } 1661 | if (pFocusData == NULL) 1662 | { 1663 | m_strErrMsg = "json data is null!"; 1664 | return(false); 1665 | } 1666 | if (pFocusData->type != cJSON_Object) 1667 | { 1668 | m_strErrMsg = "not a json object! json array?"; 1669 | return(false); 1670 | } 1671 | cJSON* pJsonStruct = cJSON_CreateBool(bValue); 1672 | if (pJsonStruct == NULL) 1673 | { 1674 | return(false); 1675 | } 1676 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1677 | if (iter != m_mapJsonObjectRef.end()) 1678 | { 1679 | if (iter->second != NULL) 1680 | { 1681 | delete (iter->second); 1682 | iter->second = NULL; 1683 | } 1684 | m_mapJsonObjectRef.erase(iter); 1685 | } 1686 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1687 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1688 | { 1689 | return(false); 1690 | } 1691 | return(true); 1692 | } 1693 | 1694 | bool CJsonObject::Replace(const std::string& strKey, float fValue) 1695 | { 1696 | cJSON* pFocusData = NULL; 1697 | if (m_pJsonData == NULL) 1698 | { 1699 | pFocusData = m_pExternJsonDataRef; 1700 | } 1701 | else 1702 | { 1703 | pFocusData = m_pJsonData; 1704 | } 1705 | if (pFocusData == NULL) 1706 | { 1707 | m_strErrMsg = "json data is null!"; 1708 | return(false); 1709 | } 1710 | if (pFocusData->type != cJSON_Object) 1711 | { 1712 | m_strErrMsg = "not a json object! json array?"; 1713 | return(false); 1714 | } 1715 | cJSON* pJsonStruct = cJSON_CreateDouble((double)fValue, -1); 1716 | if (pJsonStruct == NULL) 1717 | { 1718 | return(false); 1719 | } 1720 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1721 | if (iter != m_mapJsonObjectRef.end()) 1722 | { 1723 | if (iter->second != NULL) 1724 | { 1725 | delete (iter->second); 1726 | iter->second = NULL; 1727 | } 1728 | m_mapJsonObjectRef.erase(iter); 1729 | } 1730 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1731 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1732 | { 1733 | return(false); 1734 | } 1735 | return(true); 1736 | } 1737 | 1738 | bool CJsonObject::Replace(const std::string& strKey, double dValue) 1739 | { 1740 | cJSON* pFocusData = NULL; 1741 | if (m_pJsonData == NULL) 1742 | { 1743 | pFocusData = m_pExternJsonDataRef; 1744 | } 1745 | else 1746 | { 1747 | pFocusData = m_pJsonData; 1748 | } 1749 | if (pFocusData == NULL) 1750 | { 1751 | m_strErrMsg = "json data is null!"; 1752 | return(false); 1753 | } 1754 | if (pFocusData->type != cJSON_Object) 1755 | { 1756 | m_strErrMsg = "not a json object! json array?"; 1757 | return(false); 1758 | } 1759 | cJSON* pJsonStruct = cJSON_CreateDouble((double)dValue, -1); 1760 | if (pJsonStruct == NULL) 1761 | { 1762 | return(false); 1763 | } 1764 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1765 | if (iter != m_mapJsonObjectRef.end()) 1766 | { 1767 | if (iter->second != NULL) 1768 | { 1769 | delete (iter->second); 1770 | iter->second = NULL; 1771 | } 1772 | m_mapJsonObjectRef.erase(iter); 1773 | } 1774 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1775 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1776 | { 1777 | return(false); 1778 | } 1779 | return(true); 1780 | } 1781 | 1782 | bool CJsonObject::ReplaceWithNull(const std::string& strKey) 1783 | { 1784 | cJSON* pFocusData = NULL; 1785 | if (m_pJsonData == NULL) 1786 | { 1787 | pFocusData = m_pExternJsonDataRef; 1788 | } 1789 | else 1790 | { 1791 | pFocusData = m_pJsonData; 1792 | } 1793 | if (pFocusData == NULL) 1794 | { 1795 | m_strErrMsg = "json data is null!"; 1796 | return(false); 1797 | } 1798 | if (pFocusData->type != cJSON_Object) 1799 | { 1800 | m_strErrMsg = "not a json object! json array?"; 1801 | return(false); 1802 | } 1803 | cJSON* pJsonStruct = cJSON_CreateNull(); 1804 | if (pJsonStruct == NULL) 1805 | { 1806 | return(false); 1807 | } 1808 | std::map::iterator iter = m_mapJsonObjectRef.find(strKey); 1809 | if (iter != m_mapJsonObjectRef.end()) 1810 | { 1811 | if (iter->second != NULL) 1812 | { 1813 | delete (iter->second); 1814 | iter->second = NULL; 1815 | } 1816 | m_mapJsonObjectRef.erase(iter); 1817 | } 1818 | cJSON_ReplaceItemInObject(pFocusData, strKey.c_str(), pJsonStruct); 1819 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) 1820 | { 1821 | return(false); 1822 | } 1823 | return(true); 1824 | } 1825 | 1826 | int CJsonObject::GetArraySize() 1827 | { 1828 | if (m_pJsonData != NULL) 1829 | { 1830 | if (m_pJsonData->type == cJSON_Array) 1831 | { 1832 | return(cJSON_GetArraySize(m_pJsonData)); 1833 | } 1834 | } 1835 | else if (m_pExternJsonDataRef != NULL) 1836 | { 1837 | if(m_pExternJsonDataRef->type == cJSON_Array) 1838 | { 1839 | return(cJSON_GetArraySize(m_pExternJsonDataRef)); 1840 | } 1841 | } 1842 | return(0); 1843 | } 1844 | 1845 | bool CJsonObject::Get(int iWhich, CJsonObject& oJsonObject) const 1846 | { 1847 | cJSON* pJsonStruct = NULL; 1848 | if (m_pJsonData != NULL) 1849 | { 1850 | if (m_pJsonData->type == cJSON_Array) 1851 | { 1852 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 1853 | } 1854 | } 1855 | else if (m_pExternJsonDataRef != NULL) 1856 | { 1857 | if(m_pExternJsonDataRef->type == cJSON_Array) 1858 | { 1859 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 1860 | } 1861 | } 1862 | if (pJsonStruct == NULL) 1863 | { 1864 | return(false); 1865 | } 1866 | char* pJsonString = cJSON_Print(pJsonStruct); 1867 | std::string strJsonData = pJsonString; 1868 | free(pJsonString); 1869 | if (oJsonObject.Parse(strJsonData)) 1870 | { 1871 | return(true); 1872 | } 1873 | else 1874 | { 1875 | return(false); 1876 | } 1877 | } 1878 | 1879 | bool CJsonObject::Get(int iWhich, std::string& strValue) const 1880 | { 1881 | cJSON* pJsonStruct = NULL; 1882 | if (m_pJsonData != NULL) 1883 | { 1884 | if (m_pJsonData->type == cJSON_Array) 1885 | { 1886 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 1887 | } 1888 | } 1889 | else if (m_pExternJsonDataRef != NULL) 1890 | { 1891 | if(m_pExternJsonDataRef->type == cJSON_Array) 1892 | { 1893 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 1894 | } 1895 | } 1896 | if (pJsonStruct == NULL) 1897 | { 1898 | return(false); 1899 | } 1900 | if (pJsonStruct->type != cJSON_String) 1901 | { 1902 | return(false); 1903 | } 1904 | strValue = pJsonStruct->valuestring; 1905 | return(true); 1906 | } 1907 | 1908 | bool CJsonObject::Get(int iWhich, int32& iValue) const 1909 | { 1910 | cJSON* pJsonStruct = NULL; 1911 | if (m_pJsonData != NULL) 1912 | { 1913 | if (m_pJsonData->type == cJSON_Array) 1914 | { 1915 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 1916 | } 1917 | } 1918 | else if (m_pExternJsonDataRef != NULL) 1919 | { 1920 | if(m_pExternJsonDataRef->type == cJSON_Array) 1921 | { 1922 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 1923 | } 1924 | } 1925 | if (pJsonStruct == NULL) 1926 | { 1927 | return(false); 1928 | } 1929 | if (pJsonStruct->type == cJSON_Int) 1930 | { 1931 | iValue = (int32)(pJsonStruct->valueint); 1932 | return(true); 1933 | } 1934 | else if (pJsonStruct->type == cJSON_Double) 1935 | { 1936 | iValue = (int32)(pJsonStruct->valuedouble); 1937 | return(true); 1938 | } 1939 | return(false); 1940 | } 1941 | 1942 | bool CJsonObject::Get(int iWhich, uint32& uiValue) const 1943 | { 1944 | cJSON* pJsonStruct = NULL; 1945 | if (m_pJsonData != NULL) 1946 | { 1947 | if (m_pJsonData->type == cJSON_Array) 1948 | { 1949 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 1950 | } 1951 | } 1952 | else if (m_pExternJsonDataRef != NULL) 1953 | { 1954 | if(m_pExternJsonDataRef->type == cJSON_Array) 1955 | { 1956 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 1957 | } 1958 | } 1959 | if (pJsonStruct == NULL) 1960 | { 1961 | return(false); 1962 | } 1963 | if (pJsonStruct->type == cJSON_Int) 1964 | { 1965 | uiValue = (uint32)(pJsonStruct->valueint); 1966 | return(true); 1967 | } 1968 | else if (pJsonStruct->type == cJSON_Double) 1969 | { 1970 | uiValue = (uint32)(pJsonStruct->valuedouble); 1971 | return(true); 1972 | } 1973 | return(false); 1974 | } 1975 | 1976 | bool CJsonObject::Get(int iWhich, int64& llValue) const 1977 | { 1978 | cJSON* pJsonStruct = NULL; 1979 | if (m_pJsonData != NULL) 1980 | { 1981 | if (m_pJsonData->type == cJSON_Array) 1982 | { 1983 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 1984 | } 1985 | } 1986 | else if (m_pExternJsonDataRef != NULL) 1987 | { 1988 | if(m_pExternJsonDataRef->type == cJSON_Array) 1989 | { 1990 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 1991 | } 1992 | } 1993 | if (pJsonStruct == NULL) 1994 | { 1995 | return(false); 1996 | } 1997 | if (pJsonStruct->type == cJSON_Int) 1998 | { 1999 | llValue = (int64)(pJsonStruct->valueint); 2000 | return(true); 2001 | } 2002 | else if (pJsonStruct->type == cJSON_Double) 2003 | { 2004 | llValue = (int64)(pJsonStruct->valuedouble); 2005 | return(true); 2006 | } 2007 | return(false); 2008 | } 2009 | 2010 | bool CJsonObject::Get(int iWhich, uint64& ullValue) const 2011 | { 2012 | cJSON* pJsonStruct = NULL; 2013 | if (m_pJsonData != NULL) 2014 | { 2015 | if (m_pJsonData->type == cJSON_Array) 2016 | { 2017 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 2018 | } 2019 | } 2020 | else if (m_pExternJsonDataRef != NULL) 2021 | { 2022 | if(m_pExternJsonDataRef->type == cJSON_Array) 2023 | { 2024 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 2025 | } 2026 | } 2027 | if (pJsonStruct == NULL) 2028 | { 2029 | return(false); 2030 | } 2031 | if (pJsonStruct->type == cJSON_Int) 2032 | { 2033 | ullValue = (uint64)(pJsonStruct->valueint); 2034 | return(true); 2035 | } 2036 | else if (pJsonStruct->type == cJSON_Double) 2037 | { 2038 | ullValue = (uint64)(pJsonStruct->valuedouble); 2039 | return(true); 2040 | } 2041 | return(false); 2042 | } 2043 | 2044 | bool CJsonObject::Get(int iWhich, bool& bValue) const 2045 | { 2046 | cJSON* pJsonStruct = NULL; 2047 | if (m_pJsonData != NULL) 2048 | { 2049 | if (m_pJsonData->type == cJSON_Array) 2050 | { 2051 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 2052 | } 2053 | } 2054 | else if (m_pExternJsonDataRef != NULL) 2055 | { 2056 | if(m_pExternJsonDataRef->type == cJSON_Array) 2057 | { 2058 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 2059 | } 2060 | } 2061 | if (pJsonStruct == NULL) 2062 | { 2063 | return(false); 2064 | } 2065 | if (pJsonStruct->type > cJSON_True) 2066 | { 2067 | return(false); 2068 | } 2069 | bValue = pJsonStruct->type; 2070 | return(true); 2071 | } 2072 | 2073 | bool CJsonObject::Get(int iWhich, float& fValue) const 2074 | { 2075 | cJSON* pJsonStruct = NULL; 2076 | if (m_pJsonData != NULL) 2077 | { 2078 | if (m_pJsonData->type == cJSON_Array) 2079 | { 2080 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 2081 | } 2082 | } 2083 | else if (m_pExternJsonDataRef != NULL) 2084 | { 2085 | if(m_pExternJsonDataRef->type == cJSON_Array) 2086 | { 2087 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 2088 | } 2089 | } 2090 | if (pJsonStruct == NULL) 2091 | { 2092 | return(false); 2093 | } 2094 | if (pJsonStruct->type == cJSON_Double || pJsonStruct->type == cJSON_Int) 2095 | { 2096 | fValue = (float)(pJsonStruct->valuedouble); 2097 | return(true); 2098 | } 2099 | return(false); 2100 | } 2101 | 2102 | bool CJsonObject::Get(int iWhich, double& dValue) const 2103 | { 2104 | cJSON* pJsonStruct = NULL; 2105 | if (m_pJsonData != NULL) 2106 | { 2107 | if (m_pJsonData->type == cJSON_Array) 2108 | { 2109 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 2110 | } 2111 | } 2112 | else if (m_pExternJsonDataRef != NULL) 2113 | { 2114 | if(m_pExternJsonDataRef->type == cJSON_Array) 2115 | { 2116 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 2117 | } 2118 | } 2119 | if (pJsonStruct == NULL) 2120 | { 2121 | return(false); 2122 | } 2123 | if (pJsonStruct->type == cJSON_Double || pJsonStruct->type == cJSON_Int) 2124 | { 2125 | dValue = pJsonStruct->valuedouble; 2126 | return(true); 2127 | } 2128 | return(false); 2129 | } 2130 | 2131 | bool CJsonObject::IsNull(int iWhich) const 2132 | { 2133 | cJSON* pJsonStruct = NULL; 2134 | if (m_pJsonData != NULL) 2135 | { 2136 | if (m_pJsonData->type == cJSON_Array) 2137 | { 2138 | pJsonStruct = cJSON_GetArrayItem(m_pJsonData, iWhich); 2139 | } 2140 | } 2141 | else if (m_pExternJsonDataRef != NULL) 2142 | { 2143 | if(m_pExternJsonDataRef->type == cJSON_Array) 2144 | { 2145 | pJsonStruct = cJSON_GetArrayItem(m_pExternJsonDataRef, iWhich); 2146 | } 2147 | } 2148 | if (pJsonStruct == NULL) 2149 | { 2150 | return(false); 2151 | } 2152 | if (pJsonStruct->type != cJSON_NULL) 2153 | { 2154 | return(false); 2155 | } 2156 | return(true); 2157 | } 2158 | 2159 | bool CJsonObject::Add(const CJsonObject& oJsonObject) 2160 | { 2161 | cJSON* pFocusData = NULL; 2162 | if (m_pJsonData != NULL) 2163 | { 2164 | pFocusData = m_pJsonData; 2165 | } 2166 | else if (m_pExternJsonDataRef != NULL) 2167 | { 2168 | pFocusData = m_pExternJsonDataRef; 2169 | } 2170 | else 2171 | { 2172 | m_pJsonData = cJSON_CreateArray(); 2173 | pFocusData = m_pJsonData; 2174 | } 2175 | 2176 | if (pFocusData == NULL) 2177 | { 2178 | m_strErrMsg = "json data is null!"; 2179 | return(false); 2180 | } 2181 | if (pFocusData->type != cJSON_Array) 2182 | { 2183 | m_strErrMsg = "not a json array! json object?"; 2184 | return(false); 2185 | } 2186 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str()); 2187 | if (pJsonStruct == NULL) 2188 | { 2189 | m_strErrMsg = std::string("prase json string error at ") + cJSON_GetErrorPtr(); 2190 | return(false); 2191 | } 2192 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2193 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2194 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2195 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2196 | { 2197 | return(false); 2198 | } 2199 | unsigned int uiLastIndex = (unsigned int)cJSON_GetArraySize(pFocusData) - 1; 2200 | for (std::map::iterator iter = m_mapJsonArrayRef.begin(); 2201 | iter != m_mapJsonArrayRef.end(); ) 2202 | { 2203 | if (iter->first >= uiLastIndex) 2204 | { 2205 | if (iter->second != NULL) 2206 | { 2207 | delete (iter->second); 2208 | iter->second = NULL; 2209 | } 2210 | m_mapJsonArrayRef.erase(iter++); 2211 | } 2212 | else 2213 | { 2214 | iter++; 2215 | } 2216 | } 2217 | return(true); 2218 | } 2219 | 2220 | bool CJsonObject::Add(const std::string& strValue) 2221 | { 2222 | cJSON* pFocusData = NULL; 2223 | if (m_pJsonData != NULL) 2224 | { 2225 | pFocusData = m_pJsonData; 2226 | } 2227 | else if (m_pExternJsonDataRef != NULL) 2228 | { 2229 | pFocusData = m_pExternJsonDataRef; 2230 | } 2231 | else 2232 | { 2233 | m_pJsonData = cJSON_CreateArray(); 2234 | pFocusData = m_pJsonData; 2235 | } 2236 | 2237 | if (pFocusData == NULL) 2238 | { 2239 | m_strErrMsg = "json data is null!"; 2240 | return(false); 2241 | } 2242 | if (pFocusData->type != cJSON_Array) 2243 | { 2244 | m_strErrMsg = "not a json array! json object?"; 2245 | return(false); 2246 | } 2247 | cJSON* pJsonStruct = cJSON_CreateString(strValue.c_str()); 2248 | if (pJsonStruct == NULL) 2249 | { 2250 | return(false); 2251 | } 2252 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2253 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2254 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2255 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2256 | { 2257 | return(false); 2258 | } 2259 | return(true); 2260 | } 2261 | 2262 | bool CJsonObject::Add(int32 iValue) 2263 | { 2264 | cJSON* pFocusData = NULL; 2265 | if (m_pJsonData != NULL) 2266 | { 2267 | pFocusData = m_pJsonData; 2268 | } 2269 | else if (m_pExternJsonDataRef != NULL) 2270 | { 2271 | pFocusData = m_pExternJsonDataRef; 2272 | } 2273 | else 2274 | { 2275 | m_pJsonData = cJSON_CreateArray(); 2276 | pFocusData = m_pJsonData; 2277 | } 2278 | 2279 | if (pFocusData == NULL) 2280 | { 2281 | m_strErrMsg = "json data is null!"; 2282 | return(false); 2283 | } 2284 | if (pFocusData->type != cJSON_Array) 2285 | { 2286 | m_strErrMsg = "not a json array! json object?"; 2287 | return(false); 2288 | } 2289 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)iValue, -1); 2290 | if (pJsonStruct == NULL) 2291 | { 2292 | return(false); 2293 | } 2294 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2295 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2296 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2297 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2298 | { 2299 | return(false); 2300 | } 2301 | return(true); 2302 | } 2303 | 2304 | bool CJsonObject::Add(uint32 uiValue) 2305 | { 2306 | cJSON* pFocusData = NULL; 2307 | if (m_pJsonData != NULL) 2308 | { 2309 | pFocusData = m_pJsonData; 2310 | } 2311 | else if (m_pExternJsonDataRef != NULL) 2312 | { 2313 | pFocusData = m_pExternJsonDataRef; 2314 | } 2315 | else 2316 | { 2317 | m_pJsonData = cJSON_CreateArray(); 2318 | pFocusData = m_pJsonData; 2319 | } 2320 | 2321 | if (pFocusData == NULL) 2322 | { 2323 | m_strErrMsg = "json data is null!"; 2324 | return(false); 2325 | } 2326 | if (pFocusData->type != cJSON_Array) 2327 | { 2328 | m_strErrMsg = "not a json array! json object?"; 2329 | return(false); 2330 | } 2331 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)uiValue, 1); 2332 | if (pJsonStruct == NULL) 2333 | { 2334 | return(false); 2335 | } 2336 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2337 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2338 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2339 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2340 | { 2341 | return(false); 2342 | } 2343 | return(true); 2344 | } 2345 | 2346 | bool CJsonObject::Add(int64 llValue) 2347 | { 2348 | cJSON* pFocusData = NULL; 2349 | if (m_pJsonData != NULL) 2350 | { 2351 | pFocusData = m_pJsonData; 2352 | } 2353 | else if (m_pExternJsonDataRef != NULL) 2354 | { 2355 | pFocusData = m_pExternJsonDataRef; 2356 | } 2357 | else 2358 | { 2359 | m_pJsonData = cJSON_CreateArray(); 2360 | pFocusData = m_pJsonData; 2361 | } 2362 | 2363 | if (pFocusData == NULL) 2364 | { 2365 | m_strErrMsg = "json data is null!"; 2366 | return(false); 2367 | } 2368 | if (pFocusData->type != cJSON_Array) 2369 | { 2370 | m_strErrMsg = "not a json array! json object?"; 2371 | return(false); 2372 | } 2373 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)llValue, -1); 2374 | if (pJsonStruct == NULL) 2375 | { 2376 | return(false); 2377 | } 2378 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2379 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2380 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2381 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2382 | { 2383 | return(false); 2384 | } 2385 | return(true); 2386 | } 2387 | 2388 | bool CJsonObject::Add(uint64 ullValue) 2389 | { 2390 | cJSON* pFocusData = NULL; 2391 | if (m_pJsonData != NULL) 2392 | { 2393 | pFocusData = m_pJsonData; 2394 | } 2395 | else if (m_pExternJsonDataRef != NULL) 2396 | { 2397 | pFocusData = m_pExternJsonDataRef; 2398 | } 2399 | else 2400 | { 2401 | m_pJsonData = cJSON_CreateArray(); 2402 | pFocusData = m_pJsonData; 2403 | } 2404 | 2405 | if (pFocusData == NULL) 2406 | { 2407 | m_strErrMsg = "json data is null!"; 2408 | return(false); 2409 | } 2410 | if (pFocusData->type != cJSON_Array) 2411 | { 2412 | m_strErrMsg = "not a json array! json object?"; 2413 | return(false); 2414 | } 2415 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)ullValue, 1); 2416 | if (pJsonStruct == NULL) 2417 | { 2418 | return(false); 2419 | } 2420 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2421 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2422 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2423 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2424 | { 2425 | return(false); 2426 | } 2427 | return(true); 2428 | } 2429 | 2430 | bool CJsonObject::Add(int iAnywhere, bool bValue) 2431 | { 2432 | cJSON* pFocusData = NULL; 2433 | if (m_pJsonData != NULL) 2434 | { 2435 | pFocusData = m_pJsonData; 2436 | } 2437 | else if (m_pExternJsonDataRef != NULL) 2438 | { 2439 | pFocusData = m_pExternJsonDataRef; 2440 | } 2441 | else 2442 | { 2443 | m_pJsonData = cJSON_CreateArray(); 2444 | pFocusData = m_pJsonData; 2445 | } 2446 | 2447 | if (pFocusData == NULL) 2448 | { 2449 | m_strErrMsg = "json data is null!"; 2450 | return(false); 2451 | } 2452 | if (pFocusData->type != cJSON_Array) 2453 | { 2454 | m_strErrMsg = "not a json array! json object?"; 2455 | return(false); 2456 | } 2457 | cJSON* pJsonStruct = cJSON_CreateBool(bValue); 2458 | if (pJsonStruct == NULL) 2459 | { 2460 | return(false); 2461 | } 2462 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2463 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2464 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2465 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2466 | { 2467 | return(false); 2468 | } 2469 | return(true); 2470 | } 2471 | 2472 | bool CJsonObject::Add(float fValue) 2473 | { 2474 | cJSON* pFocusData = NULL; 2475 | if (m_pJsonData != NULL) 2476 | { 2477 | pFocusData = m_pJsonData; 2478 | } 2479 | else if (m_pExternJsonDataRef != NULL) 2480 | { 2481 | pFocusData = m_pExternJsonDataRef; 2482 | } 2483 | else 2484 | { 2485 | m_pJsonData = cJSON_CreateArray(); 2486 | pFocusData = m_pJsonData; 2487 | } 2488 | 2489 | if (pFocusData == NULL) 2490 | { 2491 | m_strErrMsg = "json data is null!"; 2492 | return(false); 2493 | } 2494 | if (pFocusData->type != cJSON_Array) 2495 | { 2496 | m_strErrMsg = "not a json array! json object?"; 2497 | return(false); 2498 | } 2499 | cJSON* pJsonStruct = cJSON_CreateDouble((double)fValue, -1); 2500 | if (pJsonStruct == NULL) 2501 | { 2502 | return(false); 2503 | } 2504 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2505 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2506 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2507 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2508 | { 2509 | return(false); 2510 | } 2511 | return(true); 2512 | } 2513 | 2514 | bool CJsonObject::Add(double dValue) 2515 | { 2516 | cJSON* pFocusData = NULL; 2517 | if (m_pJsonData != NULL) 2518 | { 2519 | pFocusData = m_pJsonData; 2520 | } 2521 | else if (m_pExternJsonDataRef != NULL) 2522 | { 2523 | pFocusData = m_pExternJsonDataRef; 2524 | } 2525 | else 2526 | { 2527 | m_pJsonData = cJSON_CreateArray(); 2528 | pFocusData = m_pJsonData; 2529 | } 2530 | 2531 | if (pFocusData == NULL) 2532 | { 2533 | m_strErrMsg = "json data is null!"; 2534 | return(false); 2535 | } 2536 | if (pFocusData->type != cJSON_Array) 2537 | { 2538 | m_strErrMsg = "not a json array! json object?"; 2539 | return(false); 2540 | } 2541 | cJSON* pJsonStruct = cJSON_CreateDouble((double)dValue, -1); 2542 | if (pJsonStruct == NULL) 2543 | { 2544 | return(false); 2545 | } 2546 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2547 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2548 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2549 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2550 | { 2551 | return(false); 2552 | } 2553 | return(true); 2554 | } 2555 | 2556 | bool CJsonObject::AddNull() 2557 | { 2558 | cJSON* pFocusData = NULL; 2559 | if (m_pJsonData != NULL) 2560 | { 2561 | pFocusData = m_pJsonData; 2562 | } 2563 | else if (m_pExternJsonDataRef != NULL) 2564 | { 2565 | pFocusData = m_pExternJsonDataRef; 2566 | } 2567 | else 2568 | { 2569 | m_pJsonData = cJSON_CreateArray(); 2570 | pFocusData = m_pJsonData; 2571 | } 2572 | 2573 | if (pFocusData == NULL) 2574 | { 2575 | m_strErrMsg = "json data is null!"; 2576 | return(false); 2577 | } 2578 | if (pFocusData->type != cJSON_Array) 2579 | { 2580 | m_strErrMsg = "not a json array! json object?"; 2581 | return(false); 2582 | } 2583 | cJSON* pJsonStruct = cJSON_CreateNull(); 2584 | if (pJsonStruct == NULL) 2585 | { 2586 | return(false); 2587 | } 2588 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2589 | cJSON_AddItemToArray(pFocusData, pJsonStruct); 2590 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2591 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2592 | { 2593 | return(false); 2594 | } 2595 | return(true); 2596 | } 2597 | 2598 | bool CJsonObject::AddAsFirst(const CJsonObject& oJsonObject) 2599 | { 2600 | cJSON* pFocusData = NULL; 2601 | if (m_pJsonData != NULL) 2602 | { 2603 | pFocusData = m_pJsonData; 2604 | } 2605 | else if (m_pExternJsonDataRef != NULL) 2606 | { 2607 | pFocusData = m_pExternJsonDataRef; 2608 | } 2609 | else 2610 | { 2611 | m_pJsonData = cJSON_CreateArray(); 2612 | pFocusData = m_pJsonData; 2613 | } 2614 | 2615 | if (pFocusData == NULL) 2616 | { 2617 | m_strErrMsg = "json data is null!"; 2618 | return(false); 2619 | } 2620 | if (pFocusData->type != cJSON_Array) 2621 | { 2622 | m_strErrMsg = "not a json array! json object?"; 2623 | return(false); 2624 | } 2625 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str()); 2626 | if (pJsonStruct == NULL) 2627 | { 2628 | m_strErrMsg = std::string("prase json string error at ") + cJSON_GetErrorPtr(); 2629 | return(false); 2630 | } 2631 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2632 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2633 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2634 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2635 | { 2636 | return(false); 2637 | } 2638 | for (std::map::iterator iter = m_mapJsonArrayRef.begin(); 2639 | iter != m_mapJsonArrayRef.end(); ) 2640 | { 2641 | if (iter->second != NULL) 2642 | { 2643 | delete (iter->second); 2644 | iter->second = NULL; 2645 | } 2646 | m_mapJsonArrayRef.erase(iter++); 2647 | } 2648 | return(true); 2649 | } 2650 | 2651 | bool CJsonObject::AddAsFirst(const std::string& strValue) 2652 | { 2653 | cJSON* pFocusData = NULL; 2654 | if (m_pJsonData != NULL) 2655 | { 2656 | pFocusData = m_pJsonData; 2657 | } 2658 | else if (m_pExternJsonDataRef != NULL) 2659 | { 2660 | pFocusData = m_pExternJsonDataRef; 2661 | } 2662 | else 2663 | { 2664 | m_pJsonData = cJSON_CreateArray(); 2665 | pFocusData = m_pJsonData; 2666 | } 2667 | 2668 | if (pFocusData == NULL) 2669 | { 2670 | m_strErrMsg = "json data is null!"; 2671 | return(false); 2672 | } 2673 | if (pFocusData->type != cJSON_Array) 2674 | { 2675 | m_strErrMsg = "not a json array! json object?"; 2676 | return(false); 2677 | } 2678 | cJSON* pJsonStruct = cJSON_CreateString(strValue.c_str()); 2679 | if (pJsonStruct == NULL) 2680 | { 2681 | return(false); 2682 | } 2683 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2684 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2685 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2686 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2687 | { 2688 | return(false); 2689 | } 2690 | return(true); 2691 | } 2692 | 2693 | bool CJsonObject::AddAsFirst(int32 iValue) 2694 | { 2695 | cJSON* pFocusData = NULL; 2696 | if (m_pJsonData != NULL) 2697 | { 2698 | pFocusData = m_pJsonData; 2699 | } 2700 | else if (m_pExternJsonDataRef != NULL) 2701 | { 2702 | pFocusData = m_pExternJsonDataRef; 2703 | } 2704 | else 2705 | { 2706 | m_pJsonData = cJSON_CreateArray(); 2707 | pFocusData = m_pJsonData; 2708 | } 2709 | 2710 | if (pFocusData == NULL) 2711 | { 2712 | m_strErrMsg = "json data is null!"; 2713 | return(false); 2714 | } 2715 | if (pFocusData->type != cJSON_Array) 2716 | { 2717 | m_strErrMsg = "not a json array! json object?"; 2718 | return(false); 2719 | } 2720 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)iValue, -1); 2721 | if (pJsonStruct == NULL) 2722 | { 2723 | return(false); 2724 | } 2725 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2726 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2727 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2728 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2729 | { 2730 | return(false); 2731 | } 2732 | return(true); 2733 | } 2734 | 2735 | bool CJsonObject::AddAsFirst(uint32 uiValue) 2736 | { 2737 | cJSON* pFocusData = NULL; 2738 | if (m_pJsonData != NULL) 2739 | { 2740 | pFocusData = m_pJsonData; 2741 | } 2742 | else if (m_pExternJsonDataRef != NULL) 2743 | { 2744 | pFocusData = m_pExternJsonDataRef; 2745 | } 2746 | else 2747 | { 2748 | m_pJsonData = cJSON_CreateArray(); 2749 | pFocusData = m_pJsonData; 2750 | } 2751 | 2752 | if (pFocusData == NULL) 2753 | { 2754 | m_strErrMsg = "json data is null!"; 2755 | return(false); 2756 | } 2757 | if (pFocusData->type != cJSON_Array) 2758 | { 2759 | m_strErrMsg = "not a json array! json object?"; 2760 | return(false); 2761 | } 2762 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)uiValue, -1); 2763 | if (pJsonStruct == NULL) 2764 | { 2765 | return(false); 2766 | } 2767 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2768 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2769 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2770 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2771 | { 2772 | return(false); 2773 | } 2774 | return(true); 2775 | } 2776 | 2777 | bool CJsonObject::AddAsFirst(int64 llValue) 2778 | { 2779 | cJSON* pFocusData = NULL; 2780 | if (m_pJsonData != NULL) 2781 | { 2782 | pFocusData = m_pJsonData; 2783 | } 2784 | else if (m_pExternJsonDataRef != NULL) 2785 | { 2786 | pFocusData = m_pExternJsonDataRef; 2787 | } 2788 | else 2789 | { 2790 | m_pJsonData = cJSON_CreateArray(); 2791 | pFocusData = m_pJsonData; 2792 | } 2793 | 2794 | if (pFocusData == NULL) 2795 | { 2796 | m_strErrMsg = "json data is null!"; 2797 | return(false); 2798 | } 2799 | if (pFocusData->type != cJSON_Array) 2800 | { 2801 | m_strErrMsg = "not a json array! json object?"; 2802 | return(false); 2803 | } 2804 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)llValue, -1); 2805 | if (pJsonStruct == NULL) 2806 | { 2807 | return(false); 2808 | } 2809 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2810 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2811 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2812 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2813 | { 2814 | return(false); 2815 | } 2816 | return(true); 2817 | } 2818 | 2819 | bool CJsonObject::AddAsFirst(uint64 ullValue) 2820 | { 2821 | cJSON* pFocusData = NULL; 2822 | if (m_pJsonData != NULL) 2823 | { 2824 | pFocusData = m_pJsonData; 2825 | } 2826 | else if (m_pExternJsonDataRef != NULL) 2827 | { 2828 | pFocusData = m_pExternJsonDataRef; 2829 | } 2830 | else 2831 | { 2832 | m_pJsonData = cJSON_CreateArray(); 2833 | pFocusData = m_pJsonData; 2834 | } 2835 | 2836 | if (pFocusData == NULL) 2837 | { 2838 | m_strErrMsg = "json data is null!"; 2839 | return(false); 2840 | } 2841 | if (pFocusData->type != cJSON_Array) 2842 | { 2843 | m_strErrMsg = "not a json array! json object?"; 2844 | return(false); 2845 | } 2846 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)ullValue, -1); 2847 | if (pJsonStruct == NULL) 2848 | { 2849 | return(false); 2850 | } 2851 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2852 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2853 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2854 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2855 | { 2856 | return(false); 2857 | } 2858 | return(true); 2859 | } 2860 | 2861 | bool CJsonObject::AddAsFirst(int iAnywhere, bool bValue) 2862 | { 2863 | cJSON* pFocusData = NULL; 2864 | if (m_pJsonData != NULL) 2865 | { 2866 | pFocusData = m_pJsonData; 2867 | } 2868 | else if (m_pExternJsonDataRef != NULL) 2869 | { 2870 | pFocusData = m_pExternJsonDataRef; 2871 | } 2872 | else 2873 | { 2874 | m_pJsonData = cJSON_CreateArray(); 2875 | pFocusData = m_pJsonData; 2876 | } 2877 | 2878 | if (pFocusData == NULL) 2879 | { 2880 | m_strErrMsg = "json data is null!"; 2881 | return(false); 2882 | } 2883 | if (pFocusData->type != cJSON_Array) 2884 | { 2885 | m_strErrMsg = "not a json array! json object?"; 2886 | return(false); 2887 | } 2888 | cJSON* pJsonStruct = cJSON_CreateBool(bValue); 2889 | if (pJsonStruct == NULL) 2890 | { 2891 | return(false); 2892 | } 2893 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2894 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2895 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2896 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2897 | { 2898 | return(false); 2899 | } 2900 | return(true); 2901 | } 2902 | 2903 | bool CJsonObject::AddAsFirst(float fValue) 2904 | { 2905 | cJSON* pFocusData = NULL; 2906 | if (m_pJsonData != NULL) 2907 | { 2908 | pFocusData = m_pJsonData; 2909 | } 2910 | else if (m_pExternJsonDataRef != NULL) 2911 | { 2912 | pFocusData = m_pExternJsonDataRef; 2913 | } 2914 | else 2915 | { 2916 | m_pJsonData = cJSON_CreateArray(); 2917 | pFocusData = m_pJsonData; 2918 | } 2919 | 2920 | if (pFocusData == NULL) 2921 | { 2922 | m_strErrMsg = "json data is null!"; 2923 | return(false); 2924 | } 2925 | if (pFocusData->type != cJSON_Array) 2926 | { 2927 | m_strErrMsg = "not a json array! json object?"; 2928 | return(false); 2929 | } 2930 | cJSON* pJsonStruct = cJSON_CreateDouble((double)fValue, -1); 2931 | if (pJsonStruct == NULL) 2932 | { 2933 | return(false); 2934 | } 2935 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2936 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2937 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2938 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2939 | { 2940 | return(false); 2941 | } 2942 | return(true); 2943 | } 2944 | 2945 | bool CJsonObject::AddAsFirst(double dValue) 2946 | { 2947 | cJSON* pFocusData = NULL; 2948 | if (m_pJsonData != NULL) 2949 | { 2950 | pFocusData = m_pJsonData; 2951 | } 2952 | else if (m_pExternJsonDataRef != NULL) 2953 | { 2954 | pFocusData = m_pExternJsonDataRef; 2955 | } 2956 | else 2957 | { 2958 | m_pJsonData = cJSON_CreateArray(); 2959 | pFocusData = m_pJsonData; 2960 | } 2961 | 2962 | if (pFocusData == NULL) 2963 | { 2964 | m_strErrMsg = "json data is null!"; 2965 | return(false); 2966 | } 2967 | if (pFocusData->type != cJSON_Array) 2968 | { 2969 | m_strErrMsg = "not a json array! json object?"; 2970 | return(false); 2971 | } 2972 | cJSON* pJsonStruct = cJSON_CreateDouble((double)dValue, -1); 2973 | if (pJsonStruct == NULL) 2974 | { 2975 | return(false); 2976 | } 2977 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 2978 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 2979 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 2980 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 2981 | { 2982 | return(false); 2983 | } 2984 | return(true); 2985 | } 2986 | 2987 | bool CJsonObject::AddNullAsFirst() 2988 | { 2989 | cJSON* pFocusData = NULL; 2990 | if (m_pJsonData != NULL) 2991 | { 2992 | pFocusData = m_pJsonData; 2993 | } 2994 | else if (m_pExternJsonDataRef != NULL) 2995 | { 2996 | pFocusData = m_pExternJsonDataRef; 2997 | } 2998 | else 2999 | { 3000 | m_pJsonData = cJSON_CreateArray(); 3001 | pFocusData = m_pJsonData; 3002 | } 3003 | 3004 | if (pFocusData == NULL) 3005 | { 3006 | m_strErrMsg = "json data is null!"; 3007 | return(false); 3008 | } 3009 | if (pFocusData->type != cJSON_Array) 3010 | { 3011 | m_strErrMsg = "not a json array! json object?"; 3012 | return(false); 3013 | } 3014 | cJSON* pJsonStruct = cJSON_CreateNull(); 3015 | if (pJsonStruct == NULL) 3016 | { 3017 | return(false); 3018 | } 3019 | int iArraySizeBeforeAdd = cJSON_GetArraySize(pFocusData); 3020 | cJSON_AddItemToArrayHead(pFocusData, pJsonStruct); 3021 | int iArraySizeAfterAdd = cJSON_GetArraySize(pFocusData); 3022 | if (iArraySizeAfterAdd == iArraySizeBeforeAdd) 3023 | { 3024 | return(false); 3025 | } 3026 | return(true); 3027 | } 3028 | 3029 | bool CJsonObject::Delete(int iWhich) 3030 | { 3031 | cJSON* pFocusData = NULL; 3032 | if (m_pJsonData == NULL) 3033 | { 3034 | pFocusData = m_pExternJsonDataRef; 3035 | } 3036 | else 3037 | { 3038 | pFocusData = m_pJsonData; 3039 | } 3040 | if (pFocusData == NULL) 3041 | { 3042 | m_strErrMsg = "json data is null!"; 3043 | return(false); 3044 | } 3045 | if (pFocusData->type != cJSON_Array) 3046 | { 3047 | m_strErrMsg = "not a json array! json object?"; 3048 | return(false); 3049 | } 3050 | cJSON_DeleteItemFromArray(pFocusData, iWhich); 3051 | for (std::map::iterator iter = m_mapJsonArrayRef.begin(); 3052 | iter != m_mapJsonArrayRef.end(); ) 3053 | { 3054 | if (iter->first >= (unsigned int)iWhich) 3055 | { 3056 | if (iter->second != NULL) 3057 | { 3058 | delete (iter->second); 3059 | iter->second = NULL; 3060 | } 3061 | m_mapJsonArrayRef.erase(iter++); 3062 | } 3063 | else 3064 | { 3065 | iter++; 3066 | } 3067 | } 3068 | return(true); 3069 | } 3070 | 3071 | bool CJsonObject::Replace(int iWhich, const CJsonObject& oJsonObject) 3072 | { 3073 | cJSON* pFocusData = NULL; 3074 | if (m_pJsonData == NULL) 3075 | { 3076 | pFocusData = m_pExternJsonDataRef; 3077 | } 3078 | else 3079 | { 3080 | pFocusData = m_pJsonData; 3081 | } 3082 | if (pFocusData == NULL) 3083 | { 3084 | m_strErrMsg = "json data is null!"; 3085 | return(false); 3086 | } 3087 | if (pFocusData->type != cJSON_Array) 3088 | { 3089 | m_strErrMsg = "not a json array! json object?"; 3090 | return(false); 3091 | } 3092 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str()); 3093 | if (pJsonStruct == NULL) 3094 | { 3095 | m_strErrMsg = std::string("prase json string error at ") + cJSON_GetErrorPtr(); 3096 | return(false); 3097 | } 3098 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3099 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3100 | { 3101 | return(false); 3102 | } 3103 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3104 | if (iter != m_mapJsonArrayRef.end()) 3105 | { 3106 | if (iter->second != NULL) 3107 | { 3108 | delete (iter->second); 3109 | iter->second = NULL; 3110 | } 3111 | m_mapJsonArrayRef.erase(iter); 3112 | } 3113 | return(true); 3114 | } 3115 | 3116 | bool CJsonObject::Replace(int iWhich, const std::string& strValue) 3117 | { 3118 | cJSON* pFocusData = NULL; 3119 | if (m_pJsonData == NULL) 3120 | { 3121 | pFocusData = m_pExternJsonDataRef; 3122 | } 3123 | else 3124 | { 3125 | pFocusData = m_pJsonData; 3126 | } 3127 | if (pFocusData == NULL) 3128 | { 3129 | m_strErrMsg = "json data is null!"; 3130 | return(false); 3131 | } 3132 | if (pFocusData->type != cJSON_Array) 3133 | { 3134 | m_strErrMsg = "not a json array! json object?"; 3135 | return(false); 3136 | } 3137 | cJSON* pJsonStruct = cJSON_CreateString(strValue.c_str()); 3138 | if (pJsonStruct == NULL) 3139 | { 3140 | return(false); 3141 | } 3142 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3143 | if (iter != m_mapJsonArrayRef.end()) 3144 | { 3145 | if (iter->second != NULL) 3146 | { 3147 | delete (iter->second); 3148 | iter->second = NULL; 3149 | } 3150 | m_mapJsonArrayRef.erase(iter); 3151 | } 3152 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3153 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3154 | { 3155 | return(false); 3156 | } 3157 | return(true); 3158 | } 3159 | 3160 | bool CJsonObject::Replace(int iWhich, int32 iValue) 3161 | { 3162 | cJSON* pFocusData = NULL; 3163 | if (m_pJsonData == NULL) 3164 | { 3165 | pFocusData = m_pExternJsonDataRef; 3166 | } 3167 | else 3168 | { 3169 | pFocusData = m_pJsonData; 3170 | } 3171 | if (pFocusData == NULL) 3172 | { 3173 | m_strErrMsg = "json data is null!"; 3174 | return(false); 3175 | } 3176 | if (pFocusData->type != cJSON_Array) 3177 | { 3178 | m_strErrMsg = "not a json array! json object?"; 3179 | return(false); 3180 | } 3181 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)iValue, -1); 3182 | if (pJsonStruct == NULL) 3183 | { 3184 | return(false); 3185 | } 3186 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3187 | if (iter != m_mapJsonArrayRef.end()) 3188 | { 3189 | if (iter->second != NULL) 3190 | { 3191 | delete (iter->second); 3192 | iter->second = NULL; 3193 | } 3194 | m_mapJsonArrayRef.erase(iter); 3195 | } 3196 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3197 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3198 | { 3199 | return(false); 3200 | } 3201 | return(true); 3202 | } 3203 | 3204 | bool CJsonObject::Replace(int iWhich, uint32 uiValue) 3205 | { 3206 | cJSON* pFocusData = NULL; 3207 | if (m_pJsonData == NULL) 3208 | { 3209 | pFocusData = m_pExternJsonDataRef; 3210 | } 3211 | else 3212 | { 3213 | pFocusData = m_pJsonData; 3214 | } 3215 | if (pFocusData == NULL) 3216 | { 3217 | m_strErrMsg = "json data is null!"; 3218 | return(false); 3219 | } 3220 | if (pFocusData->type != cJSON_Array) 3221 | { 3222 | m_strErrMsg = "not a json array! json object?"; 3223 | return(false); 3224 | } 3225 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)uiValue, 1); 3226 | if (pJsonStruct == NULL) 3227 | { 3228 | return(false); 3229 | } 3230 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3231 | if (iter != m_mapJsonArrayRef.end()) 3232 | { 3233 | if (iter->second != NULL) 3234 | { 3235 | delete (iter->second); 3236 | iter->second = NULL; 3237 | } 3238 | m_mapJsonArrayRef.erase(iter); 3239 | } 3240 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3241 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3242 | { 3243 | return(false); 3244 | } 3245 | return(true); 3246 | } 3247 | 3248 | bool CJsonObject::Replace(int iWhich, int64 llValue) 3249 | { 3250 | cJSON* pFocusData = NULL; 3251 | if (m_pJsonData == NULL) 3252 | { 3253 | pFocusData = m_pExternJsonDataRef; 3254 | } 3255 | else 3256 | { 3257 | pFocusData = m_pJsonData; 3258 | } 3259 | if (pFocusData == NULL) 3260 | { 3261 | m_strErrMsg = "json data is null!"; 3262 | return(false); 3263 | } 3264 | if (pFocusData->type != cJSON_Array) 3265 | { 3266 | m_strErrMsg = "not a json array! json object?"; 3267 | return(false); 3268 | } 3269 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)((uint64)llValue), -1); 3270 | if (pJsonStruct == NULL) 3271 | { 3272 | return(false); 3273 | } 3274 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3275 | if (iter != m_mapJsonArrayRef.end()) 3276 | { 3277 | if (iter->second != NULL) 3278 | { 3279 | delete (iter->second); 3280 | iter->second = NULL; 3281 | } 3282 | m_mapJsonArrayRef.erase(iter); 3283 | } 3284 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3285 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3286 | { 3287 | return(false); 3288 | } 3289 | return(true); 3290 | } 3291 | 3292 | bool CJsonObject::Replace(int iWhich, uint64 ullValue) 3293 | { 3294 | cJSON* pFocusData = NULL; 3295 | if (m_pJsonData == NULL) 3296 | { 3297 | pFocusData = m_pExternJsonDataRef; 3298 | } 3299 | else 3300 | { 3301 | pFocusData = m_pJsonData; 3302 | } 3303 | if (pFocusData == NULL) 3304 | { 3305 | m_strErrMsg = "json data is null!"; 3306 | return(false); 3307 | } 3308 | if (pFocusData->type != cJSON_Array) 3309 | { 3310 | m_strErrMsg = "not a json array! json object?"; 3311 | return(false); 3312 | } 3313 | cJSON* pJsonStruct = cJSON_CreateInt((uint64)ullValue, 1); 3314 | if (pJsonStruct == NULL) 3315 | { 3316 | return(false); 3317 | } 3318 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3319 | if (iter != m_mapJsonArrayRef.end()) 3320 | { 3321 | if (iter->second != NULL) 3322 | { 3323 | delete (iter->second); 3324 | iter->second = NULL; 3325 | } 3326 | m_mapJsonArrayRef.erase(iter); 3327 | } 3328 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3329 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3330 | { 3331 | return(false); 3332 | } 3333 | return(true); 3334 | } 3335 | 3336 | bool CJsonObject::Replace(int iWhich, bool bValue, bool bValueAgain) 3337 | { 3338 | cJSON* pFocusData = NULL; 3339 | if (m_pJsonData == NULL) 3340 | { 3341 | pFocusData = m_pExternJsonDataRef; 3342 | } 3343 | else 3344 | { 3345 | pFocusData = m_pJsonData; 3346 | } 3347 | if (pFocusData == NULL) 3348 | { 3349 | m_strErrMsg = "json data is null!"; 3350 | return(false); 3351 | } 3352 | if (pFocusData->type != cJSON_Array) 3353 | { 3354 | m_strErrMsg = "not a json array! json object?"; 3355 | return(false); 3356 | } 3357 | cJSON* pJsonStruct = cJSON_CreateBool(bValue); 3358 | if (pJsonStruct == NULL) 3359 | { 3360 | return(false); 3361 | } 3362 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3363 | if (iter != m_mapJsonArrayRef.end()) 3364 | { 3365 | if (iter->second != NULL) 3366 | { 3367 | delete (iter->second); 3368 | iter->second = NULL; 3369 | } 3370 | m_mapJsonArrayRef.erase(iter); 3371 | } 3372 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3373 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3374 | { 3375 | return(false); 3376 | } 3377 | return(true); 3378 | } 3379 | 3380 | bool CJsonObject::Replace(int iWhich, float fValue) 3381 | { 3382 | cJSON* pFocusData = NULL; 3383 | if (m_pJsonData == NULL) 3384 | { 3385 | pFocusData = m_pExternJsonDataRef; 3386 | } 3387 | else 3388 | { 3389 | pFocusData = m_pJsonData; 3390 | } 3391 | if (pFocusData == NULL) 3392 | { 3393 | m_strErrMsg = "json data is null!"; 3394 | return(false); 3395 | } 3396 | if (pFocusData->type != cJSON_Array) 3397 | { 3398 | m_strErrMsg = "not a json array! json object?"; 3399 | return(false); 3400 | } 3401 | cJSON* pJsonStruct = cJSON_CreateDouble((double)fValue, -1); 3402 | if (pJsonStruct == NULL) 3403 | { 3404 | return(false); 3405 | } 3406 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3407 | if (iter != m_mapJsonArrayRef.end()) 3408 | { 3409 | if (iter->second != NULL) 3410 | { 3411 | delete (iter->second); 3412 | iter->second = NULL; 3413 | } 3414 | m_mapJsonArrayRef.erase(iter); 3415 | } 3416 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3417 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3418 | { 3419 | return(false); 3420 | } 3421 | return(true); 3422 | } 3423 | 3424 | bool CJsonObject::Replace(int iWhich, double dValue) 3425 | { 3426 | cJSON* pFocusData = NULL; 3427 | if (m_pJsonData == NULL) 3428 | { 3429 | pFocusData = m_pExternJsonDataRef; 3430 | } 3431 | else 3432 | { 3433 | pFocusData = m_pJsonData; 3434 | } 3435 | if (pFocusData == NULL) 3436 | { 3437 | m_strErrMsg = "json data is null!"; 3438 | return(false); 3439 | } 3440 | if (pFocusData->type != cJSON_Array) 3441 | { 3442 | m_strErrMsg = "not a json array! json object?"; 3443 | return(false); 3444 | } 3445 | cJSON* pJsonStruct = cJSON_CreateDouble((double)dValue, -1); 3446 | if (pJsonStruct == NULL) 3447 | { 3448 | return(false); 3449 | } 3450 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3451 | if (iter != m_mapJsonArrayRef.end()) 3452 | { 3453 | if (iter->second != NULL) 3454 | { 3455 | delete (iter->second); 3456 | iter->second = NULL; 3457 | } 3458 | m_mapJsonArrayRef.erase(iter); 3459 | } 3460 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3461 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3462 | { 3463 | return(false); 3464 | } 3465 | return(true); 3466 | } 3467 | 3468 | bool CJsonObject::ReplaceWithNull(int iWhich) 3469 | { 3470 | cJSON* pFocusData = NULL; 3471 | if (m_pJsonData == NULL) 3472 | { 3473 | pFocusData = m_pExternJsonDataRef; 3474 | } 3475 | else 3476 | { 3477 | pFocusData = m_pJsonData; 3478 | } 3479 | if (pFocusData == NULL) 3480 | { 3481 | m_strErrMsg = "json data is null!"; 3482 | return(false); 3483 | } 3484 | if (pFocusData->type != cJSON_Array) 3485 | { 3486 | m_strErrMsg = "not a json array! json object?"; 3487 | return(false); 3488 | } 3489 | cJSON* pJsonStruct = cJSON_CreateNull(); 3490 | if (pJsonStruct == NULL) 3491 | { 3492 | return(false); 3493 | } 3494 | std::map::iterator iter = m_mapJsonArrayRef.find(iWhich); 3495 | if (iter != m_mapJsonArrayRef.end()) 3496 | { 3497 | if (iter->second != NULL) 3498 | { 3499 | delete (iter->second); 3500 | iter->second = NULL; 3501 | } 3502 | m_mapJsonArrayRef.erase(iter); 3503 | } 3504 | cJSON_ReplaceItemInArray(pFocusData, iWhich, pJsonStruct); 3505 | if (cJSON_GetArrayItem(pFocusData, iWhich) == NULL) 3506 | { 3507 | return(false); 3508 | } 3509 | return(true); 3510 | } 3511 | 3512 | CJsonObject::CJsonObject(cJSON* pJsonData) 3513 | : m_pJsonData(NULL), m_pExternJsonDataRef(pJsonData), m_pKeyTravers(pJsonData) 3514 | { 3515 | } 3516 | 3517 | } 3518 | 3519 | 3520 | -------------------------------------------------------------------------------- /CJsonObject.hpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Project: neb 3 | * @file CJsonObject.hpp 4 | * @brief Json 5 | * @author bwarliao 6 | * @date: 2014-7-16 7 | * @note 8 | * Modify history: 9 | ******************************************************************************/ 10 | 11 | #ifndef CJSONOBJECT_HPP_ 12 | #define CJSONOBJECT_HPP_ 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | #include "cJSON.h" 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | 34 | namespace requests 35 | { 36 | 37 | class CJsonObject 38 | { 39 | public: // method of ordinary json object or json array 40 | CJsonObject(); 41 | CJsonObject(const std::string& strJson); 42 | CJsonObject(const CJsonObject* pJsonObject); 43 | CJsonObject(const CJsonObject& oJsonObject); 44 | virtual ~CJsonObject(); 45 | 46 | CJsonObject& operator=(const CJsonObject& oJsonObject); 47 | bool operator==(const CJsonObject& oJsonObject) const; 48 | bool Parse(const std::string& strJson); 49 | void Clear(); 50 | bool IsEmpty() const; 51 | bool IsArray() const; 52 | std::string ToString() const; 53 | std::string ToFormattedString() const; 54 | const std::string& GetErrMsg() const 55 | { 56 | return(m_strErrMsg); 57 | } 58 | 59 | public: // method of ordinary json object 60 | bool AddEmptySubObject(const std::string& strKey); 61 | bool AddEmptySubArray(const std::string& strKey); 62 | bool GetKey(std::string& strKey); 63 | void ResetTraversing(); 64 | CJsonObject& operator[](const std::string& strKey); 65 | std::string operator()(const std::string& strKey) const; 66 | bool Get(const std::string& strKey, CJsonObject& oJsonObject) const; 67 | bool Get(const std::string& strKey, std::string& strValue) const; 68 | bool Get(const std::string& strKey, int32& iValue) const; 69 | bool Get(const std::string& strKey, uint32& uiValue) const; 70 | bool Get(const std::string& strKey, int64& llValue) const; 71 | bool Get(const std::string& strKey, uint64& ullValue) const; 72 | bool Get(const std::string& strKey, bool& bValue) const; 73 | bool Get(const std::string& strKey, float& fValue) const; 74 | bool Get(const std::string& strKey, double& dValue) const; 75 | bool IsNull(const std::string& strKey) const; 76 | bool Add(const std::string& strKey, const CJsonObject& oJsonObject); 77 | bool Add(const std::string& strKey, const std::string& strValue); 78 | bool Add(const std::string& strKey, int32 iValue); 79 | bool Add(const std::string& strKey, uint32 uiValue); 80 | bool Add(const std::string& strKey, int64 llValue); 81 | bool Add(const std::string& strKey, uint64 ullValue); 82 | bool Add(const std::string& strKey, bool bValue, bool bValueAgain); 83 | bool Add(const std::string& strKey, float fValue); 84 | bool Add(const std::string& strKey, double dValue); 85 | bool AddNull(const std::string& strKey); // add null like this: "key":null 86 | bool Delete(const std::string& strKey); 87 | bool Replace(const std::string& strKey, const CJsonObject& oJsonObject); 88 | bool Replace(const std::string& strKey, const std::string& strValue); 89 | bool Replace(const std::string& strKey, int32 iValue); 90 | bool Replace(const std::string& strKey, uint32 uiValue); 91 | bool Replace(const std::string& strKey, int64 llValue); 92 | bool Replace(const std::string& strKey, uint64 ullValue); 93 | bool Replace(const std::string& strKey, bool bValue, bool bValueAgain); 94 | bool Replace(const std::string& strKey, float fValue); 95 | bool Replace(const std::string& strKey, double dValue); 96 | bool ReplaceWithNull(const std::string& strKey); // replace value with null 97 | 98 | public: // method of json array 99 | int GetArraySize(); 100 | CJsonObject& operator[](unsigned int uiWhich); 101 | std::string operator()(unsigned int uiWhich) const; 102 | bool Get(int iWhich, CJsonObject& oJsonObject) const; 103 | bool Get(int iWhich, std::string& strValue) const; 104 | bool Get(int iWhich, int32& iValue) const; 105 | bool Get(int iWhich, uint32& uiValue) const; 106 | bool Get(int iWhich, int64& llValue) const; 107 | bool Get(int iWhich, uint64& ullValue) const; 108 | bool Get(int iWhich, bool& bValue) const; 109 | bool Get(int iWhich, float& fValue) const; 110 | bool Get(int iWhich, double& dValue) const; 111 | bool IsNull(int iWhich) const; 112 | bool Add(const CJsonObject& oJsonObject); 113 | bool Add(const std::string& strValue); 114 | bool Add(int32 iValue); 115 | bool Add(uint32 uiValue); 116 | bool Add(int64 llValue); 117 | bool Add(uint64 ullValue); 118 | bool Add(int iAnywhere, bool bValue); 119 | bool Add(float fValue); 120 | bool Add(double dValue); 121 | bool AddNull(); // add a null value 122 | bool AddAsFirst(const CJsonObject& oJsonObject); 123 | bool AddAsFirst(const std::string& strValue); 124 | bool AddAsFirst(int32 iValue); 125 | bool AddAsFirst(uint32 uiValue); 126 | bool AddAsFirst(int64 llValue); 127 | bool AddAsFirst(uint64 ullValue); 128 | bool AddAsFirst(int iAnywhere, bool bValue); 129 | bool AddAsFirst(float fValue); 130 | bool AddAsFirst(double dValue); 131 | bool AddNullAsFirst(); // add a null value 132 | bool Delete(int iWhich); 133 | bool Replace(int iWhich, const CJsonObject& oJsonObject); 134 | bool Replace(int iWhich, const std::string& strValue); 135 | bool Replace(int iWhich, int32 iValue); 136 | bool Replace(int iWhich, uint32 uiValue); 137 | bool Replace(int iWhich, int64 llValue); 138 | bool Replace(int iWhich, uint64 ullValue); 139 | bool Replace(int iWhich, bool bValue, bool bValueAgain); 140 | bool Replace(int iWhich, float fValue); 141 | bool Replace(int iWhich, double dValue); 142 | bool ReplaceWithNull(int iWhich); // replace with a null value 143 | 144 | private: 145 | CJsonObject(cJSON* pJsonData); 146 | 147 | private: 148 | cJSON* m_pJsonData; 149 | cJSON* m_pExternJsonDataRef; 150 | cJSON* m_pKeyTravers; 151 | std::string m_strErrMsg; 152 | std::map m_mapJsonArrayRef; 153 | std::map m_mapJsonObjectRef; 154 | }; 155 | 156 | } 157 | 158 | #endif /* CJSONHELPER_HPP_ */ 159 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | >Visual Stdio设置: 2 | >关闭预编译头:项目->属性->C/C++->预编译头->不使用预编译头 3 | >关闭安全警告:项目->属性->C/C++->预处理器->预处理器定义->添加 _CRT_SECURE_NO_WARNINGS 4 | 5 | >目前实现的http动作有:GET POST HEAD OPTIONS PUT DELETE 6 | 7 | [安装编译演示视频](https://www.bilibili.com/video/bv1gV411Z7fT) 另一个项目[WinFtp](https://github.com/lxwAsm/WinFtp) 8 | ### VisualStudio nuget安装(推荐) 9 | >工具->nuget包管理器->程序包管理控制台->输入: Install-Package CppRequests -Version 0.0.5 10 | 11 | ### 入门 12 | >#include \ 13 | >#include \ 14 | >#include "requests.h" 15 | >using namespace std; 16 | >using namespace requests; 17 | >int main(int argc) 18 | >{ 19 | >    Response resp = Get("https://baidu.com"); 20 | >    cout << resp.status << endl; 21 | >    cout << resp.GetText() << endl; 22 | >    return 0; 23 | > } 24 | ### Get获取网页内容 25 | >Response resp = Get("https://pan.baidu.com/disk/home"); 26 | >cout << resp.GetText() << endl; ` 27 | 28 | ### Post请求使用,上传json数据 29 | >map data; 30 | >data["name"] = "cpp"; 31 | >data["age"] = "14"; 32 | >Response resp = Post("http://47.106.162.182:8080/post.php", data); 33 | >cout << resp.status << endl; 34 | >cout << resp.GetText() << endl; 35 | 36 | ### Put上传文件 37 | >Response resp = Put("http://httpbin.org/put", "C:\\\\Users\\\\jack\\\\Desktop\\\\key.txt"); 38 | >cout << resp.status << endl; 39 | >cout << resp.GetText() << endl; 40 | 41 | ### Get带cookie访问网页 42 | >map header; 43 | >string cookie = "BAIDUID=B066E871294A61BE394DE24FFA475653:FG=1; BIDUPSID=B066E871294A61BE9EF72E101F79BF87; PSTM=1578882286;"; 44 | >Response resp = Get("https://pan.baidu.com/disk/home",header,cookie); 45 | >cout << resp.GetText() << endl; 46 | 47 | ### 设置代理 48 | >map options; 49 | >map header; 50 | >options["proxy"] = "http=http://122.51.49.88:8888"; 51 | >try{ 52 | >    Response resp = Get("http://pv.sohu.com/cityjson",header , "", options); 53 | >    cout << resp.status << endl; 54 | >} 55 | >catch (const char *msg){ 56 | >    cout << msg << endl; 57 | >} 58 | 59 | ### 自定义Http Header 60 | >map header; 61 | >map options; 62 | >options["timeout"] = "5000"; 63 | >header["name"] = "cpp"; 64 | >header["age"] = "14"; 65 | >header["User-Agent"] = "Cpp Brower" 66 | >Response resp = Get("http://httpbin.org/headers",header); 67 | >cout << resp.status << endl; 68 | >cout << resp.GetText() << endl; 69 | 70 | ### 上传文件 71 | >map header; 72 | >map data; 73 | >map files; 74 | >data["key"] = "something"; 75 | >data["submit"] = "Submit"; //form表单的其他要上传的数据,如账号密码。 76 | >files["file"] = "C:\\stdafx.h"; //form表单中的文件字段。 77 | >Response resp = Post(url,data,files); 78 | >cout << resp.status << endl; 79 | >cout << resp.GetText() << endl; 80 | -------------------------------------------------------------------------------- /cJSON.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Dave Gamble 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | */ 22 | 23 | /* cJSON */ 24 | /* JSON parser in C. */ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "cJSON.h" 34 | 35 | #ifndef INT_MAX 36 | #define INT_MAX 2147483647 37 | #define INT_MIN (-INT_MAX - 1) 38 | #define UINT_MAX 4294967295U 39 | #endif 40 | 41 | static const char *ep; 42 | 43 | const char *cJSON_GetErrorPtr() 44 | { 45 | return ep; 46 | } 47 | 48 | static int cJSON_strcasecmp(const char *s1, const char *s2) 49 | { 50 | if (!s1) 51 | return (s1 == s2) ? 0 : 1; 52 | if (!s2) 53 | return 1; 54 | for (; tolower(*s1) == tolower(*s2); ++s1, ++s2) 55 | if (*s1 == 0) 56 | return 0; 57 | return tolower(*(const unsigned char *)s1) 58 | - tolower(*(const unsigned char *)s2); 59 | } 60 | 61 | static void *(*cJSON_malloc)(size_t sz) = malloc; 62 | static void (*cJSON_free)(void *ptr) = free; 63 | 64 | static char* cJSON_strdup(const char* str) 65 | { 66 | size_t len; 67 | char* copy; 68 | 69 | len = strlen(str) + 1; 70 | if (!(copy = (char*) cJSON_malloc(len))) 71 | return 0; 72 | memcpy(copy, str, len); 73 | return copy; 74 | } 75 | 76 | void cJSON_InitHooks(cJSON_Hooks* hooks) 77 | { 78 | if (!hooks) 79 | { /* Reset hooks */ 80 | cJSON_malloc = malloc; 81 | cJSON_free = free; 82 | return; 83 | } 84 | 85 | cJSON_malloc = (hooks->malloc_fn) ? hooks->malloc_fn : malloc; 86 | cJSON_free = (hooks->free_fn) ? hooks->free_fn : free; 87 | } 88 | 89 | /* Internal constructor. */ 90 | static cJSON *cJSON_New_Item() 91 | { 92 | cJSON* node = (cJSON*) cJSON_malloc(sizeof(cJSON)); 93 | if (node) 94 | memset(node, 0, sizeof(cJSON)); 95 | return node; 96 | } 97 | 98 | /* Delete a cJSON structure. */ 99 | void cJSON_Delete(cJSON *c) 100 | { 101 | cJSON *next; 102 | while (c) 103 | { 104 | next = c->next; 105 | if (!(c->type & cJSON_IsReference) && c->child) 106 | cJSON_Delete(c->child); 107 | if (!(c->type & cJSON_IsReference) && c->valuestring) 108 | cJSON_free(c->valuestring); 109 | if (c->string) 110 | cJSON_free(c->string); 111 | cJSON_free(c); 112 | c = next; 113 | } 114 | } 115 | 116 | /* Parse the input text to generate a number, and populate the result into item. */ 117 | static const char *parse_number(cJSON *item, const char *num) 118 | { 119 | long double n = 0, scale = 0; 120 | int subscale = 0, signsubscale = 1; 121 | item->sign = 1; 122 | 123 | /* Could use sscanf for this? */ 124 | if (*num == '-') 125 | item->sign = -1, num++; /* Has sign? */ 126 | if (*num == '0') 127 | num++; /* is zero */ 128 | if (*num >= '1' && *num <= '9') 129 | do 130 | n = (n * 10.0) + (*num++ - '0'); 131 | while (*num >= '0' && *num <= '9'); /* Number? */ 132 | if (*num == '.' && num[1] >= '0' && num[1] <= '9') 133 | { 134 | num++; 135 | do 136 | n = (n * 10.0) + (*num++ - '0'), scale--; 137 | while (*num >= '0' && *num <= '9'); 138 | } /* Fractional part? */ 139 | if (*num == 'e' || *num == 'E') /* Exponent? */ 140 | { 141 | num++; 142 | if (*num == '+') 143 | num++; 144 | else if (*num == '-') 145 | signsubscale = -1, num++; /* With sign? */ 146 | while (*num >= '0' && *num <= '9') 147 | subscale = (subscale * 10) + (*num++ - '0'); /* Number? */ 148 | } 149 | 150 | if (scale == 0 && subscale == 0) 151 | { 152 | item->valuedouble = (double)(item->sign * n); 153 | item->valueint = item->sign * (int64)n; 154 | item->type = cJSON_Int; 155 | } 156 | else 157 | { 158 | n = item->sign * n * pow(10.0, (scale + subscale * signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */ 159 | item->valuedouble = (double)n; 160 | item->valueint = (int64)n; 161 | item->type = cJSON_Double; 162 | } 163 | return num; 164 | } 165 | 166 | /* Render the number nicely from the given item into a string. */ 167 | static char *print_double(cJSON *item) 168 | { 169 | char *str; 170 | double d = item->valuedouble; 171 | str = (char*) cJSON_malloc(64); /* This is a nice tradeoff. */ 172 | if (str) 173 | { 174 | if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e9) 175 | sprintf(str, "%lf", d); 176 | else 177 | sprintf(str, "%f", d); 178 | } 179 | return str; 180 | } 181 | 182 | static char *print_int(cJSON *item) 183 | { 184 | char *str; 185 | str = (char*) cJSON_malloc(22); /* 2^64+1 can be represented in 21 chars. */ 186 | if (str) 187 | { 188 | if (item->sign == -1) 189 | { 190 | if ((int64)item->valueint <= (int64)INT_MAX && (int64)item->valueint >= (int64)INT_MIN) 191 | { 192 | sprintf(str, "%d", (int32)item->valueint); 193 | } 194 | else 195 | { 196 | sprintf(str, "%lld", (int64)item->valueint); 197 | } 198 | } 199 | else 200 | { 201 | if (item->valueint <= (uint64)UINT_MAX) 202 | { 203 | sprintf(str, "%u", (uint32)item->valueint); 204 | } 205 | else 206 | { 207 | sprintf(str, "%llu", item->valueint); 208 | } 209 | } 210 | } 211 | return str; 212 | } 213 | 214 | /* Parse the input text into an unescaped cstring, and populate item. */ 215 | static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 216 | 0xF8, 0xFC }; 217 | static const char *parse_string(cJSON *item, const char *str) 218 | { 219 | const char *ptr = str + 1; 220 | char *ptr2; 221 | char *out; 222 | int len = 0; 223 | unsigned uc, uc2; 224 | if (*str != '\"') 225 | { 226 | ep = str; 227 | return 0; 228 | } /* not a string! */ 229 | 230 | while (*ptr != '\"' && *ptr && ++len) 231 | if (*ptr++ == '\\') 232 | ptr++; /* Skip escaped quotes. */ 233 | 234 | out = (char*) cJSON_malloc(len + 1); /* This is how long we need for the string, roughly. */ 235 | if (!out) 236 | return 0; 237 | 238 | ptr = str + 1; 239 | ptr2 = out; 240 | while (*ptr != '\"' && *ptr) 241 | { 242 | if (*ptr != '\\') 243 | *ptr2++ = *ptr++; 244 | else 245 | { 246 | ptr++; 247 | switch (*ptr) 248 | { 249 | case 'b': 250 | *ptr2++ = '\b'; 251 | break; 252 | case 'f': 253 | *ptr2++ = '\f'; 254 | break; 255 | case 'n': 256 | *ptr2++ = '\n'; 257 | break; 258 | case 'r': 259 | *ptr2++ = '\r'; 260 | break; 261 | case 't': 262 | *ptr2++ = '\t'; 263 | break; 264 | case 'u': /* transcode utf16 to utf8. */ 265 | sscanf(ptr + 1, "%4x", &uc); 266 | ptr += 4; /* get the unicode char. */ 267 | 268 | if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) 269 | break; // check for invalid. 270 | 271 | if (uc >= 0xD800 && uc <= 0xDBFF) // UTF16 surrogate pairs. 272 | { 273 | if (ptr[1] != '\\' || ptr[2] != 'u') 274 | break; // missing second-half of surrogate. 275 | sscanf(ptr + 3, "%4x", &uc2); 276 | ptr += 6; 277 | if (uc2 < 0xDC00 || uc2 > 0xDFFF) 278 | break; // invalid second-half of surrogate. 279 | uc = 0x10000 | ((uc & 0x3FF) << 10) | (uc2 & 0x3FF); 280 | } 281 | 282 | len = 4; 283 | if (uc < 0x80) 284 | len = 1; 285 | else if (uc < 0x800) 286 | len = 2; 287 | else if (uc < 0x10000) 288 | len = 3; 289 | ptr2 += len; 290 | 291 | switch (len) 292 | { 293 | case 4: 294 | *--ptr2 = ((uc | 0x80) & 0xBF); 295 | uc >>= 6; 296 | case 3: 297 | *--ptr2 = ((uc | 0x80) & 0xBF); 298 | uc >>= 6; 299 | case 2: 300 | *--ptr2 = ((uc | 0x80) & 0xBF); 301 | uc >>= 6; 302 | case 1: 303 | *--ptr2 = (uc | firstByteMark[len]); 304 | } 305 | ptr2 += len; 306 | break; 307 | default: 308 | *ptr2++ = *ptr; 309 | break; 310 | } 311 | ptr++; 312 | } 313 | } 314 | *ptr2 = 0; 315 | if (*ptr == '\"') 316 | ptr++; 317 | item->valuestring = out; 318 | item->type = cJSON_String; 319 | return ptr; 320 | } 321 | 322 | /* Render the cstring provided to an escaped version that can be printed. */ 323 | static char *print_string_ptr(const char *str) 324 | { 325 | const char *ptr; 326 | char *ptr2, *out; 327 | int len = 0; 328 | unsigned char token; 329 | 330 | if (!str) 331 | return cJSON_strdup(""); 332 | ptr = str; 333 | while ((token = *ptr) && ++len) 334 | { 335 | if (strchr("\"\\\b\f\n\r\t", token)) 336 | len++; 337 | else if (token < 32) 338 | len += 5; 339 | ptr++; 340 | } 341 | 342 | out = (char*) cJSON_malloc(len + 3); 343 | if (!out) 344 | return 0; 345 | 346 | ptr2 = out; 347 | ptr = str; 348 | *ptr2++ = '\"'; 349 | while (*ptr) 350 | { 351 | if ((unsigned char) *ptr > 31 && *ptr != '\"' && *ptr != '\\') 352 | *ptr2++ = *ptr++; 353 | else 354 | { 355 | *ptr2++ = '\\'; 356 | switch (token = *ptr++) 357 | { 358 | case '\\': 359 | *ptr2++ = '\\'; 360 | break; 361 | case '\"': 362 | *ptr2++ = '\"'; 363 | break; 364 | case '\b': 365 | *ptr2++ = 'b'; 366 | break; 367 | case '\f': 368 | *ptr2++ = 'f'; 369 | break; 370 | case '\n': 371 | *ptr2++ = 'n'; 372 | break; 373 | case '\r': 374 | *ptr2++ = 'r'; 375 | break; 376 | case '\t': 377 | *ptr2++ = 't'; 378 | break; 379 | default: 380 | sprintf(ptr2, "u%04x", token); 381 | ptr2 += 5; 382 | break; /* escape and print */ 383 | } 384 | } 385 | } 386 | *ptr2++ = '\"'; 387 | *ptr2++ = 0; 388 | return out; 389 | } 390 | /* Invote print_string_ptr (which is useful) on an item. */ 391 | static char *print_string(cJSON *item) 392 | { 393 | return print_string_ptr(item->valuestring); 394 | } 395 | 396 | /* Predeclare these prototypes. */ 397 | static const char *parse_value(cJSON *item, const char *value); 398 | static char *print_value(cJSON *item, int depth, int fmt); 399 | static const char *parse_array(cJSON *item, const char *value); 400 | static char *print_array(cJSON *item, int depth, int fmt); 401 | static const char *parse_object(cJSON *item, const char *value); 402 | static char *print_object(cJSON *item, int depth, int fmt); 403 | 404 | /* Utility to jump whitespace and cr/lf */ 405 | static const char *skip(const char *in) 406 | { 407 | while (in && *in && (unsigned char) *in <= 32) 408 | in++; 409 | return in; 410 | } 411 | 412 | /* Parse an object - create a new root, and populate. */ 413 | cJSON *cJSON_Parse(const char *value) 414 | { 415 | cJSON *c = cJSON_New_Item(); 416 | ep = 0; 417 | if (!c) 418 | return 0; /* memory fail */ 419 | 420 | if (!parse_value(c, skip(value))) 421 | { 422 | cJSON_Delete(c); 423 | return 0; 424 | } 425 | return c; 426 | } 427 | 428 | /* Render a cJSON item/entity/structure to text. */ 429 | char *cJSON_Print(cJSON *item) 430 | { 431 | return print_value(item, 0, 1); 432 | } 433 | char *cJSON_PrintUnformatted(cJSON *item) 434 | { 435 | return print_value(item, 0, 0); 436 | } 437 | 438 | /* Parser core - when encountering text, process appropriately. */ 439 | static const char *parse_value(cJSON *item, const char *value) 440 | { 441 | if (!value) 442 | return 0; /* Fail on null. */ 443 | if (!strncmp(value, "null", 4)) 444 | { 445 | item->type = cJSON_NULL; 446 | return value + 4; 447 | } 448 | if (!strncmp(value, "false", 5)) 449 | { 450 | item->type = cJSON_False; 451 | return value + 5; 452 | } 453 | if (!strncmp(value, "true", 4)) 454 | { 455 | item->type = cJSON_True; 456 | item->valueint = 1; 457 | return value + 4; 458 | } 459 | if (*value == '\"') 460 | { 461 | return parse_string(item, value); 462 | } 463 | if (*value == '-' || (*value >= '0' && *value <= '9')) 464 | { 465 | return parse_number(item, value); 466 | } 467 | if (*value == '[') 468 | { 469 | return parse_array(item, value); 470 | } 471 | if (*value == '{') 472 | { 473 | return parse_object(item, value); 474 | } 475 | 476 | ep = value; 477 | return 0; /* failure. */ 478 | } 479 | 480 | /* Render a value to text. */ 481 | static char *print_value(cJSON *item, int depth, int fmt) 482 | { 483 | char *out = 0; 484 | if (!item) 485 | return 0; 486 | switch ((item->type) & 255) 487 | { 488 | case cJSON_NULL: 489 | out = cJSON_strdup("null"); 490 | break; 491 | case cJSON_False: 492 | out = cJSON_strdup("false"); 493 | break; 494 | case cJSON_True: 495 | out = cJSON_strdup("true"); 496 | break; 497 | case cJSON_Int: 498 | out = print_int(item); 499 | break; 500 | case cJSON_Double: 501 | out = print_double(item); 502 | break; 503 | case cJSON_String: 504 | out = print_string(item); 505 | break; 506 | case cJSON_Array: 507 | out = print_array(item, depth, fmt); 508 | break; 509 | case cJSON_Object: 510 | out = print_object(item, depth, fmt); 511 | break; 512 | } 513 | return out; 514 | } 515 | 516 | /* Build an array from input text. */ 517 | static const char *parse_array(cJSON *item, const char *value) 518 | { 519 | cJSON *child; 520 | if (*value != '[') 521 | { 522 | ep = value; 523 | return 0; 524 | } /* not an array! */ 525 | 526 | item->type = cJSON_Array; 527 | value = skip(value + 1); 528 | if (*value == ']') 529 | return value + 1; /* empty array. */ 530 | 531 | item->child = child = cJSON_New_Item(); 532 | if (!item->child) 533 | return 0; /* memory fail */ 534 | value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */ 535 | if (!value) 536 | return 0; 537 | 538 | while (*value == ',') 539 | { 540 | cJSON *new_item; 541 | if (!(new_item = cJSON_New_Item())) 542 | return 0; /* memory fail */ 543 | child->next = new_item; 544 | new_item->prev = child; 545 | child = new_item; 546 | value = skip(parse_value(child, skip(value + 1))); 547 | if (!value) 548 | return 0; /* memory fail */ 549 | } 550 | 551 | if (*value == ']') 552 | return value + 1; /* end of array */ 553 | ep = value; 554 | return 0; /* malformed. */ 555 | } 556 | 557 | /* Render an array to text */ 558 | static char *print_array(cJSON *item, int depth, int fmt) 559 | { 560 | char **entries; 561 | char *out = 0, *ptr, *ret; 562 | int len = 5; 563 | cJSON *child = item->child; 564 | int numentries = 0, i = 0, fail = 0; 565 | 566 | /* How many entries in the array? */ 567 | while (child) 568 | numentries++, child = child->next; 569 | /* Allocate an array to hold the values for each */ 570 | entries = (char**) cJSON_malloc(numentries * sizeof(char*)); 571 | if (!entries) 572 | return 0; 573 | memset(entries, 0, numentries * sizeof(char*)); 574 | /* Retrieve all the results: */ 575 | child = item->child; 576 | while (child && !fail) 577 | { 578 | ret = print_value(child, depth + 1, fmt); 579 | entries[i++] = ret; 580 | if (ret) 581 | len += strlen(ret) + 2 + (fmt ? 1 : 0); 582 | else 583 | fail = 1; 584 | child = child->next; 585 | } 586 | 587 | /* If we didn't fail, try to malloc the output string */ 588 | if (!fail) 589 | out = (char*) cJSON_malloc(len); 590 | /* If that fails, we fail. */ 591 | if (!out) 592 | fail = 1; 593 | 594 | /* Handle failure. */ 595 | if (fail) 596 | { 597 | for (i = 0; i < numentries; i++) 598 | if (entries[i]) 599 | cJSON_free(entries[i]); 600 | cJSON_free(entries); 601 | return 0; 602 | } 603 | 604 | /* Compose the output array. */ 605 | *out = '['; 606 | ptr = out + 1; 607 | *ptr = 0; 608 | for (i = 0; i < numentries; i++) 609 | { 610 | strcpy(ptr, entries[i]); 611 | ptr += strlen(entries[i]); 612 | if (i != numentries - 1) 613 | { 614 | *ptr++ = ','; 615 | if (fmt) 616 | *ptr++ = ' '; 617 | *ptr = 0; 618 | } 619 | cJSON_free(entries[i]); 620 | } 621 | cJSON_free(entries); 622 | *ptr++ = ']'; 623 | *ptr++ = 0; 624 | return out; 625 | } 626 | 627 | /* Build an object from the text. */ 628 | static const char *parse_object(cJSON *item, const char *value) 629 | { 630 | cJSON *child; 631 | if (*value != '{') 632 | { 633 | ep = value; 634 | return 0; 635 | } /* not an object! */ 636 | 637 | item->type = cJSON_Object; 638 | value = skip(value + 1); 639 | if (*value == '}') 640 | return value + 1; /* empty array. */ 641 | 642 | item->child = child = cJSON_New_Item(); 643 | if (!item->child) 644 | return 0; 645 | value = skip(parse_string(child, skip(value))); 646 | if (!value) 647 | return 0; 648 | child->string = child->valuestring; 649 | child->valuestring = 0; 650 | if (*value != ':') 651 | { 652 | ep = value; 653 | return 0; 654 | } /* fail! */ 655 | value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */ 656 | if (!value) 657 | return 0; 658 | 659 | while (*value == ',') 660 | { 661 | cJSON *new_item; 662 | if (!(new_item = cJSON_New_Item())) 663 | return 0; /* memory fail */ 664 | child->next = new_item; 665 | new_item->prev = child; 666 | child = new_item; 667 | value = skip(parse_string(child, skip(value + 1))); 668 | if (!value) 669 | return 0; 670 | child->string = child->valuestring; 671 | child->valuestring = 0; 672 | if (*value != ':') 673 | { 674 | ep = value; 675 | return 0; 676 | } /* fail! */ 677 | value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */ 678 | if (!value) 679 | return 0; 680 | } 681 | 682 | if (*value == '}') 683 | return value + 1; /* end of array */ 684 | ep = value; 685 | return 0; /* malformed. */ 686 | } 687 | 688 | /* Render an object to text. */ 689 | static char *print_object(cJSON *item, int depth, int fmt) 690 | { 691 | char **entries = 0, **names = 0; 692 | char *out = 0, *ptr, *ret, *str; 693 | int len = 7, i = 0, j; 694 | cJSON *child = item->child; 695 | int numentries = 0, fail = 0; 696 | /* Count the number of entries. */ 697 | while (child) 698 | numentries++, child = child->next; 699 | /* Allocate space for the names and the objects */ 700 | entries = (char**) cJSON_malloc(numentries * sizeof(char*)); 701 | if (!entries) 702 | return 0; 703 | names = (char**) cJSON_malloc(numentries * sizeof(char*)); 704 | if (!names) 705 | { 706 | cJSON_free(entries); 707 | return 0; 708 | } 709 | memset(entries, 0, sizeof(char*) * numentries); 710 | memset(names, 0, sizeof(char*) * numentries); 711 | 712 | /* Collect all the results into our arrays: */ 713 | child = item->child; 714 | depth++; 715 | if (fmt) 716 | len += depth; 717 | while (child) 718 | { 719 | names[i] = str = print_string_ptr(child->string); 720 | entries[i++] = ret = print_value(child, depth, fmt); 721 | if (str && ret) 722 | len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0); 723 | else 724 | fail = 1; 725 | child = child->next; 726 | } 727 | 728 | /* Try to allocate the output string */ 729 | if (!fail) 730 | out = (char*) cJSON_malloc(len); 731 | if (!out) 732 | fail = 1; 733 | 734 | /* Handle failure */ 735 | if (fail) 736 | { 737 | for (i = 0; i < numentries; i++) 738 | { 739 | if (names[i]) 740 | cJSON_free(names[i]); 741 | if (entries[i]) 742 | cJSON_free(entries[i]); 743 | } 744 | cJSON_free(names); 745 | cJSON_free(entries); 746 | return 0; 747 | } 748 | 749 | /* Compose the output: */ 750 | *out = '{'; 751 | ptr = out + 1; 752 | if (fmt) 753 | *ptr++ = '\n'; 754 | *ptr = 0; 755 | for (i = 0; i < numentries; i++) 756 | { 757 | if (fmt) 758 | for (j = 0; j < depth; j++) 759 | *ptr++ = '\t'; 760 | strcpy(ptr, names[i]); 761 | ptr += strlen(names[i]); 762 | *ptr++ = ':'; 763 | if (fmt) 764 | *ptr++ = '\t'; 765 | strcpy(ptr, entries[i]); 766 | ptr += strlen(entries[i]); 767 | if (i != numentries - 1) 768 | *ptr++ = ','; 769 | if (fmt) 770 | *ptr++ = '\n'; 771 | *ptr = 0; 772 | cJSON_free(names[i]); 773 | cJSON_free(entries[i]); 774 | } 775 | 776 | cJSON_free(names); 777 | cJSON_free(entries); 778 | if (fmt) 779 | for (i = 0; i < depth - 1; i++) 780 | *ptr++ = '\t'; 781 | *ptr++ = '}'; 782 | *ptr++ = 0; 783 | return out; 784 | } 785 | 786 | /* Get Array size/item / object item. */ 787 | int cJSON_GetArraySize(cJSON *array) 788 | { 789 | cJSON *c = array->child; 790 | int i = 0; 791 | while (c) 792 | i++, c = c->next; 793 | return i; 794 | } 795 | cJSON *cJSON_GetArrayItem(cJSON *array, int item) 796 | { 797 | cJSON *c = array->child; 798 | while (c && item > 0) 799 | item--, c = c->next; 800 | return c; 801 | } 802 | cJSON *cJSON_GetObjectItem(cJSON *object, const char *string) 803 | { 804 | cJSON *c = object->child; 805 | while (c && cJSON_strcasecmp(c->string, string)) 806 | c = c->next; 807 | return c; 808 | } 809 | 810 | /* Utility for array list handling. */ 811 | static void suffix_object(cJSON *prev, cJSON *item) 812 | { 813 | prev->next = item; 814 | item->prev = prev; 815 | } 816 | /* Utility for handling references. */ 817 | static cJSON *create_reference(cJSON *item) 818 | { 819 | cJSON *ref = cJSON_New_Item(); 820 | if (!ref) 821 | return 0; 822 | memcpy(ref, item, sizeof(cJSON)); 823 | ref->string = 0; 824 | ref->type |= cJSON_IsReference; 825 | ref->next = ref->prev = 0; 826 | return ref; 827 | } 828 | 829 | /* Add item to array/object. */ 830 | void cJSON_AddItemToArray(cJSON *array, cJSON *item) 831 | { 832 | cJSON *c = array->child; 833 | if (!item) 834 | return; 835 | if (!c) 836 | { 837 | array->child = item; 838 | } 839 | else 840 | { 841 | while (c && c->next) 842 | c = c->next; 843 | suffix_object(c, item); 844 | } 845 | } 846 | 847 | void cJSON_AddItemToArrayHead(cJSON *array, cJSON *item) 848 | { 849 | cJSON *c = array->child; 850 | if (!item) 851 | return; 852 | if (!c) 853 | { 854 | array->child = item; 855 | } 856 | else 857 | { 858 | item->prev = c->prev; 859 | item->next = c; 860 | c->prev = item; 861 | array->child = item; 862 | } 863 | } 864 | 865 | void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) 866 | { 867 | if (!item) 868 | return; 869 | if (item->string) 870 | cJSON_free(item->string); 871 | item->string = cJSON_strdup(string); 872 | cJSON_AddItemToArray(object, item); 873 | } 874 | void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) 875 | { 876 | cJSON_AddItemToArray(array, create_reference(item)); 877 | } 878 | void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, 879 | cJSON *item) 880 | { 881 | cJSON_AddItemToObject(object, string, create_reference(item)); 882 | } 883 | 884 | cJSON *cJSON_DetachItemFromArray(cJSON *array, int which) 885 | { 886 | cJSON *c = array->child; 887 | while (c && which > 0) 888 | c = c->next, which--; 889 | if (!c) 890 | return 0; 891 | if (c->prev) 892 | c->prev->next = c->next; 893 | if (c->next) 894 | c->next->prev = c->prev; 895 | if (c == array->child) 896 | array->child = c->next; 897 | c->prev = c->next = 0; 898 | return c; 899 | } 900 | void cJSON_DeleteItemFromArray(cJSON *array, int which) 901 | { 902 | cJSON_Delete(cJSON_DetachItemFromArray(array, which)); 903 | } 904 | cJSON *cJSON_DetachItemFromObject(cJSON *object, const char *string) 905 | { 906 | int i = 0; 907 | cJSON *c = object->child; 908 | while (c && cJSON_strcasecmp(c->string, string)) 909 | i++, c = c->next; 910 | if (c) 911 | return cJSON_DetachItemFromArray(object, i); 912 | return 0; 913 | } 914 | void cJSON_DeleteItemFromObject(cJSON *object, const char *string) 915 | { 916 | cJSON_Delete(cJSON_DetachItemFromObject(object, string)); 917 | } 918 | 919 | /* Replace array/object items with new ones. */ 920 | void cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) 921 | { 922 | cJSON *c = array->child; 923 | while (c && which > 0) 924 | c = c->next, which--; 925 | if (!c) 926 | return; 927 | newitem->next = c->next; 928 | newitem->prev = c->prev; 929 | if (newitem->next) 930 | newitem->next->prev = newitem; 931 | if (c == array->child) 932 | array->child = newitem; 933 | else 934 | newitem->prev->next = newitem; 935 | c->next = c->prev = 0; 936 | cJSON_Delete(c); 937 | } 938 | void cJSON_ReplaceItemInObject(cJSON *object, const char *string, 939 | cJSON *newitem) 940 | { 941 | int i = 0; 942 | cJSON *c = object->child; 943 | while (c && cJSON_strcasecmp(c->string, string)) 944 | i++, c = c->next; 945 | if (c) 946 | { 947 | newitem->string = cJSON_strdup(string); 948 | cJSON_ReplaceItemInArray(object, i, newitem); 949 | } 950 | } 951 | 952 | /* Create basic types: */ 953 | cJSON *cJSON_CreateNull() 954 | { 955 | cJSON *item = cJSON_New_Item(); 956 | if (item) 957 | item->type = cJSON_NULL; 958 | return item; 959 | } 960 | cJSON *cJSON_CreateTrue() 961 | { 962 | cJSON *item = cJSON_New_Item(); 963 | if (item) 964 | item->type = cJSON_True; 965 | return item; 966 | } 967 | cJSON *cJSON_CreateFalse() 968 | { 969 | cJSON *item = cJSON_New_Item(); 970 | if (item) 971 | item->type = cJSON_False; 972 | return item; 973 | } 974 | cJSON *cJSON_CreateBool(int b) 975 | { 976 | cJSON *item = cJSON_New_Item(); 977 | if (item) 978 | item->type = b ? cJSON_True : cJSON_False; 979 | return item; 980 | } 981 | cJSON *cJSON_CreateDouble(double num, int sign) 982 | { 983 | cJSON *item = cJSON_New_Item(); 984 | if (item) 985 | { 986 | item->type = cJSON_Double; 987 | item->valuedouble = num; 988 | item->valueint = (int64)num; 989 | item->sign = sign; 990 | } 991 | return item; 992 | } 993 | cJSON *cJSON_CreateInt(uint64 num, int sign) 994 | { 995 | cJSON *item = cJSON_New_Item(); 996 | if (item) 997 | { 998 | item->type = cJSON_Int; 999 | item->valuedouble = (double)num; 1000 | item->valueint = (int64)num; 1001 | item->sign = sign; 1002 | } 1003 | return item; 1004 | } 1005 | cJSON *cJSON_CreateString(const char *string) 1006 | { 1007 | cJSON *item = cJSON_New_Item(); 1008 | if (item) 1009 | { 1010 | item->type = cJSON_String; 1011 | item->valuestring = cJSON_strdup(string); 1012 | } 1013 | return item; 1014 | } 1015 | cJSON *cJSON_CreateArray() 1016 | { 1017 | cJSON *item = cJSON_New_Item(); 1018 | if (item) 1019 | item->type = cJSON_Array; 1020 | return item; 1021 | } 1022 | cJSON *cJSON_CreateObject() 1023 | { 1024 | cJSON *item = cJSON_New_Item(); 1025 | if (item) 1026 | item->type = cJSON_Object; 1027 | return item; 1028 | } 1029 | 1030 | /* Create Arrays: */ 1031 | cJSON *cJSON_CreateIntArray(int *numbers, int sign, int count) 1032 | { 1033 | int i; 1034 | cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(); 1035 | for (i = 0; a && i < count; i++) 1036 | { 1037 | n = cJSON_CreateDouble((long double)((unsigned int)numbers[i]), sign); 1038 | if (!i) 1039 | a->child = n; 1040 | else 1041 | suffix_object(p, n); 1042 | p = n; 1043 | } 1044 | return a; 1045 | } 1046 | cJSON *cJSON_CreateFloatArray(float *numbers, int count) 1047 | { 1048 | int i; 1049 | cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(); 1050 | for (i = 0; a && i < count; i++) 1051 | { 1052 | n = cJSON_CreateDouble((long double)numbers[i], -1); 1053 | if (!i) 1054 | a->child = n; 1055 | else 1056 | suffix_object(p, n); 1057 | p = n; 1058 | } 1059 | return a; 1060 | } 1061 | cJSON *cJSON_CreateDoubleArray(double *numbers, int count) 1062 | { 1063 | int i; 1064 | cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(); 1065 | for (i = 0; a && i < count; i++) 1066 | { 1067 | n = cJSON_CreateDouble((long double)numbers[i], -1); 1068 | if (!i) 1069 | a->child = n; 1070 | else 1071 | suffix_object(p, n); 1072 | p = n; 1073 | } 1074 | return a; 1075 | } 1076 | cJSON *cJSON_CreateStringArray(const char **strings, int count) 1077 | { 1078 | int i; 1079 | cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(); 1080 | for (i = 0; a && i < count; i++) 1081 | { 1082 | n = cJSON_CreateString(strings[i]); 1083 | if (!i) 1084 | a->child = n; 1085 | else 1086 | suffix_object(p, n); 1087 | p = n; 1088 | } 1089 | return a; 1090 | } 1091 | 1092 | -------------------------------------------------------------------------------- /cJSON.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Dave Gamble 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | */ 22 | 23 | #ifndef cJSON__h 24 | #define cJSON__h 25 | #include 26 | 27 | typedef int int32; 28 | typedef unsigned int uint32; 29 | #ifndef _WIN32 30 | #if __WORDSIZE == 64 31 | typedef long int64; 32 | typedef unsigned long uint64; 33 | #endif 34 | #else 35 | typedef long long int64; 36 | typedef unsigned long long uint64; 37 | #endif 38 | 39 | 40 | #ifdef __cplusplus 41 | extern "C" 42 | { 43 | #endif 44 | 45 | /* cJSON Types: */ 46 | #define cJSON_False 0 47 | #define cJSON_True 1 48 | #define cJSON_NULL 2 49 | #define cJSON_Int 3 50 | #define cJSON_Double 4 51 | #define cJSON_String 5 52 | #define cJSON_Array 6 53 | #define cJSON_Object 7 54 | 55 | #define cJSON_IsReference 256 56 | 57 | /* The cJSON structure: */ 58 | typedef struct cJSON 59 | { 60 | struct cJSON *next, *prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ 61 | struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ 62 | 63 | int type; /* The type of the item, as above. */ 64 | 65 | char *valuestring; /* The item's string, if type==cJSON_String */ 66 | int64 valueint; /* The item's number, if type==cJSON_Number */ 67 | double valuedouble; /* The item's number, if type==cJSON_Number */ 68 | int sign; /* sign of valueint, 1(unsigned), -1(signed) */ 69 | 70 | char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ 71 | } cJSON; 72 | 73 | typedef struct cJSON_Hooks 74 | { 75 | void *(*malloc_fn)(size_t sz); 76 | void (*free_fn)(void *ptr); 77 | } cJSON_Hooks; 78 | 79 | /* Supply malloc, realloc and free functions to cJSON */ 80 | extern void cJSON_InitHooks(cJSON_Hooks* hooks); 81 | 82 | /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ 83 | extern cJSON *cJSON_Parse(const char *value); 84 | /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ 85 | extern char *cJSON_Print(cJSON *item); 86 | /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ 87 | extern char *cJSON_PrintUnformatted(cJSON *item); 88 | /* Delete a cJSON entity and all subentities. */ 89 | extern void cJSON_Delete(cJSON *c); 90 | 91 | /* Returns the number of items in an array (or object). */ 92 | extern int cJSON_GetArraySize(cJSON *array); 93 | /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ 94 | extern cJSON *cJSON_GetArrayItem(cJSON *array, int item); 95 | /* Get item "string" from object. Case insensitive. */ 96 | extern cJSON *cJSON_GetObjectItem(cJSON *object, const char *string); 97 | 98 | /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ 99 | extern const char *cJSON_GetErrorPtr(); 100 | 101 | /* These calls create a cJSON item of the appropriate type. */ 102 | extern cJSON *cJSON_CreateNull(); 103 | extern cJSON *cJSON_CreateTrue(); 104 | extern cJSON *cJSON_CreateFalse(); 105 | extern cJSON *cJSON_CreateBool(int b); 106 | extern cJSON *cJSON_CreateDouble(double num, int sign); 107 | extern cJSON *cJSON_CreateInt(uint64 num, int sign); 108 | extern cJSON *cJSON_CreateString(const char *string); 109 | extern cJSON *cJSON_CreateArray(); 110 | extern cJSON *cJSON_CreateObject(); 111 | 112 | /* These utilities create an Array of count items. */ 113 | extern cJSON *cJSON_CreateIntArray(int *numbers, int sign, int count); 114 | extern cJSON *cJSON_CreateFloatArray(float *numbers, int count); 115 | extern cJSON *cJSON_CreateDoubleArray(double *numbers, int count); 116 | extern cJSON *cJSON_CreateStringArray(const char **strings, int count); 117 | 118 | /* Append item to the specified array/object. */ 119 | extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); 120 | extern void cJSON_AddItemToArrayHead(cJSON *array, cJSON *item); /* add by Bwar on 2015-01-28 */ 121 | extern void cJSON_AddItemToObject(cJSON *object, const char *string, 122 | cJSON *item); 123 | /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ 124 | extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); 125 | extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, 126 | cJSON *item); 127 | 128 | /* Remove/Detatch items from Arrays/Objects. */ 129 | extern cJSON *cJSON_DetachItemFromArray(cJSON *array, int which); 130 | extern void cJSON_DeleteItemFromArray(cJSON *array, int which); 131 | extern cJSON *cJSON_DetachItemFromObject(cJSON *object, const char *string); 132 | extern void cJSON_DeleteItemFromObject(cJSON *object, const char *string); 133 | 134 | /* Update array items. */ 135 | extern void cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); 136 | extern void cJSON_ReplaceItemInObject(cJSON *object, const char *string, 137 | cJSON *newitem); 138 | 139 | #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) 140 | #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) 141 | #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) 142 | #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) 143 | #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) 144 | 145 | 146 | #ifdef __cplusplus 147 | } 148 | #endif 149 | 150 | #endif 151 | -------------------------------------------------------------------------------- /requests.cpp: -------------------------------------------------------------------------------- 1 | #include "requests.h" 2 | using namespace requests; 3 | Response::Response(shared_ptr rep) 4 | { 5 | int pos = rep->find("\r\n\r\n"); 6 | string head = rep->substr(0, pos); 7 | rep->erase(0, pos+4); 8 | pContent = rep; 9 | vector line; 10 | SplitString(head, line, "\r\n"); 11 | for (int i = 0; i < line.size(); i++){ 12 | int p = line[i].find(':'); 13 | if (p == -1){ 14 | header["status"] = line[i]; 15 | this->status = status2int(line[i]); 16 | continue; 17 | } 18 | string key = line[i].substr(0, p); 19 | string value = line[i].substr(p + 1, line[i].size() - 1 - p); 20 | header[key] = value; 21 | } 22 | 23 | } 24 | 25 | unsigned int Response::status2int(string &st){ 26 | int s = st.find(' '); 27 | int e = st.rfind(' '); 28 | return atoi(st.substr(s + 1, e - s).c_str()); 29 | 30 | } 31 | Response::Response(std::string h,std::string origin_domain,shared_ptr data){ 32 | string head; 33 | this->req_domain = origin_domain; 34 | int pos = h.find("\r\n\r\n"); 35 | if (pos != -1){ 36 | head = h.substr(0, pos); 37 | } 38 | else{ 39 | head = h; 40 | } 41 | pContent = data; 42 | vector line; 43 | SplitString(head, line, "\r\n"); 44 | for (int i = 0; i < line.size(); i++){ 45 | int p = line[i].find(':'); 46 | if (p == -1 && line[i].find("HTTP/")!=-1){ 47 | header["status"] = line[i]; 48 | this->status = status2int(line[i]); 49 | continue; 50 | } 51 | string key = line[i].substr(0, p); 52 | string value = line[i].substr(p + 1, line[i].size() - 1 - p); 53 | header[key] = value; 54 | //printf("https header%s:%s\n", key.c_str(), value.c_str()); 55 | if (key == s_trim("Set-Cookie")){ 56 | vector name_value; 57 | SplitString(value, name_value, ";"); 58 | for (std::string v : name_value){ 59 | int pos = v.find("="); 60 | if (pos == -1) continue; 61 | std::string name = s_trim(v.substr(0, pos)); 62 | std::string value = s_trim(v.substr(pos + 1)); 63 | cookie[name] = value;//save cookie dic 64 | } 65 | this->vec_cookie.push_back(value);//save multi cookie str 66 | } 67 | } 68 | } 69 | void Response::SplitString(const string& s, vector& v, const string& c) 70 | { 71 | string::size_type pos1, pos2; 72 | pos2 = s.find(c); 73 | pos1 = 0; 74 | while (string::npos != pos2) 75 | { 76 | v.push_back(s.substr(pos1, pos2 - pos1)); 77 | 78 | pos1 = pos2 + c.size(); 79 | pos2 = s.find(c, pos1); 80 | } 81 | if (pos1 != s.length()) 82 | v.push_back(s.substr(pos1)); 83 | } 84 | 85 | string Response::GetText(){ 86 | return pContent->to_string(); 87 | } 88 | 89 | map Response::Header(){ 90 | return header; 91 | } 92 | const byte* Response::GetBinary(){ 93 | return pContent->raw_buffer(); 94 | } 95 | 96 | unsigned int Response::size(){ 97 | return pContent->size(); 98 | } 99 | string Response::operator[](string key){ 100 | return header[key]; 101 | } 102 | 103 | Response::~Response(){ 104 | 105 | } 106 | 107 | Request::Request(std::string url, std::string method, const map &head, const map &options) :url(url) 108 | { 109 | //http://www.baidu.com/hello?jack=123 110 | header["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"; 111 | header["Connection"] = "Keep-Alive"; 112 | default_options["timeout"] = "3000"; 113 | default_options["proxy"] = ""; 114 | this->method = method;//请求get post delete put 115 | if (head.size() > 0){ 116 | map::const_iterator begin = head.cbegin(); 117 | for (; begin != head.cend(); begin++){ 118 | header[begin->first] = begin->second;//替换默认header的设置 119 | } 120 | } 121 | if (options.size() > 0){ 122 | map::const_iterator begin = options.cbegin(); 123 | for (; begin != options.end(); begin++){ 124 | default_options[begin->first] = begin->second;//替换默认options的设置 125 | } 126 | } 127 | this->timeout = atoi(default_options["timeout"].c_str()); 128 | this->proxy = default_options["proxy"]; 129 | int pos = url.find("://"); 130 | if (pos == -1){ 131 | url.insert(0, "http://"); 132 | this->prefix = "http://"; 133 | pos = url.find("://"); 134 | } 135 | else{ 136 | this->prefix = url.substr(0, pos + 3); 137 | } 138 | int end = url.find('/', pos + 3); 139 | if (end == -1){//没有参数 140 | domain = url.substr(pos + 3); 141 | if (url.substr(0, 5) == "http:"){ 142 | port = 80; 143 | } 144 | else{ 145 | port = 443; 146 | } 147 | param = "/"; 148 | } 149 | else{//有请求参数 150 | std::string domain_port = url.substr(pos + 3, end - pos - 3); 151 | int port_pos = domain_port.find(":"); 152 | if (port_pos != -1){//不是默认端口 153 | domain = domain_port.substr(0, port_pos); 154 | port = atoi(domain_port.substr(port_pos + 1).c_str()); 155 | } 156 | else{ 157 | if (url.substr(0, 5) == "http:"){ 158 | port = 80; 159 | } 160 | else{ 161 | port = 443; 162 | } 163 | domain = domain_port; 164 | } 165 | 166 | param = url.substr(end); 167 | } 168 | } 169 | 170 | void Request::SetPostHeader(BinaryData &data){ 171 | if (method == "POST" && data.size()>0){ 172 | header["Content-Length"] = to_string(data.size()); 173 | if (data.find("--216378052142052079582464804396") != -1){ 174 | header["Content-Type"] = "multipart/form-data; boundary=---------------------------216378052142052079582464804396"; 175 | } 176 | else{ 177 | header["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"; 178 | } 179 | return; 180 | } 181 | if (method == "PUT"){ 182 | header["Content-Length"] = to_string(data.size()); 183 | std::string filetype = get_content_type(data.user_data); 184 | header["Content-Type"] = filetype; 185 | } 186 | } 187 | 188 | string Request::HeaderToString(){ 189 | string head; 190 | map::const_iterator begin = header.cbegin(); 191 | for (; begin != header.cend(); begin++){ 192 | head+=begin->first + ": " + begin->second + "\r\n"; 193 | } 194 | head += "\r\n";//next to content; 195 | return head; 196 | } 197 | 198 | 199 | Request::~Request(){ 200 | 201 | } 202 | 203 | Session::Session(){ 204 | 205 | } 206 | 207 | Session::~Session(){ 208 | 209 | } 210 | 211 | bool Session::InstallCookie(Response &r){ 212 | bool isSuccess = FALSE; 213 | for (auto i : r.cookie){ 214 | cookies[i.first] = i.second; 215 | } 216 | if (r.vec_cookie.size() > 0){ 217 | this->vec_cookie = r.vec_cookie; 218 | } 219 | for (std::string &c : r.vec_cookie){ 220 | isSuccess=InternetSetCookieA(r.req_domain.c_str(), NULL, c.c_str())==TRUE; 221 | } 222 | return isSuccess; 223 | } 224 | Response Session::Get(std::string url,const map &head, std::string cookie_arg,const map &options){ 225 | BinaryData db; 226 | Response r = requests::request("GET", url, db, head, cookie_arg, options); 227 | this->InstallCookie(r); 228 | return r; 229 | } 230 | 231 | Response Session::Post(std::string url, map &data,const map files,const map &head, std::string cookie_arg,const map &options){ 232 | Response r = https_post(url, data, files, head, cookie_arg, options); 233 | this->InstallCookie(r); 234 | return r; 235 | } 236 | 237 | //----------BinaryData----------- 238 | BinaryData::BinaryData() 239 | { 240 | 241 | } 242 | 243 | BinaryData::BinaryData(int maxsize) 244 | { 245 | data.reserve(maxsize); 246 | } 247 | 248 | 249 | void BinaryData::append(byte n){ 250 | data.push_back(n); 251 | } 252 | 253 | int BinaryData::size(){ 254 | return data.size(); 255 | } 256 | void BinaryData::append(const std::string n){ 257 | data.insert(data.end(), n.begin(), n.end()); 258 | 259 | } 260 | 261 | int BinaryData::find(const char *s){ 262 | return std::string(data.begin(), data.end()).find(s); 263 | } 264 | 265 | std::string BinaryData::substr(int start, int end){ 266 | return std::string(data.begin() + start, data.begin() + end); 267 | } 268 | 269 | void BinaryData::erase(int start, int end){ 270 | data.erase(data.begin() + start, data.begin() + end); 271 | } 272 | const byte* BinaryData::raw_buffer(){ 273 | return data.data(); 274 | } 275 | void BinaryData::append(byte *buffer, int size){ 276 | data.insert(data.end(), buffer, buffer + size); 277 | } 278 | std::string BinaryData::to_string(){ 279 | return std::string(data.begin(), data.end()); 280 | } 281 | BinaryData::~BinaryData() 282 | { 283 | 284 | } 285 | 286 | //-----------EndBinary---------------------- 287 | 288 | std::string requests::GetIpByDomainName(const char *szHost) 289 | { 290 | WSADATA wsaData; 291 | std::string ip; 292 | HOSTENT *pHostEnt; 293 | int nAdapter = 0; 294 | struct sockaddr_in sAddr; 295 | if (WSAStartup(0x0101, &wsaData)) 296 | { 297 | printf(" gethostbyname error for host:\n"); 298 | return ""; 299 | } 300 | pHostEnt = gethostbyname(szHost); 301 | if (pHostEnt) 302 | { 303 | if (pHostEnt->h_addr_list[nAdapter]) 304 | { 305 | memcpy(&sAddr.sin_addr.s_addr, pHostEnt->h_addr_list[nAdapter], pHostEnt->h_length); 306 | ip = inet_ntoa(sAddr.sin_addr); 307 | } 308 | } 309 | WSACleanup(); 310 | return ip; 311 | } 312 | 313 | Response DoSend(std::string url, map &head, string method = "GET ", string data = ""){ 314 | shared_ptr pData(new BinaryData(1000)); 315 | Request req(url,method,head); 316 | try 317 | { 318 | 319 | WSADATA wData; 320 | ::WSAStartup(MAKEWORD(2, 2), &wData); 321 | std::string m_ip = GetIpByDomainName(req.domain.c_str()); 322 | SOCKET clientSocket = socket(AF_INET, 1, 0); 323 | struct sockaddr_in ServerAddr = { 0 }; 324 | ServerAddr.sin_addr.s_addr = inet_addr(m_ip.c_str()); 325 | ServerAddr.sin_port = htons(80); 326 | ServerAddr.sin_family = AF_INET; 327 | int errNo = connect(clientSocket, (sockaddr*)&ServerAddr, sizeof(ServerAddr)); 328 | if (errNo == 0) 329 | { 330 | string strSend = "";//with post data 331 | printf(strSend.c_str()); 332 | errNo = send(clientSocket, strSend.c_str(), strSend.length(), 0); 333 | if (errNo > 0) 334 | { 335 | byte bufRecv[3069] = { 0 }; 336 | while (1){ 337 | errNo = recv(clientSocket, (char*)bufRecv, 3069, 0); 338 | if (errNo > 0) 339 | { 340 | pData->append(bufRecv, errNo); 341 | } 342 | else{ break; }; 343 | } 344 | 345 | } 346 | } 347 | ::WSACleanup(); 348 | } 349 | catch (...) 350 | { 351 | ; 352 | } 353 | return Response(pData); 354 | 355 | } 356 | //************************************ 357 | // Method: Get 358 | // FullName: requests::Get 359 | // Access: public 360 | // Returns: requests::Response 361 | // Qualifier: 362 | // Parameter: std::string url 363 | // Parameter: map & head 365 | // Parameter: std::string cookie 366 | // Parameter: map & options 368 | //************************************ 369 | Response requests::Get(std::string url,const map &head,std::string cookie,const map &options) 370 | { 371 | BinaryData db; 372 | return requests::request("GET", url, db, head, cookie, options); 373 | 374 | } 375 | 376 | Response requests::Delete(std::string url, const map &head, std::string cookie, const map &options){ 377 | BinaryData db; 378 | return requests::request("DELETE", url, db, head, cookie, options); 379 | } 380 | 381 | Response requests::Head(std::string url, const map &head, std::string cookie, const map &options){ 382 | BinaryData db; 383 | return requests::request("HEAD", url, db, head, cookie, options); 384 | } 385 | 386 | Response requests::Options(std::string url, const map &head, std::string cookie, const map &options){ 387 | BinaryData db; 388 | return requests::request("OPTIONS", url, db, head, cookie, options); 389 | 390 | } 391 | 392 | Response requests::Put(std::string url, const std::string filepath, const map &head, std::string cookie, const map &options){ 393 | ifstream in(filepath, ios::in | ios::binary | ios::ate); 394 | BinaryData up_data; 395 | if (!in.is_open()){ 396 | throw "file open failed"; 397 | } 398 | char * buffer; 399 | long size; 400 | size = in.tellg(); 401 | in.seekg(0, ios::beg); 402 | buffer = new char[size]; 403 | in.read(buffer, size); 404 | in.close(); 405 | up_data.append((byte*)buffer, size); 406 | delete[] buffer; 407 | up_data.user_data = filepath; 408 | return requests::request("PUT", url, up_data, head, cookie, options); 409 | } 410 | Response requests::Post(std::string url, BinaryData &data,const map &head, std::string cookie,const map &options){ 411 | return requests::https_post(url, data, head,cookie,options); 412 | } 413 | 414 | Response requests::Post(string url, map &data,const map files,const map &head,std::string cookie,const map &options){ 415 | return https_post(url,data,files,head,cookie,options); 416 | } 417 | 418 | Response requests::Post(std::string url, std::string str_data, const map &head, std::string cookie, const map &options){ 419 | BinaryData bd; 420 | bd.append(str_data); 421 | return requests::https_post(url, bd, head, cookie, options); 422 | } 423 | 424 | Response requests::request(string method, string url, BinaryData &data,const map &head,std::string cookie,const map &options){ 425 | DWORD flags; 426 | if (to_lower(url.substr(0, 6)) == "https:"){ 427 | flags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | 428 | INTERNET_FLAG_KEEP_CONNECTION | 429 | //INTERNET_FLAG_NO_AUTH | 430 | //INTERNET_FLAG_NO_COOKIES | 431 | INTERNET_FLAG_NO_UI | 432 | INTERNET_FLAG_SECURE | 433 | INTERNET_FLAG_RELOAD; 434 | return requests::https_send(method, url, INTERNET_DEFAULT_HTTPS_PORT, flags,data, head,cookie,options); 435 | } 436 | else{ 437 | flags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD; 438 | return requests::https_send(method, url, INTERNET_DEFAULT_HTTP_PORT,flags, data, head,cookie,options); 439 | } 440 | } 441 | 442 | 443 | Response requests::https_post(string url, BinaryData &data,const map &head,std::string cookie,const map &options){ 444 | return requests::request("POST", url, data, head,cookie,options); 445 | } 446 | 447 | std::string requests::get_content_type(string &filename){ 448 | std::map map_type = {// 449 | { ".pdf", "application/pdf" }, 450 | { ".js", "application/ecmascript" }, 451 | { ".json", "application/json" }, 452 | { ".zip", "application/x-zip-compressed" }, 453 | { ".txt", "text/plain" }, 454 | { ".mp3", "audio/mpeg" }, 455 | { ".gif", "image/gif" }, 456 | { ".c", "text/plain" }, 457 | { ".h", "text/plain" }, 458 | { ".py", "text/x-python-script" }, 459 | { ".jpg", "image/jpeg" } 460 | }; 461 | int index = filename.rfind('.'); 462 | if (index == -1){ 463 | return "application/octet-stream"; 464 | } 465 | else{ 466 | std::string file_type = filename.substr(index); 467 | std::string result = map_type[file_type]; 468 | if (result.size() > 0){ 469 | return result; 470 | } 471 | else{ 472 | return "application/octet-stream";//unkonw file type 473 | } 474 | } 475 | } 476 | 477 | Response requests::https_post(string url, map &data,const map files,const map &head,std::string cookie,const map &options){ 478 | string up_str; 479 | BinaryData up_data; 480 | if (files.size() > 0){// 481 | for (auto &f : files){ 482 | up_data.append("-----------------------------216378052142052079582464804396\r\n"); 483 | int pos = f.second.rfind("\\"); 484 | std::string filename; 485 | if (pos != -1){ 486 | filename = f.second.substr(pos + 1); 487 | } 488 | else{ 489 | filename = f.second; 490 | } 491 | up_data.append("Content-Disposition: form-data; name=\"" + f.first + "\"" + "; filename=\"" + filename + "\"\r\n"); 492 | std::string file_type = get_content_type(filename); 493 | up_data.append("Content-Type: "+file_type+"\r\n\r\n"); 494 | char * buffer; 495 | long size; 496 | ifstream in(f.second, ios::in | ios::binary | ios::ate); 497 | if (!in.is_open()){ 498 | throw "file open failed"; 499 | } 500 | 501 | size = in.tellg(); 502 | in.seekg(0, ios::beg); 503 | buffer = new char[size]; 504 | in.read(buffer, size); 505 | in.close(); 506 | up_data.append((byte*)buffer, size); 507 | delete[] buffer; 508 | } 509 | for (auto &i : data){ 510 | up_data.append("\r\n-----------------------------216378052142052079582464804396\r\n"); 511 | up_data.append("Content-Disposition: form-data; name=\""+i.first+"\"\r\n\r\n"); 512 | up_data.append(i.second); 513 | } 514 | up_data.append("\r\n-----------------------------216378052142052079582464804396--"); 515 | } 516 | else{ 517 | for (auto &i : data){ 518 | up_str += i.first + "="; 519 | up_str += i.second + "&"; 520 | } 521 | up_str.erase(up_str.end() - 1); 522 | up_data.append(up_str); 523 | } 524 | return requests::request("POST", url, up_data, head,cookie,options); 525 | } 526 | 527 | std::string requests::http_err2str(DWORD code){ 528 | static map err_code = { 529 | { 12001, "ERROR_INTERNET_OUT_OF_HANDLES No more handles could be generated at this time." }, 530 | { 12002, "ERROR_INTERNET_TIMEOUT The request has timed out." }, 531 | { 12003, "ERROR_INTERNET_EXTENDED_ERROR An extended error was returned from the server., This is typically a string or buffer containing a verbose error message. Call InternetGetLastResponseInfo to retrieve the error text." }, 532 | { 12004, "ERROR_INTERNET_INTERNAL_ERROR An internal error has occurred." }, 533 | { 12005, "ERROR_INTERNET_INVALID_URL The URL is invalid." }, 534 | { 12006, "ERROR_INTERNET_UNRECOGNIZED_SCHEME The URL scheme could not be recognized or is not supported." }, 535 | { 12007, "ERROR_INTERNET_NAME_NOT_RESOLVED The server name could not be resolved." }, 536 | { 12008, "ERROR_INTERNET_PROTOCOL_NOT_FOUND The requested protocol could not be located." }, 537 | { 12009, "ERROR_INTERNET_INVALID_OPTION A request to InternetQueryOption or InternetSetOption specified an invalid option value." }, 538 | { 12010, "ERROR_INTERNET_BAD_OPTION_LENGTH The length of an option supplied to InternetQueryOption or InternetSetOption is incorrect for the type of option specified." }, 539 | { 12011, "ERROR_INTERNET_OPTION_NOT_SETTABLE The request option cannot be set, only queried." }, 540 | { 12012, "ERROR_INTERNET_SHUTDOWN The Win32 Internet function support is being shut down or unloaded." }, 541 | { 12013, "ERROR_INTERNET_INCORRECT_USER_NAME The request to connect and log on to an FTP server could not be completed because the supplied user name is incorrect." }, 542 | { 12014, "ERROR_INTERNET_INCORRECT_PASSWORD The request to connect and log on to an FTP server could not be completed because the supplied password is incorrect." }, 543 | { 12015, "ERROR_INTERNET_LOGIN_FAILURE The request to connect to and log on to an FTP server failed." }, 544 | { 12016, "ERROR_INTERNET_INVALID_OPERATION The requested operation is invalid." }, 545 | { 12017, "ERROR_INTERNET_OPERATION_CANCELLED The operation was canceled, usually because the handle on which the request was operating was closed before the operation completed." }, 546 | { 12018, "ERROR_INTERNET_INCORRECT_HANDLE_TYPE The type of handle supplied is incorrect for this operation." }, 547 | { 12019, "ERROR_INTERNET_INCORRECT_HANDLE_STATE The requested operation cannot be carried out because the handle supplied is not in the correct state." }, 548 | { 12020, "ERROR_INTERNET_NOT_PROXY_REQUEST The request cannot be made via a proxy." }, 549 | { 12021, "ERROR_INTERNET_REGISTRY_VALUE_NOT_FOUND A required registry value could not be located." }, 550 | { 12022, "ERROR_INTERNET_BAD_REGISTRY_PARAMETER A required registry value was located but is an incorrect type or has an invalid value." }, 551 | { 12023, "ERROR_INTERNET_NO_DIRECT_ACCESS Direct network access cannot be made at this time." }, 552 | { 12024, "ERROR_INTERNET_NO_CONTEXT An asynchronous request could not be made because a zero context value was supplied." }, 553 | { 12025, "ERROR_INTERNET_NO_CALLBACK An asynchronous request could not be made because a callback function has not been set." }, 554 | { 12026, "ERROR_INTERNET_REQUEST_PENDING The required operation could not be completed because one or more requests are pending." }, 555 | { 12027, "ERROR_INTERNET_INCORRECT_FORMAT The format of the request is invalid." }, 556 | { 12028, "ERROR_INTERNET_ITEM_NOT_FOUND The requested item could not be located." }, 557 | { 12029, "ERROR_INTERNET_CANNOT_CONNECT The attempt to connect to the server failed." }, 558 | { 12030, "ERROR_INTERNET_CONNECTION_ABORTED The connection with the server has been terminated." }, 559 | { 12031, "ERROR_INTERNET_CONNECTION_RESET The connection with the server has been reset." }, 560 | { 12032, "ERROR_INTERNET_FORCE_RETRY Calls for the Win32 Internet function to redo the request." }, 561 | { 12033, "ERROR_INTERNET_INVALID_PROXY_REQUEST The request to the proxy was invalid." }, 562 | { 12036, "ERROR_INTERNET_HANDLE_EXISTS The request failed because the handle already exists." }, 563 | { 12037, "ERROR_INTERNET_SEC_CERT_DATE_INVALID SSL certificate date that was received from the server is bad.The certificate is expired."}, 564 | { 12038, "ERROR_INTERNET_SEC_CERT_CN_INVALID SSL certificate common name (host name field) is incorrect., For example, if you entered www.server.com and the common name on the certificate says www.,different.com."}, 565 | { 12039, "ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR The application is moving from a non-SSL to an SSL connection because of a redirect."}, 566 | { 12040, "ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR The application is moving from an SSL to an non-SSL connection because of a redirect." }, 567 | { 12041, "ERROR_INTERNET_MIXED_SECURITY Indicates that the content is not entirely secure. Some of the content being viewed may have come from unsecured servers."}, 568 | { 12042, "ERROR_INTERNET_CHG_POST_IS_NON_SECURE The application is posting and attempting to change multiple lines of text on a server that is not secure."}, 569 | { 12043, "ERROR_INTERNET_POST_IS_NON_SECURE The application is posting data to a server that is not secure." }, 570 | { 12110, "ERROR_FTP_TRANSFER_IN_PROGRESS The requested operation cannot be made on the FTP session handle because an operation is already in progress." }, 571 | { 12111, "ERROR_FTP_DROPPED The FTP operation was not completed because the session was aborted." }, 572 | { 12130, "ERROR_GOPHER_PROTOCOL_ERROR An error was detected while parsing data returned from the gopher server." }, 573 | { 12131, "ERROR_GOPHER_NOT_FILE The request must be made for a file locator." }, 574 | { 12132, "ERROR_GOPHER_DATA_ERROR An error was detected while receiving data from the gopher server." }, 575 | { 12133, "ERROR_GOPHER_END_OF_DATA The end of the data has been reached." }, 576 | { 12134, "ERROR_GOPHER_INVALID_LOCATOR The supplied locator is not valid." }, 577 | { 12135, "ERROR_GOPHER_INCORRECT_LOCATOR_TYPE The type of the locator is not correct for this operation." }, 578 | { 12136, "ERROR_GOPHER_NOT_GOPHER_PLUS The requested operation can only be made against a Gopher+ server or with a locator that specifies a Gopher+ operation." }, 579 | { 12137, "ERROR_GOPHER_ATTRIBUTE_NOT_FOUND The requested attribute could not be located." }, 580 | { 12138, "ERROR_GOPHER_UNKNOWN_LOCATOR The locator type is unknown." }, 581 | { 12150, "ERROR_HTTP_HEADER_NOT_FOUND The requested header could not be located." }, 582 | { 12151, "ERROR_HTTP_DOWNLEVEL_SERVER The server did not return any headers." }, 583 | { 12152, "ERROR_HTTP_INVALID_SERVER_RESPONSE The server response could not be parsed." }, 584 | { 12153, "ERROR_HTTP_INVALID_HEADER The supplied header is invalid." }, 585 | { 12154, "ERROR_HTTP_INVALID_QUERY_REQUEST The request made to HttpQueryInfo is invalid." }, 586 | { 12155, "ERROR_HTTP_HEADER_ALREADY_EXISTS The header could not be added because it already exists." }, 587 | { 12156, "ERROR_HTTP_REDIRECT_FAILED The redirection failed because either the scheme changed (for example, HTTP to FTP) or all attempts made to redirect failed (default is five attempts)." }, 588 | }; 589 | return err_code[code]; 590 | } 591 | 592 | Response requests::https_send(string method, string url, int port, DWORD flags, BinaryData &data,const map &head,std::string cookie,const map &options){ 593 | Request req(url,method, head,options); 594 | LPCTSTR lpszAgent = L"WinInetGet/CppRequests"; 595 | std::wstring proxy = s2ws(req.proxy); 596 | LPCTSTR lpszProxy = proxy.c_str(); 597 | HINTERNET hInternet = NULL; 598 | if (proxy.size() > 0){ 599 | hInternet = InternetOpen(lpszAgent,INTERNET_OPEN_TYPE_PROXY, lpszProxy, NULL, 0); 600 | } 601 | else{ 602 | hInternet = InternetOpen(lpszAgent,INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 603 | } 604 | auto domain = s2ws(req.domain); 605 | //printf("domain:%S", domain.c_str()); 606 | LPCTSTR lpszServerName = domain.c_str(); 607 | INTERNET_PORT nServerPort = req.port; // 608 | LPCTSTR lpszUserName = NULL;//s2ws(req.default_options["username"]).c_str(); // 609 | LPCTSTR lpszPassword = NULL;//s2ws(req.default_options["password"]).c_str(); // 610 | DWORD dwConnectFlags = 0; 611 | DWORD dwConnectContext = 0; 612 | DWORD dwError = 0; 613 | HINTERNET hConnect = InternetConnect(hInternet, 614 | lpszServerName, nServerPort, 615 | lpszUserName, lpszPassword, 616 | INTERNET_SERVICE_HTTP, 617 | dwConnectFlags, dwConnectContext); 618 | 619 | //使用Get 620 | auto http_method = s2ws(s_trim(req.method)); 621 | LPCTSTR lpszVerb = http_method.c_str(); 622 | auto param = s2ws(req.param); 623 | //printf("param:%S", param.c_str()); 624 | LPCTSTR lpszObjectName = param.c_str(); 625 | LPCTSTR lpszVersion = L"HTTP/1.1"; // 626 | LPCTSTR lpszReferrer = NULL; // 627 | LPCTSTR *lplpszAcceptTypes = NULL; // 628 | DWORD dwOpenRequestFlags = flags; 629 | DWORD dwOpenRequestContext = 0; 630 | HINTERNET hRequest = HttpOpenRequest(hConnect, lpszVerb, lpszObjectName, lpszVersion, 631 | lpszReferrer, lplpszAcceptTypes, 632 | dwOpenRequestFlags, dwOpenRequestContext); 633 | 634 | if (hRequest == NULL){ 635 | char err_buffer[200] = { 0 }; 636 | dwError = GetLastError(); 637 | std::string err_info = http_err2str(dwError); 638 | _snprintf(err_buffer, 200, "HttpOpenRequest failed,error = %d (%s)\n", 639 | dwError, err_info.c_str()); 640 | throw err_buffer; 641 | } 642 | //Set Internet Timeout here 643 | DWORD dwTimeOut = req.timeout; 644 | InternetSetOption(hRequest, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeOut, sizeof(dwTimeOut)); 645 | //Set Cookies Header 646 | auto header_cookie=head.find("Cookie"); 647 | if (header_cookie!=head.end()){ 648 | cookie += ";" + header_cookie->second; 649 | } 650 | if (cookie.size()>0){ 651 | vector name_value = SplitString(cookie, ";"); 652 | for (std::string v : name_value){ 653 | int pos = v.find("="); 654 | if (pos == -1) continue; 655 | std::string name = s_trim(v.substr(0, pos)); 656 | std::string value = s_trim(v.substr(pos + 1)); 657 | if (!InternetSetCookieA((req.prefix + req.domain).c_str(), name.c_str(),value.c_str())){ 658 | //printf("Cookies Add Ok"); 659 | } 660 | } 661 | 662 | } 663 | req.SetPostHeader(data); 664 | auto he = s2ws(req.HeaderToString()); 665 | //printf("https:%S\ntimeout:%d\nproxy:%s\n%s", he.c_str(),req.timeout,req.proxy.c_str(),data.to_string().c_str()); 666 | //printf("post data:%s", data.to_string().c_str()); 667 | //Ignore 12057 Error 668 | DWORD dwFlags; 669 | DWORD dwBuffLen = sizeof(dwFlags); 670 | InternetQueryOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen); 671 | dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA; 672 | dwFlags |= SECURITY_FLAG_IGNORE_REVOCATION; 673 | InternetSetOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags)); 674 | if (!HttpSendRequest(hRequest, he.c_str(), he.size(), (LPVOID)data.raw_buffer(), data.size())) 675 | { 676 | char err_buffer[200] = { 0 }; 677 | dwError = GetLastError(); 678 | std::string err_info = http_err2str(dwError); 679 | _snprintf(err_buffer, 200, "HttpSendRequest failed,error = %d (%s)\n", 680 | dwError, err_info.c_str()); 681 | throw err_buffer; 682 | } 683 | // 684 | DWORD dwInfoLevel = HTTP_QUERY_RAW_HEADERS_CRLF; 685 | DWORD dwInfoBufferLength = 2048; 686 | BYTE *pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength + 2); 687 | while (!HttpQueryInfo(hRequest, dwInfoLevel, pInfoBuffer, &dwInfoBufferLength, NULL)) { 688 | DWORD dwError = GetLastError(); 689 | if (dwError == ERROR_INSUFFICIENT_BUFFER) { 690 | free(pInfoBuffer); 691 | pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength + 2); 692 | } 693 | else { 694 | char err_buffer[200] = { 0 }; 695 | dwError = GetLastError(); 696 | std::string err_info = http_err2str(dwError); 697 | _snprintf(err_buffer, 200, "HttpQueryInfo failed,error = %d (%s)\n", 698 | dwError, err_info.c_str()); 699 | throw err_buffer; 700 | } 701 | } 702 | pInfoBuffer[dwInfoBufferLength] = '/0'; 703 | pInfoBuffer[dwInfoBufferLength + 1] = '/0'; 704 | wstring header = (WCHAR*)pInfoBuffer; 705 | free(pInfoBuffer); 706 | //HTTP Response 707 | DWORD dwBytesAvailable; 708 | BYTE *pMessageBody = NULL; 709 | shared_ptr content(new BinaryData(1000)); 710 | while (InternetQueryDataAvailable(hRequest, &dwBytesAvailable, 0, 0)) { 711 | pMessageBody = (BYTE*)malloc(dwBytesAvailable + 1); 712 | DWORD dwBytesRead; 713 | BOOL bResult = InternetReadFile(hRequest, pMessageBody, 714 | dwBytesAvailable, &dwBytesRead); 715 | if (!bResult) { 716 | char err_buffer[200] = { 0 }; 717 | free(pMessageBody); 718 | _snprintf(err_buffer,200,"InternetReadFile failed, error = %d (0x%x)/n", 719 | GetLastError(), GetLastError()); 720 | throw err_buffer; 721 | } 722 | if (dwBytesRead == 0) { 723 | free(pMessageBody); 724 | break; // End of File. 725 | } 726 | content->append(pMessageBody, dwBytesRead); 727 | free(pMessageBody); 728 | } 729 | //Free handles 730 | InternetCloseHandle(hRequest); 731 | InternetCloseHandle(hConnect); 732 | InternetCloseHandle(hInternet); 733 | auto h = ws2s(header); 734 | Response rep(h,req.prefix+req.domain, content); 735 | return rep; 736 | } 737 | -------------------------------------------------------------------------------- /requests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef http996 3 | #define http996 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "wininet.h" 11 | #include "utils.h" 12 | #include "CJsonObject.hpp" 13 | #pragma comment(lib,"wininet.lib") 14 | #pragma comment(lib, "ws2_32.lib") 15 | using namespace std; 16 | typedef unsigned char byte;// 17 | namespace requests{ 18 | 19 | class BinaryData 20 | { 21 | public: 22 | BinaryData(); 23 | explicit BinaryData(int size); 24 | //BinaryData(const BinaryData &); 25 | ~BinaryData(); 26 | void append(byte n); 27 | void append(byte *buffer, int size); 28 | void append(std::string n); 29 | int find(const char *s); 30 | std::string substr(int start, int end); 31 | void erase(int start, int end); 32 | const byte* raw_buffer(); 33 | int size(); 34 | std::string to_string(); 35 | std::string user_data; 36 | private: 37 | std::vector data; 38 | }; 39 | class Response 40 | { 41 | public: 42 | Response(); 43 | Response(std::shared_ptr rep); 44 | Response(std::string h,std::string origin_domain, std::shared_ptr data); 45 | ~Response(); 46 | std::string GetText(); 47 | map Header(); 48 | const byte*GetBinary(); 49 | unsigned int size(); 50 | map < std::string, std::string> cookie; 51 | std::vector vec_cookie; 52 | std::string req_domain; 53 | std::string operator[](std::string key); 54 | unsigned int status; 55 | private: 56 | map header; 57 | std:: string text; 58 | std::shared_ptr pContent; 59 | void SplitString(const string& s, vector& v, const string& c); 60 | unsigned int status2int(string &s); 61 | }; 62 | 63 | class Request 64 | { 65 | public: 66 | Request(std::string url, std::string method,const map &header =map(),const map &options = map()); 67 | ~Request(); 68 | string HeaderToString(); 69 | void SetPostHeader(BinaryData &data); 70 | std::string url; 71 | std::string domain; 72 | std::string param; 73 | std::string prefix; 74 | unsigned int timeout; 75 | std::string proxy; 76 | map header; 77 | map default_options; 78 | int port; 79 | std::string method; 80 | }; 81 | class Session{ 82 | public: 83 | Session(); 84 | Session(std::string proxy); 85 | ~Session(); 86 | Response Get(std::string url, const map &head = map(), std::string cookie = "", const map &options = map()); 87 | Response Post(std::string url, map &data,const map files = map(), const map &head = map(), std::string cookie = "", const map &options = map()); 88 | Response Post(std::string url, BinaryData &data,const map &head = map(), const map &options = map()); 89 | map cookies; 90 | std::vector vec_cookie; 91 | private: 92 | HINTERNET hInternet; 93 | HINTERNET hConnect; 94 | HINTERNET hRequest; 95 | std::string inner_proxy; 96 | private: 97 | bool InstallCookie(Response &r); 98 | }; 99 | 100 | std::string GetIpByDomainName(const char *szHost); 101 | Response Get(std::string url,const map &head = map(),std::string cookie="",const map &options = map()); 102 | Response Delete(std::string url, const map &head = map(), std::string cookie = "", const map &options = map()); 103 | Response Head(std::string url, const map &head = map(), std::string cookie = "", const map &options = map()); 104 | Response Options(std::string url, const map &head = map(), std::string cookie = "", const map &options = map()); 105 | Response Put(std::string url, const std::string filepath,const map &head = map(), std::string cookie = "", const map &options = map()); 106 | Response Post(std::string url, map &data, map files = map(),const map &head = map(),std::string cookie="",const map &options = map()); 107 | Response Post(std::string url, BinaryData &bin_data,const map &head = map(),std::string cookie="",const map &options = map()); 108 | Response Post(std::string url, std::string str_data, const map &head = map(), std::string cookie = "", const map &options = map()); 109 | Response https_post(string url, BinaryData &data,const map &head = map(),std::string cookie="",const map &options = map()); 110 | Response https_post(string url, map &data, map files,const map &head = map(),std::string cookie="",const map &options = map()); 111 | Response request(string method, string url, BinaryData &data,const map &head = map(),std::string cookie="",const map &options = map()); 112 | Response https_send(string method, string url, int port, DWORD flags, BinaryData &data,const map &head = map(),std::string cookie="",const map &options = map()); 113 | std::string get_content_type(string &filename); 114 | std::string http_err2str(DWORD code); 115 | } 116 | #endif -------------------------------------------------------------------------------- /utils.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lxwAsm/requests/c0d67b3b0220addcea7783ef1701e79846eae314/utils.cpp -------------------------------------------------------------------------------- /utils.h: -------------------------------------------------------------------------------- 1 | #ifndef utils996 2 | #define utils996 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include // 11 | 12 | std::wstring s2ws(const std::string& str); 13 | std::string ws2s(const std::wstring &str); 14 | std::string to_lower(std::string str); 15 | 16 | class Base64 17 | { 18 | public: 19 | std::string Encode(char* Data, int DataByte); 20 | std::string Decode(const char* Data, int DataByte, int& OutByte); 21 | }; 22 | 23 | std::string md5(const std::string str); 24 | std::vector SplitString(const std::string& s, const std::string& c); 25 | 26 | class MD5 27 | { 28 | public: 29 | typedef unsigned int size_type; // must be 32bit 30 | 31 | MD5(); 32 | MD5(const std::string& text); 33 | void update(const unsigned char *buf, size_type length); 34 | void update(const char *buf, size_type length); 35 | MD5& finalize(); 36 | std::string hexdigest() const; 37 | friend std::ostream& operator<<(std::ostream&, MD5 md5); 38 | 39 | private: 40 | void init(); 41 | typedef unsigned char uint1; // 8bit 42 | typedef unsigned int uint4; // 32bit 43 | enum { blocksize = 64 }; // VC6 won't eat a const static int here 44 | 45 | void transform(const uint1 block[blocksize]); 46 | static void decode(uint4 output[], const uint1 input[], size_type len); 47 | static void encode(uint1 output[], const uint4 input[], size_type len); 48 | 49 | bool finalized; 50 | uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk 51 | uint4 count[2]; // 64bit counter for number of bits (lo, hi) 52 | uint4 state[4]; // digest so far 53 | uint1 digest[16]; // the result 54 | 55 | // low level logic operations 56 | static inline uint4 F(uint4 x, uint4 y, uint4 z); 57 | static inline uint4 G(uint4 x, uint4 y, uint4 z); 58 | static inline uint4 H(uint4 x, uint4 y, uint4 z); 59 | static inline uint4 I(uint4 x, uint4 y, uint4 z); 60 | static inline uint4 rotate_left(uint4 x, int n); 61 | static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 62 | static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 63 | static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 64 | static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 65 | }; 66 | 67 | static std::string ltrim(std::string s); 68 | static std::string rtrim(std::string s); 69 | std::string s_trim(std::string s); 70 | #endif 71 | --------------------------------------------------------------------------------