axmol/extensions/cocostudio/DictionaryHelper.cpp

245 lines
6.7 KiB
C++
Raw Normal View History

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
https://adxeproject.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"
#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()
{
2021-12-25 10:04:45 +08:00
CC_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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
CC_BREAK_IF(!root.HasMember(key));
CC_BREAK_IF(root[key].IsNull());
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
CC_BREAK_IF(!root.HasMember(key));
CC_BREAK_IF(root[key].IsNull());
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
CC_BREAK_IF(!root.HasMember(key));
CC_BREAK_IF(root[key].IsNull());
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
CC_BREAK_IF(!root.HasMember(key));
CC_BREAK_IF(root[key].IsNull());
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
CC_BREAK_IF(!root.HasMember(key));
CC_BREAK_IF(root[key].IsNull());
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
CC_BREAK_IF(!root.HasMember(arrayKey));
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
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
{
2019-11-23 20:27:39 +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
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
{
2019-11-23 20:27:39 +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
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
{
2019-11-23 20:27:39 +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
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
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
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(root.IsNull());
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
{
2019-11-23 20:27:39 +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);
2019-11-23 20:27:39 +08:00
bRet = true;
} while (0);
return bRet;
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio