[DEV] std::map to etk::Hash due to a test error
This commit is contained in:
parent
53e2f04072
commit
f93e3bd9bc
128
ejson/Object.cpp
128
ejson/Object.cpp
@ -20,12 +20,12 @@
|
|||||||
#define __class__ "Object"
|
#define __class__ "Object"
|
||||||
|
|
||||||
void ejson::Object::clear() {
|
void ejson::Object::clear() {
|
||||||
for(auto &it : m_value) {
|
for (int32_t iii=0; iii<m_value.size(); ++iii) {
|
||||||
if (it.second == NULL) {
|
if (NULL == m_value[iii]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
delete(it.second);
|
delete(m_value[iii]);
|
||||||
it.second = NULL;
|
m_value[iii] = NULL;
|
||||||
}
|
}
|
||||||
m_value.clear();
|
m_value.clear();
|
||||||
}
|
}
|
||||||
@ -209,8 +209,8 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
|
|||||||
} else if (_indent<=1) {
|
} else if (_indent<=1) {
|
||||||
oneLine=false;
|
oneLine=false;
|
||||||
} else {
|
} else {
|
||||||
for(auto it = m_value.begin(); it != m_value.end(); ++it) {
|
for (int32_t iii=0; iii<m_value.size() ; iii++) {
|
||||||
ejson::Value* tmp = it->second;
|
ejson::Value* tmp = m_value[iii];
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -224,9 +224,9 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
|
|||||||
}
|
}
|
||||||
if (true == tmp->isString()) {
|
if (true == tmp->isString()) {
|
||||||
ejson::String* tmp2 = tmp->toString();
|
ejson::String* tmp2 = tmp->toString();
|
||||||
if (tmp2 != NULL) {
|
if (tmp2 != nullptr) {
|
||||||
if( tmp2->get().size()>25
|
if( tmp2->get().size()>25
|
||||||
|| it->first.size()>25) {
|
|| m_value.getKey(iii).size()>25) {
|
||||||
oneLine=false;
|
oneLine=false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -239,17 +239,15 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
|
|||||||
} else {
|
} else {
|
||||||
_data += "{\n";
|
_data += "{\n";
|
||||||
}
|
}
|
||||||
for(auto it = m_value.begin();
|
for (int32_t iii=0; iii<m_value.size() ; iii++) {
|
||||||
it != m_value.end();
|
|
||||||
++it) {
|
|
||||||
if (false == oneLine) {
|
if (false == oneLine) {
|
||||||
addIndent(_data, _indent);
|
addIndent(_data, _indent);
|
||||||
}
|
}
|
||||||
_data += "\"";
|
_data += "\"";
|
||||||
_data += it->first;
|
_data += m_value.getKey(iii);
|
||||||
_data += "\": ";
|
_data += "\": ";
|
||||||
it->second->iGenerate(_data, _indent+1);
|
m_value.getValue(iii)->iGenerate(_data, _indent+1);
|
||||||
if (it != m_value.end()) {
|
if (iii<m_value.size()-1) {
|
||||||
_data += ",";
|
_data += ",";
|
||||||
}
|
}
|
||||||
if (true == oneLine) {
|
if (true == oneLine) {
|
||||||
@ -266,111 +264,85 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ejson::Object::exist(const std::string& _name) const {
|
bool ejson::Object::exist(const std::string& _name) const {
|
||||||
return m_value.find(_name) != m_value.end();
|
return m_value.exist(_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::Value* ejson::Object::get(const std::string& _name) {
|
ejson::Value* ejson::Object::get(const std::string& _name) {
|
||||||
auto it = m_value.find(_name);
|
if (false == m_value.exist(_name)) {
|
||||||
if (it == m_value.end()) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return it->second;
|
return m_value[_name];
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejson::Value* ejson::Object::get(const std::string& _name) const {
|
const ejson::Value* ejson::Object::get(const std::string& _name) const {
|
||||||
auto it = m_value.find(_name);
|
if (false == m_value.exist(_name)) {
|
||||||
if (it == m_value.end()) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return it->second;
|
return m_value[_name];
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::Object* ejson::Object::getObject(const std::string& _name) {
|
ejson::Object* ejson::Object::getObject(const std::string& _name) {
|
||||||
auto it = m_value.find(_name);
|
ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<ejson::Object*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<ejson::Object*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejson::Object* ejson::Object::getObject(const std::string& _name) const {
|
const ejson::Object* ejson::Object::getObject(const std::string& _name) const {
|
||||||
auto it = m_value.find(_name);
|
const ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<const ejson::Object*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<const ejson::Object*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::Array* ejson::Object::getArray(const std::string& _name) {
|
ejson::Array* ejson::Object::getArray(const std::string& _name) {
|
||||||
auto it = m_value.find(_name);
|
ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<ejson::Array*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<ejson::Array*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejson::Array* ejson::Object::getArray(const std::string& _name) const {
|
const ejson::Array* ejson::Object::getArray(const std::string& _name) const {
|
||||||
auto it = m_value.find(_name);
|
const ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<const ejson::Array*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<const ejson::Array*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::Null* ejson::Object::getNull(const std::string& _name) {
|
ejson::Null* ejson::Object::getNull(const std::string& _name) {
|
||||||
auto it = m_value.find(_name);
|
ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<ejson::Null*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<ejson::Null*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejson::Null* ejson::Object::getNull(const std::string& _name) const {
|
const ejson::Null* ejson::Object::getNull(const std::string& _name) const {
|
||||||
auto it = m_value.find(_name);
|
const ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<const ejson::Null*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<const ejson::Null*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::String* ejson::Object::getString(const std::string& _name) {
|
ejson::String* ejson::Object::getString(const std::string& _name) {
|
||||||
auto it = m_value.find(_name);
|
ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<ejson::String*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<ejson::String*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejson::String* ejson::Object::getString(const std::string& _name) const {
|
const ejson::String* ejson::Object::getString(const std::string& _name) const {
|
||||||
auto it = m_value.find(_name);
|
const ejson::Value* tmp = get(_name);
|
||||||
if (it == m_value.end()) {
|
if (NULL == tmp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (NULL == it->second) {
|
return dynamic_cast<const ejson::String*>(tmp);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return dynamic_cast<const ejson::String*>(it->second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& ejson::Object::getStringValue(const std::string& _name) const {
|
const std::string& ejson::Object::getStringValue(const std::string& _name) const {
|
||||||
@ -446,13 +418,13 @@ bool ejson::Object::add(const std::string& _name, ejson::Value* _value) {
|
|||||||
if (_name.size() == 0) {
|
if (_name.size() == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto it = m_value.find(_name);
|
if (m_value.exist(_name)) {
|
||||||
if (it != m_value.end()) {
|
ejson::Value* tmp = m_value[_name];
|
||||||
delete(it->second);
|
delete(tmp);
|
||||||
it->second = _value;
|
m_value[_name] = _value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
m_value.insert(std::pair<std::string, ejson::Value*>(_name, _value));
|
m_value.add(_name, _value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,11 +470,13 @@ ejson::Value* ejson::Object::duplicate() const {
|
|||||||
JSON_ERROR("Allocation error ...");
|
JSON_ERROR("Allocation error ...");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
for(auto it = m_value.begin(); it != m_value.end(); ++it) {
|
for (int32_t iii=0; iii<m_value.size(); ++iii) {
|
||||||
if (it->second == NULL) {
|
ejson::Value* val = m_value.getValue(iii);
|
||||||
|
std::string key = m_value.getKey(iii);
|
||||||
|
if (NULL == val) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
output->add(it->first, it->second->duplicate());
|
output->add(key, val->duplicate());
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
#define __ETK_JSON_OBJECT_H__
|
#define __ETK_JSON_OBJECT_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <map>
|
#include <etk/Hash.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ namespace ejson {
|
|||||||
*/
|
*/
|
||||||
virtual ~Object() { };
|
virtual ~Object() { };
|
||||||
protected:
|
protected:
|
||||||
std::map<std::string, ejson::Value*> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
|
etk::Hash<ejson::Value*> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
|
||||||
public:
|
public:
|
||||||
// TODO : add direct id access....
|
// TODO : add direct id access....
|
||||||
/**
|
/**
|
||||||
@ -57,11 +57,7 @@ namespace ejson {
|
|||||||
* @return a vector of all name (key).
|
* @return a vector of all name (key).
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> getKeys() const {
|
std::vector<std::string> getKeys() const {
|
||||||
std::vector<std::string> keys;
|
return m_value.getKeys();
|
||||||
for (auto &it : m_value) {
|
|
||||||
keys.push_back(it.first);
|
|
||||||
}
|
|
||||||
return keys;
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @brief get the number of sub element in the current one
|
* @brief get the number of sub element in the current one
|
||||||
@ -76,33 +72,19 @@ namespace ejson {
|
|||||||
* @return NULL if the element does not exist.
|
* @return NULL if the element does not exist.
|
||||||
*/
|
*/
|
||||||
ejson::Value* get(size_t _id) {
|
ejson::Value* get(size_t _id) {
|
||||||
size_t id = 0;
|
return m_value[_id];
|
||||||
for(auto &it : m_value) {
|
|
||||||
if (id == _id) {
|
|
||||||
return it.second;
|
|
||||||
}
|
|
||||||
id++;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
};
|
};
|
||||||
//! @previous
|
//! @previous
|
||||||
const ejson::Value* get(size_t _id) const{
|
const ejson::Value* get(size_t _id) const{
|
||||||
size_t id = 0;
|
return m_value[_id];
|
||||||
for(auto &it : m_value) {
|
|
||||||
if (id == _id) {
|
|
||||||
return it.second;
|
|
||||||
}
|
|
||||||
id++;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
};
|
};
|
||||||
//! @previous
|
//! @previous
|
||||||
ejson::Value* operator[] (size_t _id) {
|
ejson::Value* operator[] (size_t _id) {
|
||||||
return get(_id);
|
return m_value[_id];
|
||||||
}
|
}
|
||||||
//! @previous
|
//! @previous
|
||||||
const ejson::Value* operator[] (size_t _id) const {
|
const ejson::Value* operator[] (size_t _id) const {
|
||||||
return get(_id);
|
return m_value[_id];
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @brief Get the element name (key).
|
* @brief Get the element name (key).
|
||||||
@ -110,13 +92,7 @@ namespace ejson {
|
|||||||
* @return The name (key).
|
* @return The name (key).
|
||||||
*/
|
*/
|
||||||
std::string getKey(size_t _id) const {
|
std::string getKey(size_t _id) const {
|
||||||
size_t id = 0;
|
return m_value.getKey(_id);
|
||||||
for(auto it = m_value.begin(); it != m_value.end(); ++it, ++id) {
|
|
||||||
if (id == _id) {
|
|
||||||
return it->first;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @brief get the sub element with his name (Casted as Object if it is possible)
|
* @brief get the sub element with his name (Casted as Object if it is possible)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user