2013-09-09 19:51:26 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2014-01-07 11:47:11 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "DictionaryHelper.h"
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
|
|
|
|
2013-11-14 14:05:24 +08:00
|
|
|
static DictionaryHelper* sharedHelper = nullptr;
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DictionaryHelper::DictionaryHelper() {}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DictionaryHelper::~DictionaryHelper() {}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2013-12-27 17:21:13 +08:00
|
|
|
DictionaryHelper* DictionaryHelper::getInstance()
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!sharedHelper)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
sharedHelper = new DictionaryHelper();
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
return sharedHelper;
|
|
|
|
}
|
|
|
|
|
2013-12-27 17:21:13 +08:00
|
|
|
void DictionaryHelper::destroyInstance()
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_SAFE_DELETE(sharedHelper);
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& DictionaryHelper::getSubDictionary_json(const rapidjson::Value& root, const char* key)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return root[key];
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& DictionaryHelper::getSubDictionary_json(const rapidjson::Value& root, const char* key, int idx)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
return root[key][idx];
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& DictionaryHelper::getSubDictionary_json(const rapidjson::Value& root, int idx)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
return root[idx];
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
int DictionaryHelper::getIntValue_json(const rapidjson::Value& root, const char* key, int def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
int nRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
2015-05-26 17:18:09 +08:00
|
|
|
CC_BREAK_IF(!root.HasMember(key));
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root[key].IsNull());
|
|
|
|
nRet = root[key].GetInt();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return nRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float DictionaryHelper::getFloatValue_json(const rapidjson::Value& root, const char* key, float def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float fRet = def;
|
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
2015-05-26 17:18:09 +08:00
|
|
|
CC_BREAK_IF(!root.HasMember(key));
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root[key].IsNull());
|
|
|
|
fRet = (float)root[key].GetDouble();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return fRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::getBooleanValue_json(const rapidjson::Value& root, const char* key, bool def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
bool bRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
2015-05-26 17:18:09 +08:00
|
|
|
CC_BREAK_IF(!root.HasMember(key));
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root[key].IsNull());
|
|
|
|
bRet = root[key].GetBool();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return bRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* DictionaryHelper::getStringValue_json(const rapidjson::Value& root, const char* key, const char* def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
const char* sRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
2015-05-26 17:18:09 +08:00
|
|
|
CC_BREAK_IF(!root.HasMember(key));
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root[key].IsNull());
|
|
|
|
sRet = root[key].GetString();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return sRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
int DictionaryHelper::getArrayCount_json(const rapidjson::Value& root, const char* key, int def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
int nRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
2015-05-26 17:18:09 +08:00
|
|
|
CC_BREAK_IF(!root.HasMember(key));
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root[key].IsNull());
|
|
|
|
nRet = (int)(root[key].Size());
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return nRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int DictionaryHelper::getIntValueFromArray_json(const rapidjson::Value& root, const char* arrayKey, int idx, int def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
int nRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
2015-05-26 17:18:09 +08:00
|
|
|
CC_BREAK_IF(!root.HasMember(arrayKey));
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey][idx].IsNull());
|
|
|
|
nRet = root[arrayKey][idx].GetInt();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return nRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float DictionaryHelper::getFloatValueFromArray_json(const rapidjson::Value& root,
|
|
|
|
const char* arrayKey,
|
|
|
|
int idx,
|
|
|
|
float def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
float fRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey][idx].IsNull());
|
|
|
|
fRet = (float)root[arrayKey][idx].GetDouble();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return fRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::getBoolValueFromArray_json(const rapidjson::Value& root, const char* arrayKey, int idx, bool def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool bRet = def;
|
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey][idx].IsNull());
|
|
|
|
bRet = root[arrayKey][idx].GetBool();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return bRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* DictionaryHelper::getStringValueFromArray_json(const rapidjson::Value& root,
|
|
|
|
const char* arrayKey,
|
|
|
|
int idx,
|
|
|
|
const char* def)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* sRet = def;
|
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
CC_BREAK_IF(root[arrayKey][idx].IsNull());
|
|
|
|
sRet = root[arrayKey][idx].GetString();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return sRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& DictionaryHelper::getDictionaryFromArray_json(const rapidjson::Value& root,
|
|
|
|
const char* key,
|
|
|
|
int idx)
|
2013-12-24 20:33:55 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return root[key][idx];
|
2013-12-24 20:33:55 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::checkObjectExist_json(const rapidjson::Value& root)
|
2013-12-24 20:33:55 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::checkObjectExist_json(const rapidjson::Value& root, const char* key)
|
2013-12-24 20:33:55 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
|
|
|
bRet = root.HasMember(key);
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::checkObjectExist_json(const rapidjson::Value& root, int index)
|
2013-12-24 20:33:55 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-24 20:33:55 +08:00
|
|
|
CC_BREAK_IF(root.IsNull());
|
|
|
|
CC_BREAK_IF(!root.IsArray());
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_BREAK_IF(index < 0 || root.Size() <= (unsigned int)index);
|
2013-12-24 20:33:55 +08:00
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|