[DEV] it works

This commit is contained in:
Edouard DUPIN 2013-07-31 22:16:10 +02:00
parent 217c194765
commit 9a05682468
8 changed files with 166 additions and 26 deletions

View File

@ -24,13 +24,99 @@ void ejson::Array::Clear(void)
bool ejson::Array::IParse(const etk::UString& _data, int32_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc)
{
JSON_PARSE_ELEMENT("start parse : 'Object' ");
for (int32_t iii=_pos+1; iii<_data.Size(); iii++) {
_filePos.Check(_data[iii]);
#ifdef ENABLE_DISPLAY_PARSED_ELEMENT
DrawElementParsed(_data[iii], _filePos);
#endif
ejson::filePos tmpPos;
if( _data[iii]==' '
|| _data[iii]=='\t'
|| _data[iii]=='\n'
|| _data[iii]=='\r') {
// white space ==> nothing to do ...
} else if(_data[iii]==']') {
// find end of value:
_pos=iii; // ==> return the end element type ==> usefull to check end and check if adding element is needed
return true;
} else if (_data[iii]=='{') {
// find an object:
JSON_PARSE_ELEMENT("find Object");
ejson::Object * tmpElement = new ejson::Object();
if (NULL==tmpElement) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object");
_pos=iii;
return false;
}
tmpElement->IParse(_data, iii, _filePos, _doc);
m_value.PushBack(tmpElement);
} else if (_data[iii]=='"') {
// find a string:
JSON_PARSE_ELEMENT("find String quoted");
ejson::String * tmpElement = new ejson::String(true);
if (NULL==tmpElement) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String");
_pos=iii;
return false;
}
tmpElement->IParse(_data, iii, _filePos, _doc);
m_value.PushBack(tmpElement);
} else if (_data[iii]=='[') {
// find a list:
JSON_PARSE_ELEMENT("find List");
ejson::Array * tmpElement = new ejson::Array();
if (NULL==tmpElement) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array");
_pos=iii;
return false;
}
tmpElement->IParse(_data, iii, _filePos, _doc);
m_value.PushBack(tmpElement);
} else if( CheckAvaillable(_data[iii]) ) {
// find a string without "" ==> special hook for the etk-json parser
JSON_PARSE_ELEMENT("find String");
ejson::String * tmpElement = new ejson::String(false);
if (NULL==tmpElement) {
EJSON_CREATE_ERROR(_doc, _data, iii-1, _filePos, "Allocation error in String");
_pos=iii;
return false;
}
tmpElement->IParse(_data, iii, _filePos, _doc);
JSON_PARSE_ELEMENT("find String (end)");
m_value.PushBack(tmpElement);
} else if(_data[iii]==',') {
// find Separator : Restart cycle ...
// TODO : check if element are separated with ','
} else {
// find an error ....
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Find '>' with no element in the element...");
// move the curent index
_pos = iii+1;
return false;
}
}
_pos = _data.Size();
return false;
}
bool ejson::Array::IGenerate(etk::UString& _data, int32_t _indent) const
{
return false;
_data += "[\n";
for (esize_t iii=0; iii<m_value.Size() ; iii++) {
AddIndent(_data, _indent);
if (NULL != m_value[iii]) {
m_value[iii]->IGenerate(_data, _indent+1);
if (iii<m_value.Size()-1) {
_data += ",";
}
}
_data += "\n";
}
AddIndent(_data, _indent-1);
_data += "]";
return true;
}

View File

@ -64,13 +64,15 @@ bool ejson::Object::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
}
}
} else if (CheckAvaillable(_data[iii]) ) {
currentName += _data[iii];
for (iii++; iii<_data.Size(); iii++) {
_filePos.Check(_data[iii]);
#ifdef ENABLE_DISPLAY_PARSED_ELEMENT
DrawElementParsed(_data[iii], _filePos);
#endif
if (CheckAvaillable(_data[iii])) {
if (false==CheckAvaillable(_data[iii])) {
mode = parseMiddle;
iii--;
break;
} else {
currentName += _data[iii];
@ -102,6 +104,7 @@ bool ejson::Object::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
}
tmpElement->IParse(_data, iii, _filePos, _doc);
m_value.Add(currentName, tmpElement);
currentName = "";
} else if (_data[iii]=='"') {
// find a string:
JSON_PARSE_ELEMENT("find String quoted");
@ -112,8 +115,8 @@ bool ejson::Object::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
return false;
}
tmpElement->IParse(_data, iii, _filePos, _doc);
JSON_WARNING(" add : " << currentName);
m_value.Add(currentName, tmpElement);
currentName = "";
} else if (_data[iii]=='[') {
// find a list:
JSON_PARSE_ELEMENT("find List");
@ -125,6 +128,7 @@ bool ejson::Object::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
}
tmpElement->IParse(_data, iii, _filePos, _doc);
m_value.Add(currentName, tmpElement);
currentName = "";
} else if( CheckAvaillable(_data[iii]) ) {
// find a string without "" ==> special hook for the etk-json parser
JSON_PARSE_ELEMENT("find String");
@ -134,11 +138,14 @@ bool ejson::Object::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
_pos=iii;
return false;
}
iii--;
tmpElement->IParse(_data, iii, _filePos, _doc);
iii--;
//JSON_ERROR(" add : " << currentName );
m_value.Add(currentName, tmpElement);
currentName = "";
} else if(_data[iii]==',') {
// find Separator : Restart cycle ...
JSON_INFO("start new parse ...");
mode = parseName;
currentName = "";
} else {
@ -156,7 +163,6 @@ bool ejson::Object::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
}
bool ejson::Object::IGenerate(etk::UString& _data, int32_t _indent) const
{
AddIndent(_data, _indent);
_data += "{\n";
for (esize_t iii=0; iii<m_value.Size() ; iii++) {
AddIndent(_data, _indent);
@ -164,10 +170,13 @@ bool ejson::Object::IGenerate(etk::UString& _data, int32_t _indent) const
_data += m_value.GetKey(iii);
_data += "\": ";
m_value.GetValue(iii)->IGenerate(_data, _indent+1);
if (iii<m_value.Size()-1) {
_data += ",";
}
_data += "\n";
}
AddIndent(_data, _indent);
_data += "}\n";
AddIndent(_data, _indent-1);
_data += "}";
return true;
}

View File

@ -38,7 +38,7 @@ bool ejson::String::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
// white space ==> nothing to do ...
} else if(m_quoted==true) {
// TODO : manage \x
if( _data[iii]!= '"') {
if( _data[iii]!= '\"') {
m_value += _data[iii];
} else {
_pos = iii;
@ -65,13 +65,13 @@ bool ejson::String::IParse(const etk::UString& _data, int32_t& _pos, ejson::file
bool ejson::String::IGenerate(etk::UString& _data, int32_t _indent) const
{
if (m_quoted==true) {
_data += '"';
}
//if (m_quoted==true) {
_data += "\"";;
//}
_data += m_value;
if (m_quoted==true) {
_data += '"';
}
//if (m_quoted==true) {
_data += "\"";;
//}
return true;
}

View File

@ -75,6 +75,7 @@ bool ejson::Value::CheckAvaillable(const etk::UniChar& _val, bool _firstChar) co
|| _val == '+'
|| _val == ','
|| _val == '/'
|| _val == ':'
|| _val == ';'
|| _val == '<'
|| _val == '='

View File

@ -15,13 +15,13 @@
namespace ejson
{
#define ENABLE_DISPLAY_PARSED_ELEMENT
#if 0
//#define ENABLE_DISPLAY_PARSED_ELEMENT
#if 1
#define JSON_PARSE_ELEMENT JSON_VERBOSE
#else
#define JSON_PARSE_ELEMENT JSON_DEBUG
#endif
#if 0
#if 1
#define JSON_PARSE_ATTRIBUTE JSON_VERBOSE
#else
#define JSON_PARSE_ATTRIBUTE JSON_DEBUG

View File

@ -37,13 +37,10 @@ ejson::Document::~Document(void)
bool ejson::Document::IGenerate(etk::UString& _data, int32_t _indent) const
{
AddIndent(_data, _indent);
_data += "{\n";
if (NULL!=m_subElement) {
m_subElement->IGenerate(_data, _indent+1);
}
AddIndent(_data, _indent);
_data += "}\n";
_data += "\n";
return true;
}

View File

@ -86,7 +86,7 @@ namespace ejson
#define EJSON_CREATE_ERROR(doc,data,pos,filePos,comment) \
do { \
JSON_CRITICAL(comment); \
JSON_ERROR(comment); \
(doc).CreateError((data),(pos),(filePos),(comment)); \
} while (0)

View File

@ -61,7 +61,10 @@ void Init(void)
" \"id\": \"file\",\n"
" \"value\": \"File\",\n"
" \"popup\": {\n"
" \"menuitem\": {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n"
" \"menuitem\": {\n"
" \"value\": \"Close\",\n"
" \"onclick\": \"CloseDoc()\"\n"
" }\n"
" }\n"
" }\n"
"}\n";
@ -69,15 +72,29 @@ void Init(void)
-1,
reference);
l_list.PushBack(check);
// ------------------------------------------------------
reference = "{\n"
" \"menu\": {\n"
" \"id\": \"file\",\n"
" \"value\": \"File\",\n"
" \"popup\": {\n"
" \"menuitem\": [\n"
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n"
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n"
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n"
" {\n"
" \"value\": \"Close\",\n"
" \"onclick\": \"CloseDoc()\"\n"
" },\n"
" {\n"
" \"value\": \"New\",\n"
" \"onclick\": \"CreateNewDoc()\"\n"
" },\n"
" {\n"
" \"value\": \"Open\",\n"
" \"onclick\": \"OpenDoc()\"\n"
" },\n"
" {\n"
" \"value\": \"Close\",\n"
" \"onclick\": \"CloseDoc()\"\n"
" }\n"
" ]\n"
" }\n"
" }\n"
@ -86,6 +103,36 @@ void Init(void)
-1,
reference);
l_list.PushBack(check);
// ------------------------------------------------------
check.Set(reference,
-1,
"{\n"
" menu: {\n"
" id: file,\n"
" value: File,\n"
" popup: {\n"
" menuitem: [\n"
" {\n"
" value: Close,\n"
" onclick: \"CloseDoc()\"\n"
" },\n"
" {\n"
" value: New,\n"
" onclick: \"CreateNewDoc()\"\n"
" },\n"
" {\n"
" value: Open,\n"
" onclick: \"OpenDoc()\"\n"
" },\n"
" {\n"
" value: Close,\n"
" onclick: \"CloseDoc()\"\n"
" }\n"
" ]\n"
" }\n"
" }\n"
"}\n");
l_list.PushBack(check);
}
int main(int argc, const char *argv[])