axmol/extensions/CCArmature/external_tool/Json/CSContentJsonDictionary.cpp

376 lines
10 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/*
* Copyright (c) 2012 Chukong Technologies, Inc.
*
* http://www.cocostudio.com
* http://tools.cocoachina.com
*
* 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 <iostream>
#include "CSContentJsonDictionary.h"
namespace cs {
CSJsonDictionary::CSJsonDictionary()
{
_value.clear();
2013-06-06 12:02:54 +08:00
}
CSJsonDictionary::~CSJsonDictionary()
{
_value.clear();
2013-06-06 12:02:54 +08:00
}
void CSJsonDictionary::initWithDescription(const char *pszDescription)
{
CSJson::Reader cReader;
_value.clear();
2013-06-06 12:02:54 +08:00
if (pszDescription && *pszDescription)
{
std::string strValue = pszDescription;
cReader.parse(strValue, _value, false);
2013-06-06 12:02:54 +08:00
}
}
void CSJsonDictionary::initWithValue(CSJson::Value& value)
2013-06-06 12:02:54 +08:00
{
_value = value;
2013-06-06 12:02:54 +08:00
}
void CSJsonDictionary::insertItem(const char *pszKey, int nValue)
{
_value[pszKey] = nValue;
2013-06-06 12:02:54 +08:00
}
void CSJsonDictionary::insertItem(const char *pszKey, double fValue)
{
_value[pszKey] = fValue;
2013-06-06 12:02:54 +08:00
}
void CSJsonDictionary::insertItem(const char *pszKey, const char * pszValue)
{
_value[pszKey] = pszValue;
2013-06-06 12:02:54 +08:00
}
void CSJsonDictionary::insertItem(const char *pszKey, bool bValue)
{
_value[pszKey] = bValue;
2013-06-06 12:02:54 +08:00
}
void CSJsonDictionary::insertItem(const char *pszKey, CSJsonDictionary * subDictionary)
{
if (subDictionary)
_value[pszKey] = subDictionary->_value;
2013-06-06 12:02:54 +08:00
}
bool CSJsonDictionary::deleteItem(const char *pszKey)
{
if(!_value.isMember(pszKey))
2013-06-06 12:02:54 +08:00
return false;
_value.removeMember(pszKey);
2013-06-06 12:02:54 +08:00
return true;
}
void CSJsonDictionary::cleanUp()
{
_value.clear();
2013-06-06 12:02:54 +08:00
}
bool CSJsonDictionary::isKeyValidate(const char *pszKey)
{
return _value.isMember(pszKey);
2013-06-06 12:02:54 +08:00
}
int CSJsonDictionary::getItemIntValue(const char *pszKey, int nDefaultValue)
{
if (!isKeyValidate(pszKey, _value) || !_value[pszKey].isNumeric())
2013-06-06 12:02:54 +08:00
return nDefaultValue;
return _value[pszKey].asInt();
2013-06-06 12:02:54 +08:00
}
double CSJsonDictionary::getItemFloatValue(const char *pszKey, double fDefaultValue)
{
if (!isKeyValidate(pszKey, _value) || !_value[pszKey].isNumeric())
2013-06-06 12:02:54 +08:00
return fDefaultValue;
return _value[pszKey].asDouble();
2013-06-06 12:02:54 +08:00
}
const char * CSJsonDictionary::getItemStringValue(const char *pszKey)
{
if (!isKeyValidate(pszKey, _value) || !_value[pszKey].isString())
2013-06-06 12:02:54 +08:00
return NULL;
return _value[pszKey].asCString();
2013-06-06 12:02:54 +08:00
}
bool CSJsonDictionary::getItemBoolvalue(const char *pszKey, bool bDefaultValue)
{
if (!isKeyValidate(pszKey, _value) || !_value[pszKey].isBool())
2013-06-06 12:02:54 +08:00
return bDefaultValue;
return _value[pszKey].asBool();
2013-06-06 12:02:54 +08:00
}
CSJsonDictionary * CSJsonDictionary::getSubDictionary(const char *pszKey)
{
CSJsonDictionary * pNewDictionary;
if (!isKeyValidate(pszKey, _value) || (!_value[pszKey].isArray() &&
!_value[pszKey].isObject() &&
!_value[pszKey].isConvertibleTo(CSJson::arrayValue) &&
!_value[pszKey].isConvertibleTo(CSJson::objectValue)))
2013-06-06 12:02:54 +08:00
{
pNewDictionary = NULL;
}
else
{
pNewDictionary = new CSJsonDictionary();
pNewDictionary->initWithValue(_value[pszKey]);
2013-06-06 12:02:54 +08:00
}
return pNewDictionary;
}
std::string CSJsonDictionary::getDescription()
{
std::string strReturn = _value.toStyledString();
2013-06-06 12:02:54 +08:00
return strReturn;
}
bool CSJsonDictionary::insertItemToArray(const char *pszArrayKey, int nValue)
{
CSJson::Value array;
if(_value.isMember(pszArrayKey))
2013-06-06 12:02:54 +08:00
{
if (!_value[pszArrayKey].isArray() && !_value[pszArrayKey].isConvertibleTo(CSJson::arrayValue))
2013-06-06 12:02:54 +08:00
return false;
array = _value[pszArrayKey];
2013-06-06 12:02:54 +08:00
}
array.append(nValue);
_value[pszArrayKey] = array;
2013-06-06 12:02:54 +08:00
return true;
}
bool CSJsonDictionary::insertItemToArray(const char *pszArrayKey, double fValue)
{
CSJson::Value array;
if(_value.isMember(pszArrayKey))
2013-06-06 12:02:54 +08:00
{
if (!_value[pszArrayKey].isArray() && !_value[pszArrayKey].isConvertibleTo(CSJson::arrayValue))
2013-06-06 12:02:54 +08:00
return false;
array = _value[pszArrayKey];
2013-06-06 12:02:54 +08:00
}
array.append(fValue);
_value[pszArrayKey] = array;
2013-06-06 12:02:54 +08:00
return true;
}
bool CSJsonDictionary::insertItemToArray(const char *pszArrayKey, const char * pszValue)
{
CSJson::Value array;
if(_value.isMember(pszArrayKey))
2013-06-06 12:02:54 +08:00
{
if (!_value[pszArrayKey].isArray() && !_value[pszArrayKey].isConvertibleTo(CSJson::arrayValue))
2013-06-06 12:02:54 +08:00
return false;
array = _value[pszArrayKey];
2013-06-06 12:02:54 +08:00
}
array.append(pszValue);
_value[pszArrayKey] = array;
2013-06-06 12:02:54 +08:00
return true;
}
bool CSJsonDictionary::insertItemToArray(const char *pszArrayKey, CSJsonDictionary * subDictionary)
{
CSJson::Value array;
if(_value.isMember(pszArrayKey))
2013-06-06 12:02:54 +08:00
{
if (!_value[pszArrayKey].isArray() && !_value[pszArrayKey].isConvertibleTo(CSJson::arrayValue))
2013-06-06 12:02:54 +08:00
return false;
array = _value[pszArrayKey];
2013-06-06 12:02:54 +08:00
}
array.append(subDictionary->_value);
_value[pszArrayKey] = array;
2013-06-06 12:02:54 +08:00
return true;
}
int CSJsonDictionary::getItemCount()
{
return _value.size();
2013-06-06 12:02:54 +08:00
}
DicItemType CSJsonDictionary::getItemType(int nIndex)
{
return (DicItemType)_value[nIndex].type();
2013-06-06 12:02:54 +08:00
}
DicItemType CSJsonDictionary::getItemType(const char *pszKey)
{
return (DicItemType)_value[pszKey].type();
2013-06-06 12:02:54 +08:00
}
std::vector<std::string> CSJsonDictionary::getAllMemberNames()
{
return _value.getMemberNames();
2013-06-06 12:02:54 +08:00
}
int CSJsonDictionary::getArrayItemCount(const char *pszArrayKey)
{
int nRet = 0;
if (!isKeyValidate(pszArrayKey, _value) ||
(!_value[pszArrayKey].isArray() && !_value[pszArrayKey].isObject() &&
!_value[pszArrayKey].isConvertibleTo(CSJson::arrayValue) && !_value[pszArrayKey].isConvertibleTo(CSJson::objectValue)))
2013-06-06 12:02:54 +08:00
{
nRet = 0;
}
else
{
CSJson::Value arrayValue = _value[pszArrayKey];
2013-06-06 12:02:54 +08:00
nRet = arrayValue.size();
}
return nRet;
}
int CSJsonDictionary::getIntValueFromArray(const char *pszArrayKey, int nIndex, int nDefaultValue)
{
int nRet = nDefaultValue;
CSJson::Value * arrayValue = validateArrayItem(pszArrayKey, nIndex);
2013-06-06 12:02:54 +08:00
if (arrayValue)
{
if ((*arrayValue)[nIndex].isNumeric())
nRet = (*arrayValue)[nIndex].asInt();
}
return nRet;
}
double CSJsonDictionary::getFloatValueFromArray(const char *pszArrayKey, int nIndex, double fDefaultValue)
{
double fRet = fDefaultValue;
CSJson::Value * arrayValue = validateArrayItem(pszArrayKey, nIndex);
2013-06-06 12:02:54 +08:00
if (arrayValue)
{
if ((*arrayValue)[nIndex].isNumeric())
fRet = (*arrayValue)[nIndex].asDouble();
}
return fRet;
}
const char * CSJsonDictionary::getStringValueFromArray(const char *pszArrayKey, int nIndex)
{
CSJson::Value * arrayValue = validateArrayItem(pszArrayKey, nIndex);
2013-06-06 12:02:54 +08:00
if (arrayValue)
{
if ((*arrayValue)[nIndex].isString())
return (*arrayValue)[nIndex].asCString();
}
return NULL;
}
CSJsonDictionary * CSJsonDictionary::getSubItemFromArray(const char *pszArrayKey, int nIndex)
{
CSJson::Value * arrayValue = validateArrayItem(pszArrayKey, nIndex);
2013-06-06 12:02:54 +08:00
if (arrayValue)
{
if ((*arrayValue)[nIndex].isArray() || (*arrayValue)[nIndex].isObject())
{
CSJsonDictionary * pNewDictionary = new CSJsonDictionary();
pNewDictionary->initWithValue((*arrayValue)[nIndex]);
return pNewDictionary;
}
}
return NULL;
}
DicItemType CSJsonDictionary::getItemTypeFromArray(const char *pszArrayKey, int nIndex)
{
CSJson::Value * arrayValue = validateArrayItem(pszArrayKey, nIndex);
2013-06-06 12:02:54 +08:00
if (arrayValue)
return (DicItemType)((*arrayValue)[nIndex].type());
return (DicItemType)CSJson::nullValue;
2013-06-06 12:02:54 +08:00
}
inline bool CSJsonDictionary::isKeyValidate(const char *pszKey, CSJson::Value& root)
2013-06-06 12:02:54 +08:00
{
if (root.isNull() || !root.isMember(pszKey))
return false;
return true;
}
inline CSJson::Value * CSJsonDictionary::validateArrayItem(const char *pszArrayKey, int nIndex)
2013-06-06 12:02:54 +08:00
{
if (!isKeyValidate(pszArrayKey, _value) && !_value[pszArrayKey].isArray() && !_value[pszArrayKey].isConvertibleTo(CSJson::arrayValue))
2013-06-06 12:02:54 +08:00
return NULL;
if (!_value[pszArrayKey].isValidIndex(nIndex))
2013-06-06 12:02:54 +08:00
return NULL;
return &_value[pszArrayKey];
2013-06-06 12:02:54 +08:00
}
}