2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-10 09:47:41 +08:00
|
|
|
https://axis-project.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "DictionaryHelper.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
static DictionaryHelper* sharedHelper = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DictionaryHelper::DictionaryHelper() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
DictionaryHelper::~DictionaryHelper() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
DictionaryHelper* DictionaryHelper::getInstance()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!sharedHelper)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
sharedHelper = new DictionaryHelper();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return sharedHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryHelper::destroyInstance()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(sharedHelper);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& DictionaryHelper::getSubDictionary_json(const rapidjson::Value& root, const char* key)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return root[key];
|
2019-11-23 20:27:39 +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)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return root[key][idx];
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& DictionaryHelper::getSubDictionary_json(const rapidjson::Value& root, int idx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return root[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
int DictionaryHelper::getIntValue_json(const rapidjson::Value& root, const char* key, int def)
|
|
|
|
{
|
|
|
|
int nRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.HasMember(key));
|
|
|
|
AX_BREAK_IF(root[key].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
nRet = root[key].GetInt();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float DictionaryHelper::getFloatValue_json(const rapidjson::Value& root, const char* key, float def)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float fRet = def;
|
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.HasMember(key));
|
|
|
|
AX_BREAK_IF(root[key].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
fRet = (float)root[key].GetDouble();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::getBooleanValue_json(const rapidjson::Value& root, const char* key, bool def)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool bRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.HasMember(key));
|
|
|
|
AX_BREAK_IF(root[key].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
bRet = root[key].GetBool();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* DictionaryHelper::getStringValue_json(const rapidjson::Value& root, const char* key, const char* def)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
const char* sRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.HasMember(key));
|
|
|
|
AX_BREAK_IF(root[key].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
sRet = root[key].GetString();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return sRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DictionaryHelper::getArrayCount_json(const rapidjson::Value& root, const char* key, int def)
|
|
|
|
{
|
|
|
|
int nRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.HasMember(key));
|
|
|
|
AX_BREAK_IF(root[key].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
nRet = (int)(root[key].Size());
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int DictionaryHelper::getIntValueFromArray_json(const rapidjson::Value& root, const char* arrayKey, int idx, int def)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int nRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.HasMember(arrayKey));
|
|
|
|
AX_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey][idx].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
nRet = root[arrayKey][idx].GetInt();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float DictionaryHelper::getFloatValueFromArray_json(const rapidjson::Value& root,
|
|
|
|
const char* arrayKey,
|
|
|
|
int idx,
|
|
|
|
float def)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
float fRet = def;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey][idx].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
fRet = (float)root[arrayKey][idx].GetDouble();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::getBoolValueFromArray_json(const rapidjson::Value& root, const char* arrayKey, int idx, bool def)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool bRet = def;
|
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey][idx].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
bRet = root[arrayKey][idx].GetBool();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
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)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* sRet = def;
|
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey].IsNull());
|
|
|
|
AX_BREAK_IF(root[arrayKey][idx].IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
sRet = root[arrayKey][idx].GetString();
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return root[key][idx];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::checkObjectExist_json(const rapidjson::Value& root)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::checkObjectExist_json(const rapidjson::Value& root, const char* key)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
2019-11-23 20:27:39 +08:00
|
|
|
bRet = root.HasMember(key);
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool DictionaryHelper::checkObjectExist_json(const rapidjson::Value& root, int index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(root.IsNull());
|
|
|
|
AX_BREAK_IF(!root.IsArray());
|
|
|
|
AX_BREAK_IF(index < 0 || root.Size() <= (unsigned int)index);
|
2019-11-23 20:27:39 +08:00
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|