[DEV] add a machine generation of json (remove the space and indentation ...)

This commit is contained in:
Edouard DUPIN 2016-05-24 21:47:00 +02:00
parent f8613d244e
commit b37e93ca37
18 changed files with 121 additions and 34 deletions

View File

@ -155,16 +155,6 @@ const ejson::Object::iterator ejson::Object::end() const {
return ejson::Object::iterator(*this, size());
}
std::string ejson::Object::generate() const {
std::string out;
if (m_data == nullptr) {
EJSON_ERROR("Can not remove (nullptr) ...");
return out;
}
static_cast<ejson::internal::Object*>(m_data.get())->iGenerate(out, 0);
return out;
}
#include <ejson/details/iterator.hxx>
template class ejson::iterator<ejson::Object>;

View File

@ -137,12 +137,6 @@ namespace ejson {
* @return New valid iterator on the next element or this.end()
*/
iterator remove(const iterator& _it);
public:
/**
* @brief generate a string that contain the created XML
* @return generated data
*/
std::string generate() const;
};
}

View File

@ -42,6 +42,26 @@ bool ejson::Value::exist() const {
return true;
}
std::string ejson::Value::generateHumanString() const {
std::string out;
if (m_data == nullptr) {
EJSON_ERROR("Can not remove (nullptr) ...");
return out;
}
static_cast<ejson::internal::Value*>(m_data.get())->iGenerate(out, 0);
return out;
}
std::string ejson::Value::generateMachineString() const {
std::string out;
if (m_data == nullptr) {
EJSON_ERROR("Can not remove (nullptr) ...");
return out;
}
static_cast<ejson::internal::Value*>(m_data.get())->iMachineGenerate(out);
return out;
}
/*
ejson::FilePos ejson::Value::getPos() const {
if (m_data == nullptr) {

View File

@ -187,6 +187,17 @@ namespace ejson {
* @return nullptr in an error occured, the pointer on the element otherwise
*/
ejson::Value clone() const;
public:
/**
* @brief generate a string that contain the created JSON
* @return generated data
*/
std::string generateHumanString() const;
/**
* @brief generate a string that contain the created JSON
* @return generated data
*/
std::string generateMachineString() const;
};
//! @not_in_doc
std::ostream& operator <<(std::ostream& _os, const ejson::Value& _obj);

View File

@ -206,6 +206,22 @@ bool ejson::internal::Array::iGenerate(std::string& _data, size_t _indent) const
return true;
}
void ejson::internal::Array::iMachineGenerate(std::string& _data) const {
_data += "[";
bool needComa = false;
for (size_t iii=0; iii<m_value.size() ; iii++) {
if (m_value[iii] == nullptr) {
continue;
}
if (needComa == true) {
_data += ",";
}
m_value[iii]->iMachineGenerate(_data);
needComa = true;
}
_data += "]";
}
size_t ejson::internal::Array::size() const {
return m_value.size();
}

View File

@ -61,6 +61,7 @@ namespace ejson {
public:
bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
bool iGenerate(std::string& _data, size_t _indent) const override;
void iMachineGenerate(std::string& _data) const override;
void clear() override;
bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
ememory::SharedPtr<ejson::internal::Value> clone() const override;

View File

@ -55,7 +55,7 @@ bool ejson::internal::Boolean::iParse(const std::string& _data, size_t& _pos, ej
bool ejson::internal::Boolean::iGenerate(std::string& _data, size_t _indent) const {
if (true == m_value) {
if (m_value == true) {
_data += "true";
} else {
_data += "false";
@ -63,6 +63,14 @@ bool ejson::internal::Boolean::iGenerate(std::string& _data, size_t _indent) con
return true;
}
void ejson::internal::Boolean::iMachineGenerate(std::string& _data) const {
if (m_value == true) {
_data += "true";
} else {
_data += "false";
}
}
bool ejson::internal::Boolean::transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) {
if (_obj == nullptr) {

View File

@ -43,6 +43,7 @@ namespace ejson {
public:
bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
bool iGenerate(std::string& _data, size_t _indent) const override;
void iMachineGenerate(std::string& _data) const override;
bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
ememory::SharedPtr<ejson::internal::Value> clone() const override;
};

View File

@ -40,6 +40,10 @@ bool ejson::internal::Null::iGenerate(std::string& _data, size_t _indent) const
return true;
}
void ejson::internal::Null::iMachineGenerate(std::string& _data) const {
_data += "null";
}
bool ejson::internal::Null::transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) {
if (_obj == nullptr) {

View File

@ -28,6 +28,7 @@ namespace ejson {
public:
bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
bool iGenerate(std::string& _data, size_t _indent) const override;
void iMachineGenerate(std::string& _data) const override;
bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
ememory::SharedPtr<ejson::internal::Value> clone() const override;
};

View File

@ -40,8 +40,18 @@ bool ejson::internal::Number::iParse(const std::string& _data, size_t& _pos, ejs
return false;
}
bool ejson::internal::Number::iGenerate(std::string& _data, size_t _indent) const {
// special thing to remove .000000 at the end of perfect number ...
int64_t tmpVal = m_value;
if (double(tmpVal) == m_value) {
_data += etk::to_string(tmpVal);
} else {
_data += etk::to_string(m_value);
}
return true;
}
void ejson::internal::Number::iMachineGenerate(std::string& _data) const {
// special thing to remove .000000 at the end of perfect number ...
int64_t tmpVal = m_value;
if ((double)tmpVal == m_value) {
@ -49,7 +59,6 @@ bool ejson::internal::Number::iGenerate(std::string& _data, size_t _indent) cons
} else {
_data += etk::to_string(m_value);
}
return true;
}

View File

@ -43,6 +43,7 @@ namespace ejson {
public:
bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
bool iGenerate(std::string& _data, size_t _indent) const override;
void iMachineGenerate(std::string& _data) const override;
bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
ememory::SharedPtr<ejson::internal::Value> clone() const override;
};

View File

@ -272,6 +272,22 @@ bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) cons
return true;
}
void ejson::internal::Object::iMachineGenerate(std::string& _data) const {
_data += "{";
bool needComa = false;
for (int32_t iii=0; iii<m_value.size(); ++iii) {
if (needComa == true) {
_data += ",";
}
needComa = true;
_data += "\"";
_data += m_value.getKey(iii);
_data += "\":";
m_value.getValue(iii)->iMachineGenerate(_data);
}
_data += "}";
}
bool ejson::internal::Object::exist(const std::string& _name) const {
return m_value.exist(_name);
}

View File

@ -36,6 +36,7 @@ namespace ejson {
*/
static ememory::SharedPtr<Object> create(const std::string& _data);
protected:
// TODO : Change this with a generic methode ...
etk::Hash<ememory::SharedPtr<ejson::internal::Value> > m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
public:
/**
@ -117,6 +118,7 @@ namespace ejson {
public:
bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
bool iGenerate(std::string& _data, size_t _indent) const override;
void iMachineGenerate(std::string& _data) const override;
void clear() override;
bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
ememory::SharedPtr<ejson::internal::Value> clone() const override;

View File

@ -53,11 +53,17 @@ bool ejson::internal::String::iParse(const std::string& _data, size_t& _pos, ejs
bool ejson::internal::String::iGenerate(std::string& _data, size_t _indent) const {
_data += "\"";;
// TODO : Manage the \" elements ....
_data += m_value;
_data += "\"";;
return true;
}
void ejson::internal::String::iMachineGenerate(std::string& _data) const {
_data += "\"";;
// TODO : Manage the \" elements ....
_data += m_value;
_data += "\"";;
}
bool ejson::internal::String::transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) {
if (_obj == nullptr) {

View File

@ -41,10 +41,11 @@ namespace ejson {
*/
const std::string& get() const;
public:
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
virtual bool iGenerate(std::string& _data, size_t _indent) const override;
virtual bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
virtual ememory::SharedPtr<ejson::internal::Value> clone() const override;
bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override;
bool iGenerate(std::string& _data, size_t _indent) const override;
void iMachineGenerate(std::string& _data) const override;
bool transfertIn(ememory::SharedPtr<ejson::internal::Value> _obj) override;
ememory::SharedPtr<ejson::internal::Value> clone() const override;
};
}
}

View File

@ -74,13 +74,19 @@ namespace ejson {
ejson::FilePos& _filePos,
ejson::internal::Document& _doc) = 0;
/**
* @brief generate a string with the tree of the xml
* @brief generate a string with the tree of the json
* @param[in,out] _data string where to add the elements
* @param[in] _indent current indentation of the file
* @return false if an error occured.
*/
virtual bool iGenerate(std::string& _data,
size_t _indent) const = 0;
/**
* @brief generate a string with the tree of the json (not human readable ==> for computer transfer)
* @param[in,out] _data string where to add the elements
* @return false if an error occured.
*/
virtual void iMachineGenerate(std::string& _data) const = 0;
/**
* @brief Display the Document on console
*/

View File

@ -12,31 +12,31 @@
std::ostream& ejson::operator <<(std::ostream& _os, enum ejson::valueType _obj) {
switch (_obj) {
case ejson::valueType::unknow:
_os << "exml::valueType::unknow";
_os << "ejson::valueType::unknow";
break;
case ejson::valueType::value:
_os << "exml::valueType::value";
_os << "ejson::valueType::value";
break;
case ejson::valueType::document:
_os << "exml::valueType::document";
_os << "ejson::valueType::document";
break;
case ejson::valueType::array:
_os << "exml::valueType::array";
_os << "ejson::valueType::array";
break;
case ejson::valueType::object:
_os << "exml::valueType::object";
_os << "ejson::valueType::object";
break;
case ejson::valueType::string:
_os << "exml::valueType::string";
_os << "ejson::valueType::string";
break;
case ejson::valueType::number:
_os << "exml::valueType::number";
_os << "ejson::valueType::number";
break;
case ejson::valueType::null:
_os << "exml::valueType::null";
_os << "ejson::valueType::null";
break;
case ejson::valueType::boolean:
_os << "exml::valueType::boolean";
_os << "ejson::valueType::boolean";
break;
}
return _os;