[DEV] add feature on json generation on Object

This commit is contained in:
Edouard DUPIN 2016-05-17 22:10:47 +02:00
parent c41bcd52d4
commit f8613d244e
5 changed files with 30 additions and 11 deletions

View File

@ -155,6 +155,16 @@ 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,6 +137,12 @@ 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

@ -213,11 +213,11 @@ bool ejson::internal::Object::iParse(const std::string& _data, size_t& _pos, ejs
return false;
}
bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) const {
bool oneLine=true;
bool oneLine = true;
if (m_value.size()>3) {
oneLine=false;
oneLine = false;
} else if (_indent<=1) {
oneLine=false;
oneLine = false;
} else {
for (int32_t iii=0; iii<m_value.size() ; iii++) {
ememory::SharedPtr<const ejson::internal::Value> tmp = m_value[iii];
@ -243,13 +243,13 @@ bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) cons
}
}
}
if (true == oneLine) {
if (oneLine == true) {
_data += "{ ";
} else {
_data += "{\n";
}
for (int32_t iii=0; iii<m_value.size() ; iii++) {
if (false == oneLine) {
for (int32_t iii=0; iii<m_value.size(); ++iii) {
if (oneLine == false) {
addIndent(_data, _indent);
}
_data += "\"";
@ -259,13 +259,13 @@ bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) cons
if (iii<m_value.size()-1) {
_data += ",";
}
if (true == oneLine) {
if (oneLine == true) {
_data += " ";
} else {
_data += "\n";
}
}
if (false == oneLine) {
if (oneLine == false) {
addIndent(_data, _indent-1);
}
_data += "}";

View File

@ -27,8 +27,11 @@ bool ejson::internal::Value::isWhiteChar(char32_t _val) {
return false;
}
void ejson::internal::Value::addIndent(std::string& _data, size_t _indent) const {
for (size_t iii=0; iii<_indent; iii++) {
void ejson::internal::Value::addIndent(std::string& _data, int32_t _indent) const {
if (_indent <= 0) {
return;
}
for (int32_t iii=0; iii<_indent; iii++) {
_data+="\t";
}
}

View File

@ -91,7 +91,7 @@ namespace ejson {
* @param[in,out] _data String where the indentation is done.
* @param[in] _indent Number of tab to add at the string.
*/
void addIndent(std::string& _data, size_t _indent) const;
void addIndent(std::string& _data, int32_t _indent) const;
/**
* @brief Display the cuurent element that is curently parse.
* @param[in] _val Char that is parsed.