| 1011 | } |
| 1012 | |
| 1013 | bool CJsonObject::Add(const std::string& strKey, const CJsonObject& oJsonObject) |
| 1014 | { |
| 1015 | cJSON* pFocusData = NULL; |
| 1016 | if (m_pJsonData != NULL) |
| 1017 | { |
| 1018 | pFocusData = m_pJsonData; |
| 1019 | } |
| 1020 | else if (m_pExternJsonDataRef != NULL) |
| 1021 | { |
| 1022 | pFocusData = m_pExternJsonDataRef; |
| 1023 | } |
| 1024 | else |
| 1025 | { |
| 1026 | m_pJsonData = cJSON_CreateObject(); |
| 1027 | m_pKeyTravers = m_pJsonData; |
| 1028 | pFocusData = m_pJsonData; |
| 1029 | } |
| 1030 | |
| 1031 | if (pFocusData == NULL) |
| 1032 | { |
| 1033 | m_strErrMsg = "json data is null!"; |
| 1034 | return(false); |
| 1035 | } |
| 1036 | if (pFocusData->type != cJSON_Object) |
| 1037 | { |
| 1038 | m_strErrMsg = "not a json object! json array?"; |
| 1039 | return(false); |
| 1040 | } |
| 1041 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) |
| 1042 | { |
| 1043 | m_strErrMsg = "key exists!"; |
| 1044 | return(false); |
| 1045 | } |
| 1046 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str(), &mc_pError) ; |
| 1047 | if (pJsonStruct == NULL) |
| 1048 | { |
| 1049 | m_strErrMsg = std::string("prase json string error at ") + mc_pError; |
| 1050 | return(false); |
| 1051 | } |
| 1052 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); |
| 1053 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) |
| 1054 | { |
| 1055 | return(false); |
| 1056 | } |
| 1057 | #if __cplusplus < 201101L |
| 1058 | std::map<std::string, CJsonObject*>::iterator iter = m_mapJsonObjectRef.find(strKey); |
| 1059 | #else |
| 1060 | auto iter = m_mapJsonObjectRef.find(strKey); |
| 1061 | #endif |
| 1062 | if (iter != m_mapJsonObjectRef.end()) |
| 1063 | { |
| 1064 | if (iter->second != NULL) |
| 1065 | { |
| 1066 | delete (iter->second); |
| 1067 | iter->second = NULL; |
| 1068 | } |
| 1069 | m_mapJsonObjectRef.erase(iter); |
| 1070 | } |
nothing calls this directly
no test coverage detected