[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()); 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> #include <ejson/details/iterator.hxx>
template class ejson::iterator<ejson::Object>; 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() * @return New valid iterator on the next element or this.end()
*/ */
iterator remove(const iterator& _it); iterator remove(const iterator& _it);
public:
/**
* @brief generate a string that contain the created XML
* @return generated data
*/
std::string generate() const;
}; };
} }

View File

@ -243,13 +243,13 @@ bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) cons
} }
} }
} }
if (true == oneLine) { if (oneLine == true) {
_data += "{ "; _data += "{ ";
} else { } else {
_data += "{\n"; _data += "{\n";
} }
for (int32_t iii=0; iii<m_value.size() ; iii++) { for (int32_t iii=0; iii<m_value.size(); ++iii) {
if (false == oneLine) { if (oneLine == false) {
addIndent(_data, _indent); addIndent(_data, _indent);
} }
_data += "\""; _data += "\"";
@ -259,13 +259,13 @@ bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) cons
if (iii<m_value.size()-1) { if (iii<m_value.size()-1) {
_data += ","; _data += ",";
} }
if (true == oneLine) { if (oneLine == true) {
_data += " "; _data += " ";
} else { } else {
_data += "\n"; _data += "\n";
} }
} }
if (false == oneLine) { if (oneLine == false) {
addIndent(_data, _indent-1); addIndent(_data, _indent-1);
} }
_data += "}"; _data += "}";

View File

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

View File

@ -91,7 +91,7 @@ namespace ejson {
* @param[in,out] _data String where the indentation is done. * @param[in,out] _data String where the indentation is done.
* @param[in] _indent Number of tab to add at the string. * @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. * @brief Display the cuurent element that is curently parse.
* @param[in] _val Char that is parsed. * @param[in] _val Char that is parsed.