| 974 | } |
| 975 | |
| 976 | bool CJsonObject::Add(const std::string& strKey, const CJsonObject& oJsonObject) |
| 977 | { |
| 978 | cJSON* pFocusData = NULL; |
| 979 | if (m_pJsonData != NULL) |
| 980 | { |
| 981 | pFocusData = m_pJsonData; |
| 982 | } |
| 983 | else if (m_pExternJsonDataRef != NULL) |
| 984 | { |
| 985 | pFocusData = m_pExternJsonDataRef; |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | m_pJsonData = cJSON_CreateObject(); |
| 990 | m_pKeyTravers = m_pJsonData; |
| 991 | pFocusData = m_pJsonData; |
| 992 | } |
| 993 | |
| 994 | if (pFocusData == NULL) |
| 995 | { |
| 996 | m_strErrMsg = "json data is null!"; |
| 997 | return(false); |
| 998 | } |
| 999 | if (pFocusData->type != cJSON_Object) |
| 1000 | { |
| 1001 | m_strErrMsg = "not a json object! json array?"; |
| 1002 | return(false); |
| 1003 | } |
| 1004 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) != NULL) |
| 1005 | { |
| 1006 | m_strErrMsg = "key exists!"; |
| 1007 | return(false); |
| 1008 | } |
| 1009 | cJSON* pJsonStruct = cJSON_Parse(oJsonObject.ToString().c_str(), &mc_pError) ; |
| 1010 | if (pJsonStruct == NULL) |
| 1011 | { |
| 1012 | m_strErrMsg = std::string("prase json string error at ") + mc_pError; |
| 1013 | return(false); |
| 1014 | } |
| 1015 | cJSON_AddItemToObject(pFocusData, strKey.c_str(), pJsonStruct); |
| 1016 | if (cJSON_GetObjectItem(pFocusData, strKey.c_str()) == NULL) |
| 1017 | { |
| 1018 | return(false); |
| 1019 | } |
| 1020 | #if __cplusplus < 201101L |
| 1021 | std::map<std::string, CJsonObject*>::iterator iter = m_mapJsonObjectRef.find(strKey); |
| 1022 | #else |
| 1023 | auto iter = m_mapJsonObjectRef.find(strKey); |
| 1024 | #endif |
| 1025 | if (iter != m_mapJsonObjectRef.end()) |
| 1026 | { |
| 1027 | if (iter->second != NULL) |
| 1028 | { |
| 1029 | delete (iter->second); |
| 1030 | iter->second = NULL; |
| 1031 | } |
| 1032 | m_mapJsonObjectRef.erase(iter); |
| 1033 | } |
no test coverage detected