[DEV] use std::map insted of etk::Hash
This commit is contained in:
parent
e1c8319249
commit
74f13fd919
@ -28,18 +28,35 @@ include_directories(.)
|
|||||||
#Create src file list
|
#Create src file list
|
||||||
set(src_files
|
set(src_files
|
||||||
ejson/debug.cpp
|
ejson/debug.cpp
|
||||||
|
ejson/debug.h
|
||||||
ejson/ejson.cpp
|
ejson/ejson.cpp
|
||||||
|
ejson/ejson.h
|
||||||
ejson/Array.cpp
|
ejson/Array.cpp
|
||||||
|
ejson/Array.h
|
||||||
ejson/Boolean.cpp
|
ejson/Boolean.cpp
|
||||||
|
ejson/Boolean.h
|
||||||
ejson/Null.cpp
|
ejson/Null.cpp
|
||||||
|
ejson/Null.h
|
||||||
ejson/Number.cpp
|
ejson/Number.cpp
|
||||||
|
ejson/Number.h
|
||||||
ejson/String.cpp
|
ejson/String.cpp
|
||||||
|
ejson/String.h
|
||||||
ejson/Object.cpp
|
ejson/Object.cpp
|
||||||
|
ejson/Object.h
|
||||||
ejson/Value.cpp
|
ejson/Value.cpp
|
||||||
|
ejson/Value.h
|
||||||
)
|
)
|
||||||
|
|
||||||
add_definitions( -DDEBUG_LEVEL=3 )
|
add_definitions( -DDEBUG_LEVEL=3 )
|
||||||
add_definitions( -DDEBUG=1 )
|
add_definitions( -DDEBUG=1 )
|
||||||
|
if (APPLE)
|
||||||
|
add_definitions( -D__TARGET_OS__MacOs )
|
||||||
|
elseif (UNIX)
|
||||||
|
add_definitions( -D__TARGET_OS__Linux )
|
||||||
|
elseif (WIN32)
|
||||||
|
add_definitions( -D__TARGET_OS__Windows )
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
|
||||||
include_directories(${etk_SOURCE_DIR})
|
include_directories(${etk_SOURCE_DIR})
|
||||||
include_directories(${linearmath_SOURCE_DIR}/bullet/src/)
|
include_directories(${linearmath_SOURCE_DIR}/bullet/src/)
|
||||||
|
12
ejson/Array.h
Normal file → Executable file
12
ejson/Array.h
Normal file → Executable file
@ -10,8 +10,6 @@
|
|||||||
#define __ETK_JSON_ARRAY_H__
|
#define __ETK_JSON_ARRAY_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
@ -165,16 +163,6 @@ namespace ejson {
|
|||||||
public: // herited function :
|
public: // herited function :
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeArray;
|
|
||||||
};
|
|
||||||
virtual ejson::Array* toArray(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
//! @previous
|
|
||||||
virtual const ejson::Array* toArray(void) const {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual void clear(void);
|
virtual void clear(void);
|
||||||
virtual bool transfertIn(ejson::Value* _obj);
|
virtual bool transfertIn(ejson::Value* _obj);
|
||||||
virtual ejson::Value* duplicate(void) const;
|
virtual ejson::Value* duplicate(void) const;
|
||||||
|
12
ejson/Boolean.h
Normal file → Executable file
12
ejson/Boolean.h
Normal file → Executable file
@ -10,8 +10,6 @@
|
|||||||
#define __ETK_JSON_BOOLEAN_H__
|
#define __ETK_JSON_BOOLEAN_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
@ -50,16 +48,6 @@ namespace ejson {
|
|||||||
public: // herited function :
|
public: // herited function :
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeString;
|
|
||||||
};
|
|
||||||
virtual ejson::Boolean* toBoolean(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
//! @previous
|
|
||||||
virtual const ejson::Boolean* toBoolean(void) const{
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual bool transfertIn(ejson::Value* _obj);
|
virtual bool transfertIn(ejson::Value* _obj);
|
||||||
virtual ejson::Value* duplicate(void) const;
|
virtual ejson::Value* duplicate(void) const;
|
||||||
};
|
};
|
||||||
|
12
ejson/Null.h
Normal file → Executable file
12
ejson/Null.h
Normal file → Executable file
@ -10,8 +10,6 @@
|
|||||||
#define __ETK_JSON_NULL_H__
|
#define __ETK_JSON_NULL_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
@ -28,16 +26,6 @@ namespace ejson {
|
|||||||
public: // herited function :
|
public: // herited function :
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeString;
|
|
||||||
};
|
|
||||||
virtual ejson::Null* toNull(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
//! @previous
|
|
||||||
virtual const ejson::Null* toNull(void) const{
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual bool transfertIn(ejson::Value* _obj);
|
virtual bool transfertIn(ejson::Value* _obj);
|
||||||
virtual ejson::Value* duplicate(void) const;
|
virtual ejson::Value* duplicate(void) const;
|
||||||
};
|
};
|
||||||
|
12
ejson/Number.h
Normal file → Executable file
12
ejson/Number.h
Normal file → Executable file
@ -10,8 +10,6 @@
|
|||||||
#define __ETK_JSON_NUMBER_H__
|
#define __ETK_JSON_NUMBER_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
@ -62,16 +60,6 @@ namespace ejson {
|
|||||||
public: // herited function :
|
public: // herited function :
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeString;
|
|
||||||
};
|
|
||||||
virtual ejson::Number* toNumber(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
//! @previous
|
|
||||||
virtual const ejson::Number* toNumber(void) const{
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual bool transfertIn(ejson::Value* _obj);
|
virtual bool transfertIn(ejson::Value* _obj);
|
||||||
virtual ejson::Value* duplicate(void) const;
|
virtual ejson::Value* duplicate(void) const;
|
||||||
};
|
};
|
||||||
|
128
ejson/Object.cpp
128
ejson/Object.cpp
@ -20,12 +20,12 @@
|
|||||||
#define __class__ "Object"
|
#define __class__ "Object"
|
||||||
|
|
||||||
void ejson::Object::clear(void) {
|
void ejson::Object::clear(void) {
|
||||||
for (int32_t iii=0; iii<m_value.size(); ++iii) {
|
for(auto &it : m_value) {
|
||||||
if (NULL == m_value[iii]) {
|
if (it.second == NULL) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
delete(m_value[iii]);
|
delete(it.second);
|
||||||
m_value[iii] = NULL;
|
it.second = 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 (int32_t iii=0; iii<m_value.size() ; iii++) {
|
for(auto it = m_value.begin(); it != m_value.end(); ++it) {
|
||||||
ejson::Value* tmp = m_value[iii];
|
ejson::Value* tmp = it->second;
|
||||||
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 (NULL!=tmp2) {
|
if (tmp2 != NULL) {
|
||||||
if( tmp2->get().size()>25
|
if( tmp2->get().size()>25
|
||||||
|| m_value.getKey(iii).size()>25) {
|
|| it->first.size()>25) {
|
||||||
oneLine=false;
|
oneLine=false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -239,15 +239,17 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
|
|||||||
} else {
|
} else {
|
||||||
_data += "{\n";
|
_data += "{\n";
|
||||||
}
|
}
|
||||||
for (int32_t iii=0; iii<m_value.size() ; iii++) {
|
for(auto it = m_value.begin();
|
||||||
|
it != m_value.end();
|
||||||
|
++it) {
|
||||||
if (false == oneLine) {
|
if (false == oneLine) {
|
||||||
addIndent(_data, _indent);
|
addIndent(_data, _indent);
|
||||||
}
|
}
|
||||||
_data += "\"";
|
_data += "\"";
|
||||||
_data += m_value.getKey(iii);
|
_data += it->first;
|
||||||
_data += "\": ";
|
_data += "\": ";
|
||||||
m_value.getValue(iii)->iGenerate(_data, _indent+1);
|
it->second->iGenerate(_data, _indent+1);
|
||||||
if (iii<m_value.size()-1) {
|
if (it != m_value.end()) {
|
||||||
_data += ",";
|
_data += ",";
|
||||||
}
|
}
|
||||||
if (true == oneLine) {
|
if (true == oneLine) {
|
||||||
@ -264,85 +266,111 @@ 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.exist(_name);
|
return m_value.find(_name) != m_value.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::Value* ejson::Object::get(const std::string& _name) {
|
ejson::Value* ejson::Object::get(const std::string& _name) {
|
||||||
if (false == m_value.exist(_name)) {
|
auto it = m_value.find(_name);
|
||||||
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return m_value[_name];
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejson::Value* ejson::Object::get(const std::string& _name) const {
|
const ejson::Value* ejson::Object::get(const std::string& _name) const {
|
||||||
if (false == m_value.exist(_name)) {
|
auto it = m_value.find(_name);
|
||||||
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return m_value[_name];
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
ejson::Object* ejson::Object::getObject(const std::string& _name) {
|
ejson::Object* ejson::Object::getObject(const std::string& _name) {
|
||||||
ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toObject();
|
if (NULL == it->second) {
|
||||||
|
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 {
|
||||||
const ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toObject();
|
if (NULL == it->second) {
|
||||||
|
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) {
|
||||||
ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toArray();
|
if (NULL == it->second) {
|
||||||
|
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 {
|
||||||
const ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toArray();
|
if (NULL == it->second) {
|
||||||
|
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) {
|
||||||
ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toNull();
|
if (NULL == it->second) {
|
||||||
|
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 {
|
||||||
const ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toNull();
|
if (NULL == it->second) {
|
||||||
|
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) {
|
||||||
ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toString();
|
if (NULL == it->second) {
|
||||||
|
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 {
|
||||||
const ejson::Value* tmp = get(_name);
|
auto it = m_value.find(_name);
|
||||||
if (NULL == tmp) {
|
if (it == m_value.end()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return tmp->toString();
|
if (NULL == it->second) {
|
||||||
|
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 {
|
||||||
@ -418,13 +446,13 @@ bool ejson::Object::add(const std::string& _name, ejson::Value* _value) {
|
|||||||
if (_name.size() == 0) {
|
if (_name.size() == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (m_value.exist(_name)) {
|
auto it = m_value.find(_name);
|
||||||
ejson::Value* tmp = m_value[_name];
|
if (it != m_value.end()) {
|
||||||
delete(tmp);
|
delete(it->second);
|
||||||
m_value[_name] = _value;
|
it->second = _value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
m_value.add(_name, _value);
|
m_value.insert(std::pair<std::string, ejson::Value*>(_name, _value));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,13 +498,11 @@ ejson::Value* ejson::Object::duplicate(void) const {
|
|||||||
JSON_ERROR("Allocation error ...");
|
JSON_ERROR("Allocation error ...");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
for (int32_t iii=0; iii<m_value.size(); ++iii) {
|
for(auto it = m_value.begin(); it != m_value.end(); ++it) {
|
||||||
ejson::Value* val = m_value.getValue(iii);
|
if (it->second == NULL) {
|
||||||
std::string key = m_value.getKey(iii);
|
|
||||||
if (NULL == val) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
output->add(key, val->duplicate());
|
output->add(it->first, it->second->duplicate());
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
52
ejson/Object.h
Normal file → Executable file
52
ejson/Object.h
Normal file → Executable file
@ -10,9 +10,8 @@
|
|||||||
#define __ETK_JSON_OBJECT_H__
|
#define __ETK_JSON_OBJECT_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
#include <map>
|
||||||
#include <etk/Hash.h>
|
#include <algorithm>
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
@ -27,7 +26,7 @@ namespace ejson {
|
|||||||
*/
|
*/
|
||||||
virtual ~Object(void) { };
|
virtual ~Object(void) { };
|
||||||
protected:
|
protected:
|
||||||
etk::Hash<ejson::Value*> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
|
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 ...)
|
||||||
public:
|
public:
|
||||||
// TODO : add direct id access....
|
// TODO : add direct id access....
|
||||||
/**
|
/**
|
||||||
@ -52,12 +51,17 @@ namespace ejson {
|
|||||||
const ejson::Value* operator[] (const std::string& _name) const {
|
const ejson::Value* operator[] (const std::string& _name) const {
|
||||||
return get(_name);
|
return get(_name);
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Get all the element name (keys).
|
* @brief Get all the element name (keys).
|
||||||
* @return a vector of all name (key).
|
* @return a vector of all name (key).
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> getKeys(void) const {
|
std::vector<std::string> getKeys(void) const {
|
||||||
return m_value.getKeys();
|
std::vector<std::string> keys;
|
||||||
|
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
|
||||||
@ -72,19 +76,31 @@ 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) {
|
||||||
return m_value[_id];
|
size_t id = 0;
|
||||||
|
for(auto &it : m_value) {
|
||||||
|
if (id == _id) {
|
||||||
|
return it.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
};
|
};
|
||||||
//! @previous
|
//! @previous
|
||||||
const ejson::Value* get(size_t _id) const{
|
const ejson::Value* get(size_t _id) const{
|
||||||
return m_value[_id];
|
size_t id = 0;
|
||||||
|
for(auto &it : m_value) {
|
||||||
|
if (id == _id) {
|
||||||
|
return it.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
};
|
};
|
||||||
//! @previous
|
//! @previous
|
||||||
ejson::Value* operator[] (size_t _id) {
|
ejson::Value* operator[] (size_t _id) {
|
||||||
return m_value[_id];
|
return get(_id);
|
||||||
}
|
}
|
||||||
//! @previous
|
//! @previous
|
||||||
const ejson::Value* operator[] (size_t _id) const {
|
const ejson::Value* operator[] (size_t _id) const {
|
||||||
return m_value[_id];
|
return get(_id);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @brief Get the element name (key).
|
* @brief Get the element name (key).
|
||||||
@ -92,7 +108,13 @@ namespace ejson {
|
|||||||
* @return The name (key).
|
* @return The name (key).
|
||||||
*/
|
*/
|
||||||
std::string getKey(size_t _id) const {
|
std::string getKey(size_t _id) const {
|
||||||
return m_value.getKey(_id);
|
size_t id = 0;
|
||||||
|
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)
|
||||||
@ -207,16 +229,6 @@ namespace ejson {
|
|||||||
public: // herited function :
|
public: // herited function :
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeObject;
|
|
||||||
};
|
|
||||||
virtual ejson::Object* toObject(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
//! @previous
|
|
||||||
virtual const ejson::Object* toObject(void) const{
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual void clear(void);
|
virtual void clear(void);
|
||||||
virtual bool transfertIn(ejson::Value* _obj);
|
virtual bool transfertIn(ejson::Value* _obj);
|
||||||
virtual ejson::Value* duplicate(void) const;
|
virtual ejson::Value* duplicate(void) const;
|
||||||
|
12
ejson/String.h
Normal file → Executable file
12
ejson/String.h
Normal file → Executable file
@ -10,8 +10,6 @@
|
|||||||
#define __ETK_JSON_STRING_H__
|
#define __ETK_JSON_STRING_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
#include <ejson/Value.h>
|
#include <ejson/Value.h>
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
@ -48,16 +46,6 @@ namespace ejson {
|
|||||||
public: // herited function :
|
public: // herited function :
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeString;
|
|
||||||
};
|
|
||||||
virtual ejson::String* toString(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
//! @previous
|
|
||||||
virtual const ejson::String* toString(void) const {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual bool transfertIn(ejson::Value* _obj);
|
virtual bool transfertIn(ejson::Value* _obj);
|
||||||
virtual ejson::Value* duplicate(void) const;
|
virtual ejson::Value* duplicate(void) const;
|
||||||
};
|
};
|
||||||
|
@ -123,3 +123,46 @@ bool ejson::Value::checkNumber(char32_t _val) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ejson::Document* ejson::Value::toDocument(void) {
|
||||||
|
return dynamic_cast<ejson::Document*>(this);
|
||||||
|
};
|
||||||
|
const ejson::Document* ejson::Value::toDocument(void) const {
|
||||||
|
return dynamic_cast<const ejson::Document*>(this);
|
||||||
|
};
|
||||||
|
ejson::Array* ejson::Value::toArray(void) {
|
||||||
|
return dynamic_cast<ejson::Array*>(this);
|
||||||
|
};
|
||||||
|
const ejson::Array* ejson::Value::toArray(void) const{
|
||||||
|
return dynamic_cast<const ejson::Array*>(this);
|
||||||
|
};
|
||||||
|
ejson::Object* ejson::Value::toObject(void) {
|
||||||
|
return dynamic_cast<ejson::Object*>(this);
|
||||||
|
};
|
||||||
|
const ejson::Object* ejson::Value::toObject(void) const{
|
||||||
|
return dynamic_cast<const ejson::Object*>(this);
|
||||||
|
};
|
||||||
|
ejson::String* ejson::Value::toString(void) {
|
||||||
|
return dynamic_cast<ejson::String*>(this);
|
||||||
|
};
|
||||||
|
const ejson::String* ejson::Value::toString(void) const{
|
||||||
|
return dynamic_cast<const ejson::String*>(this);
|
||||||
|
};
|
||||||
|
ejson::Number* ejson::Value::toNumber(void) {
|
||||||
|
return dynamic_cast<ejson::Number*>(this);
|
||||||
|
};
|
||||||
|
const ejson::Number* ejson::Value::toNumber(void) const{
|
||||||
|
return dynamic_cast<const ejson::Number*>(this);
|
||||||
|
};
|
||||||
|
ejson::Boolean* ejson::Value::toBoolean(void) {
|
||||||
|
return dynamic_cast<ejson::Boolean*>(this);
|
||||||
|
};
|
||||||
|
const ejson::Boolean* ejson::Value::toBoolean(void) const{
|
||||||
|
return dynamic_cast<const ejson::Boolean*>(this);
|
||||||
|
};
|
||||||
|
ejson::Null* ejson::Value::toNull(void) {
|
||||||
|
return dynamic_cast<ejson::Null*>(this);
|
||||||
|
};
|
||||||
|
const ejson::Null* ejson::Value::toNull(void) const{
|
||||||
|
return dynamic_cast<const ejson::Null*>(this);
|
||||||
|
};
|
103
ejson/Value.h
Normal file → Executable file
103
ejson/Value.h
Normal file → Executable file
@ -10,8 +10,6 @@
|
|||||||
#define __ETK_JSON_VALUE_H__
|
#define __ETK_JSON_VALUE_H__
|
||||||
|
|
||||||
#include <etk/types.h>
|
#include <etk/types.h>
|
||||||
#include <etk/types.h>
|
|
||||||
#include <etk/math/Vector2D.h>
|
|
||||||
|
|
||||||
namespace ejson {
|
namespace ejson {
|
||||||
//#define ENABLE_DISPLAY_PARSED_ELEMENT
|
//#define ENABLE_DISPLAY_PARSED_ELEMENT
|
||||||
@ -32,18 +30,6 @@ namespace ejson {
|
|||||||
class Null;
|
class Null;
|
||||||
class Number;
|
class Number;
|
||||||
class String;
|
class String;
|
||||||
|
|
||||||
enum nodeType{
|
|
||||||
typeUnknow, //!< might be an error ...
|
|
||||||
typeValue, //!< XXXXXX:*
|
|
||||||
typeDocument, //!< all the file main access
|
|
||||||
typeArray, //!< [...]
|
|
||||||
typeString, //!< the ""
|
|
||||||
typeNumber, //!< the -1565.21515
|
|
||||||
typeBoolean, //!< the true and false
|
|
||||||
typeNull, //!< the null element
|
|
||||||
typeObject, //!< the { ... }
|
|
||||||
};
|
|
||||||
//! @not-in-doc
|
//! @not-in-doc
|
||||||
class filePos {
|
class filePos {
|
||||||
private:
|
private:
|
||||||
@ -145,14 +131,6 @@ namespace ejson {
|
|||||||
* @return false if an error occured.
|
* @return false if an error occured.
|
||||||
*/
|
*/
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const = 0;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const = 0;
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief get the node type.
|
|
||||||
* @return the type of the Node.
|
|
||||||
*/
|
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeValue;
|
|
||||||
};
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
* @brief add indentation of the string input.
|
* @brief add indentation of the string input.
|
||||||
@ -189,139 +167,111 @@ namespace ejson {
|
|||||||
* @brief Cast the element in a Value if it is possible.
|
* @brief Cast the element in a Value if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Value* toValue(void) {
|
ejson::Value* toValue(void) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Value* toValue(void) const {
|
const ejson::Value* toValue(void) const {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a Document if it is possible.
|
* @brief Cast the element in a Document if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Document* toDocument(void) {
|
ejson::Document* toDocument(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Document* toDocument(void) const {
|
const ejson::Document* toDocument(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a Array if it is possible.
|
* @brief Cast the element in a Array if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Array* toArray(void) {
|
ejson::Array* toArray(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Array* toArray(void) const{
|
const ejson::Array* toArray(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a Object if it is possible.
|
* @brief Cast the element in a Object if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Object* toObject(void) {
|
ejson::Object* toObject(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Object* toObject(void) const{
|
const ejson::Object* toObject(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a String if it is possible.
|
* @brief Cast the element in a String if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::String* toString(void) {
|
ejson::String* toString(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::String* toString(void) const{
|
const ejson::String* toString(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a Number if it is possible.
|
* @brief Cast the element in a Number if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Number* toNumber(void) {
|
ejson::Number* toNumber(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Number* toNumber(void) const{
|
const ejson::Number* toNumber(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a Boolean if it is possible.
|
* @brief Cast the element in a Boolean if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Boolean* toBoolean(void) {
|
ejson::Boolean* toBoolean(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Boolean* toBoolean(void) const{
|
const ejson::Boolean* toBoolean(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Cast the element in a Null if it is possible.
|
* @brief Cast the element in a Null if it is possible.
|
||||||
* @return pointer on the class or NULL.
|
* @return pointer on the class or NULL.
|
||||||
*/
|
*/
|
||||||
virtual ejson::Null* toNull(void) {
|
ejson::Null* toNull(void);
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
//! @previous
|
//! @previous
|
||||||
virtual const ejson::Null* toNull(void) const{
|
const ejson::Null* toNull(void) const;
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::Document
|
* @brief check if the node is a ejson::Document
|
||||||
* @return true if the node is a ejson::Document
|
* @return true if the node is a ejson::Document
|
||||||
*/
|
*/
|
||||||
bool isDocument(void) const {
|
bool isDocument(void) const {
|
||||||
return getType() == ejson::typeDocument;
|
return toDocument() != NULL;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::Array
|
* @brief check if the node is a ejson::Array
|
||||||
* @return true if the node is a ejson::Array
|
* @return true if the node is a ejson::Array
|
||||||
*/
|
*/
|
||||||
bool isArray(void) const {
|
bool isArray(void) const {
|
||||||
return getType() == ejson::typeArray;
|
return toArray() != NULL;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::Object
|
* @brief check if the node is a ejson::Object
|
||||||
* @return true if the node is a ejson::Object
|
* @return true if the node is a ejson::Object
|
||||||
*/
|
*/
|
||||||
bool isObject(void) const {
|
bool isObject(void) const {
|
||||||
return getType() == ejson::typeObject;
|
return toObject() != NULL;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::String
|
* @brief check if the node is a ejson::String
|
||||||
* @return true if the node is a ejson::String
|
* @return true if the node is a ejson::String
|
||||||
*/
|
*/
|
||||||
bool isString(void) const {
|
bool isString(void) const {
|
||||||
return getType() == ejson::typeString;
|
return toString() != NULL;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::Number
|
* @brief check if the node is a ejson::Number
|
||||||
* @return true if the node is a ejson::Number
|
* @return true if the node is a ejson::Number
|
||||||
*/
|
*/
|
||||||
bool isNumber(void) const {
|
bool isNumber(void) const {
|
||||||
return getType() == ejson::typeNumber;
|
return toNumber() != NULL;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::Boolean
|
* @brief check if the node is a ejson::Boolean
|
||||||
* @return true if the node is a ejson::Boolean
|
* @return true if the node is a ejson::Boolean
|
||||||
*/
|
*/
|
||||||
bool isBoolean(void) const {
|
bool isBoolean(void) const {
|
||||||
return getType() == ejson::typeBoolean;
|
return toBoolean() != NULL;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* @brief check if the node is a ejson::Null
|
* @brief check if the node is a ejson::Null
|
||||||
* @return true if the node is a ejson::Null
|
* @return true if the node is a ejson::Null
|
||||||
*/
|
*/
|
||||||
bool isNull(void) const {
|
bool isNull(void) const {
|
||||||
return getType() == ejson::typeNull;
|
return toNull() != NULL;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -353,5 +303,12 @@ namespace ejson {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include <ejson/Array.h>
|
||||||
|
#include <ejson/Boolean.h>
|
||||||
|
#include <ejson/Null.h>
|
||||||
|
#include <ejson/Number.h>
|
||||||
|
#include <ejson/Object.h>
|
||||||
|
#include <ejson/String.h>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
9
ejson/ejson.h
Normal file → Executable file
9
ejson/ejson.h
Normal file → Executable file
@ -76,17 +76,8 @@ namespace ejson {
|
|||||||
void createError(const std::string& _data, size_t _pos, const ejson::filePos& _filePos, const std::string& _comment);
|
void createError(const std::string& _data, size_t _pos, const ejson::filePos& _filePos, const std::string& _comment);
|
||||||
void displayError(void);
|
void displayError(void);
|
||||||
public: // herited function:
|
public: // herited function:
|
||||||
virtual enum nodeType getType(void) const {
|
|
||||||
return typeDocument;
|
|
||||||
};
|
|
||||||
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
|
||||||
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
virtual bool iGenerate(std::string& _data, size_t _indent) const;
|
||||||
virtual ejson::Document* toDocument(void) {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
virtual const ejson::Document* toDocument(void) const {
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user