[DEV] generate array and object in one line when they are simple

This commit is contained in:
Edouard DUPIN 2013-08-23 09:10:19 +02:00
parent dfe3beecd6
commit 17ca49b86f
3 changed files with 207 additions and 183 deletions

View File

@ -135,18 +135,58 @@ bool ejson::Array::IParse(const etk::UString& _data, int32_t& _pos, ejson::fileP
bool ejson::Array::IGenerate(etk::UString& _data, int32_t _indent) const bool ejson::Array::IGenerate(etk::UString& _data, int32_t _indent) const
{ {
_data += "[\n"; bool oneLine=true;
if (m_value.Size()>3) {
oneLine=false;
} else {
for (esize_t iii=0; iii<m_value.Size() ; iii++) { for (esize_t iii=0; iii<m_value.Size() ; iii++) {
ejson::Value* tmp = m_value[iii];
if (tmp == NULL) {
continue;
}
if (true==tmp->IsObject()) {
oneLine=false;
break;
}
if (true==tmp->IsArray()) {
oneLine=false;
break;
}
if (true==tmp->IsString()) {
ejson::String* tmp2 = tmp->ToString();
if (NULL!=tmp2) {
if(tmp2->GetValue().Size()>40) {
oneLine=false;
break;
}
}
}
}
}
if (true==oneLine) {
_data += "[ ";
} else {
_data += "[\n";
}
for (esize_t iii=0; iii<m_value.Size() ; iii++) {
if (false==oneLine) {
AddIndent(_data, _indent); AddIndent(_data, _indent);
}
if (NULL != m_value[iii]) { if (NULL != m_value[iii]) {
m_value[iii]->IGenerate(_data, _indent+1); m_value[iii]->IGenerate(_data, _indent+1);
if (iii<m_value.Size()-1) { if (iii<m_value.Size()-1) {
_data += ","; _data += ",";
} }
} }
if (true==oneLine) {
_data += " ";
} else {
_data += "\n"; _data += "\n";
} }
}
if (false==oneLine) {
AddIndent(_data, _indent-1); AddIndent(_data, _indent-1);
}
_data += "]"; _data += "]";
return true; return true;
} }

View File

@ -209,6 +209,8 @@ bool ejson::Object::IGenerate(etk::UString& _data, int32_t _indent) const
bool oneLine=true; bool oneLine=true;
if (m_value.Size()>3) { if (m_value.Size()>3) {
oneLine=false; oneLine=false;
} else if (_indent<=1) {
oneLine=false;
} else { } else {
for (esize_t iii=0; iii<m_value.Size() ; iii++) { for (esize_t iii=0; iii<m_value.Size() ; iii++) {
ejson::Value* tmp = m_value[iii]; ejson::Value* tmp = m_value[iii];
@ -226,7 +228,8 @@ bool ejson::Object::IGenerate(etk::UString& _data, int32_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 (NULL!=tmp2) {
if (tmp2->GetValue().Size()>50) { if( tmp2->GetValue().Size()>25
|| m_value.GetKey(iii).Size()>25) {
oneLine=false; oneLine=false;
break; break;
} }
@ -234,11 +237,10 @@ bool ejson::Object::IGenerate(etk::UString& _data, int32_t _indent) const
} }
} }
} }
if (true==oneLine) {
if (false==oneLine) {
_data += "{\n";
} else {
_data += "{ "; _data += "{ ";
} else {
_data += "{\n";
} }
for (esize_t iii=0; iii<m_value.Size() ; iii++) { for (esize_t iii=0; iii<m_value.Size() ; iii++) {
if (false==oneLine) { if (false==oneLine) {
@ -251,10 +253,10 @@ bool ejson::Object::IGenerate(etk::UString& _data, int32_t _indent) const
if (iii<m_value.Size()-1) { if (iii<m_value.Size()-1) {
_data += ","; _data += ",";
} }
if (false==oneLine) { if (true==oneLine) {
_data += "\n";
} else {
_data += " "; _data += " ";
} else {
_data += "\n";
} }
} }
if (false==oneLine) { if (false==oneLine) {

View File

@ -157,10 +157,7 @@ void Init(void)
" \"id\": \"file\",\n" " \"id\": \"file\",\n"
" \"value\": \"File\",\n" " \"value\": \"File\",\n"
" \"popup\": {\n" " \"popup\": {\n"
" \"menuitem\": {\n" " \"menuitem\": { \"value\": \"Close\", \"onclick\": \"CloseDoc()\" }\n"
" \"value\": \"Close\",\n"
" \"onclick\": \"CloseDoc()\"\n"
" }\n"
" }\n" " }\n"
" }\n" " }\n"
"}\n"; "}\n";
@ -175,22 +172,10 @@ void Init(void)
" \"value\": \"File\",\n" " \"value\": \"File\",\n"
" \"popup\": {\n" " \"popup\": {\n"
" \"menuitem\": [\n" " \"menuitem\": [\n"
" {\n" " { \"value\": \"Close\", \"onclick\": \"CloseDoc()\" },\n"
" \"value\": \"Close\",\n" " { \"value\": \"New\", \"onclick\": \"CreateNewDoc()\" },\n"
" \"onclick\": \"CloseDoc()\"\n" " { \"value\": \"Open\", \"onclick\": \"OpenDoc()\" },\n"
" },\n" " { \"value\": \"Close\", \"onclick\": \"CloseDoc()\" }\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"
" }\n" " }\n"
@ -273,10 +258,7 @@ void Init(void)
" \"Abbrev\": \"ISO 8879:1986\",\n" " \"Abbrev\": \"ISO 8879:1986\",\n"
" \"GlossDef\": {\n" " \"GlossDef\": {\n"
" \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n" " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
" \"GlossSeeAlso\": [\n" " \"GlossSeeAlso\": [ \"GML\", \"XML\" ]\n"
" \"GML\",\n"
" \"XML\"\n"
" ]\n"
" },\n" " },\n"
" \"GlossSee\": \"markup\"\n" " \"GlossSee\": \"markup\"\n"
" }\n" " }\n"
@ -295,18 +277,9 @@ void Init(void)
" \"value\": \"File\",\n" " \"value\": \"File\",\n"
" \"popup\": {\n" " \"popup\": {\n"
" \"menuitem\": [\n" " \"menuitem\": [\n"
" {\n" " { \"value\": \"New\", \"onclick\": \"CreateNewDoc()\" },\n"
" \"value\": \"New\",\n" " { \"value\": \"Open\", \"onclick\": \"OpenDoc()\" },\n"
" \"onclick\": \"CreateNewDoc()\"\n" " { \"value\": \"Close\", \"onclick\": \"CloseDoc()\" }\n"
" },\n"
" {\n"
" \"value\": \"Open\",\n"
" \"onclick\": \"OpenDoc()\"\n"
" },\n"
" {\n"
" \"value\": \"Close\",\n"
" \"onclick\": \"CloseDoc()\"\n"
" }\n"
" ]\n" " ]\n"
" }\n" " }\n"
" }\n" " }\n"
@ -316,10 +289,6 @@ void Init(void)
reference); reference);
l_list.PushBack(check); l_list.PushBack(check);
// ------------------------------------------------------ // ------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
reference = "{\n" reference = "{\n"
" \"widget\": {\n" " \"widget\": {\n"
" \"debug\": \"on\",\n" " \"debug\": \"on\",\n"
@ -353,10 +322,6 @@ void Init(void)
reference); reference);
l_list.PushBack(check); l_list.PushBack(check);
// ------------------------------------------------------ // ------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
/*
reference = "{\n" reference = "{\n"
" \"web-app\": {\n" " \"web-app\": {\n"
" \"servlet\": [\n" " \"servlet\": [\n"
@ -378,16 +343,16 @@ void Init(void)
" \"useJSP\": false,\n" " \"useJSP\": false,\n"
" \"jspListTemplate\": \"listTemplate.jsp\",\n" " \"jspListTemplate\": \"listTemplate.jsp\",\n"
" \"jspFileTemplate\": \"articleTemplate.jsp\",\n" " \"jspFileTemplate\": \"articleTemplate.jsp\",\n"
" \"cachePackageTagsTrack\": 200,\n" " \"cachePackageTagsTrack\": 200.000000,\n"
" \"cachePackageTagsStore\": 200,\n" " \"cachePackageTagsStore\": 200.000000,\n"
" \"cachePackageTagsRefresh\": 60,\n" " \"cachePackageTagsRefresh\": 60.000000,\n"
" \"cacheTemplatesTrack\": 100,\n" " \"cacheTemplatesTrack\": 100.000000,\n"
" \"cacheTemplatesStore\": 50,\n" " \"cacheTemplatesStore\": 50.000000,\n"
" \"cacheTemplatesRefresh\": 15,\n" " \"cacheTemplatesRefresh\": 15.000000,\n"
" \"cachePagesTrack\": 200,\n" " \"cachePagesTrack\": 200.000000,\n"
" \"cachePagesStore\": 100,\n" " \"cachePagesStore\": 100.000000,\n"
" \"cachePagesRefresh\": 10,\n" " \"cachePagesRefresh\": 10.000000,\n"
" \"cachePagesDirtyRead\": 10,\n" " \"cachePagesDirtyRead\": 10.000000,\n"
" \"searchEngineListTemplate\": \"forSearchEnginesList.htm\",\n" " \"searchEngineListTemplate\": \"forSearchEnginesList.htm\",\n"
" \"searchEngineFileTemplate\": \"forSearchEngines.htm\",\n" " \"searchEngineFileTemplate\": \"forSearchEngines.htm\",\n"
" \"searchEngineRobotsDb\": \"WEB-INF/robots.db\",\n" " \"searchEngineRobotsDb\": \"WEB-INF/robots.db\",\n"
@ -401,63 +366,58 @@ void Init(void)
" \"dataStorePassword\": \"dataStoreTestQuery\",\n" " \"dataStorePassword\": \"dataStoreTestQuery\",\n"
" \"dataStoreTestQuery\": \"SET NOCOUNT ON;select test='test';\",\n" " \"dataStoreTestQuery\": \"SET NOCOUNT ON;select test='test';\",\n"
" \"dataStoreLogFile\": \"/usr/local/tomcat/logs/datastore.log\",\n" " \"dataStoreLogFile\": \"/usr/local/tomcat/logs/datastore.log\",\n"
" \"dataStoreInitConns\": 10,\n" " \"dataStoreInitConns\": 10.000000,\n"
" \"dataStoreMaxConns\": 100,\n" " \"dataStoreMaxConns\": 100.000000,\n"
" \"dataStoreConnUsageLimit\": 100,\n" " \"dataStoreConnUsageLimit\": 100.000000,\n"
" \"dataStoreLogLevel\": \"debug\",\n" " \"dataStoreLogLevel\": \"debug\",\n"
" \"maxUrlLength\": 500\n" " \"maxUrlLength\": 500.000000\n"
" }\n" " }\n"
" },\n" " },\n"
" {\n" " {\n"
" \"servlet-name\": \"cofaxEmail\",\n" " \"servlet-name\": \"cofaxEmail\",\n"
" \"servlet-class\": \"org.cofax.cds.EmailServlet\",\n" " \"servlet-class\": \"org.cofax.cds.EmailServlet\",\n"
" \"init-param\": {\n" " \"init-param\": { \"mailHost\": \"mail1\", \"mailHostOverride\": \"mail2\" }\n"
" \"mailHost\": \"mail1\",\n" " },\n"
" \"mailHostOverride\": \"mail2\"}},\n"
" {\n" " {\n"
" \"servlet-name\": \"cofaxAdmin\",\n" " \"servlet-name\": \"cofaxAdmin\",\n"
" \"servlet-class\": \"org.cofax.cds.AdminServlet\"},\n" " \"servlet-class\": \"org.cofax.cds.AdminServlet\"\n"
" \n" " },\n"
" {\n" " { \"servlet-name\": \"fileServlet\", \"servlet-class\": \"org.cofax.cds.FileServlet\" },\n"
" \"servlet-name\": \"fileServlet\",\n"
" \"servlet-class\": \"org.cofax.cds.FileServlet\"},\n"
" {\n" " {\n"
" \"servlet-name\": \"cofaxTools\",\n" " \"servlet-name\": \"cofaxTools\",\n"
" \"servlet-class\": \"org.cofax.cms.CofaxToolsServlet\",\n" " \"servlet-class\": \"org.cofax.cms.CofaxToolsServlet\",\n"
" \"init-param\": {\n" " \"init-param\": {\n"
" \"templatePath\": \"toolstemplates/\",\n" " \"templatePath\": \"toolstemplates/\",\n"
" \"log\": 1,\n" " \"log\": 1.000000,\n"
" \"logLocation\": \"/usr/local/tomcat/logs/CofaxTools.log\",\n" " \"logLocation\": \"/usr/local/tomcat/logs/CofaxTools.log\",\n"
" \"logMaxSize\": \"\",\n" " \"logMaxSize\": \"\",\n"
" \"dataLog\": 1,\n" " \"dataLog\": 1.000000,\n"
" \"dataLogLocation\": \"/usr/local/tomcat/logs/dataLog.log\",\n" " \"dataLogLocation\": \"/usr/local/tomcat/logs/dataLog.log\",\n"
" \"dataLogMaxSize\": \"\",\n" " \"dataLogMaxSize\": \"\",\n"
" \"removePageCache\": \"/content/admin/remove?cache=pages&id=\",\n" " \"removePageCache\": \"/content/admin/remove?cache=pages&id=\",\n"
" \"removeTemplateCache\": \"/content/admin/remove?cache=templates&id=\",\n" " \"removeTemplateCache\": \"/content/admin/remove?cache=templates&id=\",\n"
" \"fileTransferFolder\": \"/usr/local/tomcat/webapps/content/fileTransferFolder\",\n" " \"fileTransferFolder\": \"/usr/local/tomcat/webapps/content/fileTransferFolder\",\n"
" \"lookInContext\": 1,\n" " \"lookInContext\": 1.000000,\n"
" \"adminGroupID\": 4,\n" " \"adminGroupID\": 4.000000,\n"
" \"betaServer\": true}}],\n" " \"betaServer\": true\n"
" }\n"
" }\n"
" ],\n"
" \"servlet-mapping\": {\n" " \"servlet-mapping\": {\n"
" \"cofaxCDS\": \"/\",\n" " \"cofaxCDS\": \"/\",\n"
" \"cofaxEmail\": \"/cofaxutil/aemail/*\",\n" " \"cofaxEmail\": \"/cofaxutil/aemail/*\",\n"
" \"cofaxAdmin\": \"/admin/*\",\n" " \"cofaxAdmin\": \"/admin/*\",\n"
" \"fileServlet\": \"/static/*\",\n" " \"fileServlet\": \"/static/*\",\n"
" \"cofaxTools\": \"/tools/*\"},\n" " \"cofaxTools\": \"/tools/*\"\n"
" \n" " },\n"
" \"taglib\": {\n" " \"taglib\": { \"taglib-uri\": \"cofax.tld\", \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\" }\n"
" \"taglib-uri\": \"cofax.tld\",\n" " }\n"
" \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\"}}}\n" "}\n";
" \n"
"";
check.Set(reference, check.Set(reference,
-1, -1,
reference); reference);
l_list.PushBack(check); l_list.PushBack(check);
// ------------------------------------------------------ // ------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
*/
reference = "{\n" reference = "{\n"
" \"menu\": {\n" " \"menu\": {\n"
" \"header\": \"SVG Viewer\",\n" " \"header\": \"SVG Viewer\",\n"
@ -485,15 +445,13 @@ void Init(void)
" { \"id\": \"Help\" },\n" " { \"id\": \"Help\" },\n"
" { \"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\" }\n" " { \"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\" }\n"
" ]\n" " ]\n"
"}}\n"; " }\n"
"}\n";
check.Set(reference, check.Set(reference,
-1, -1,
reference); reference);
l_list.PushBack(check); l_list.PushBack(check);
// ------------------------------------------------------ // ------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
} }
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
@ -554,16 +512,40 @@ int main(int argc, const char *argv[])
JSON_INFO("[TEST] " << sectionID << ":" << jjj << " { OK } Result in error (normal case)"); JSON_INFO("[TEST] " << sectionID << ":" << jjj << " { OK } Result in error (normal case)");
} else { } else {
JSON_ERROR("[TEST] " << sectionID << ":" << jjj << " {ERROR } different output"); JSON_ERROR("[TEST] " << sectionID << ":" << jjj << " {ERROR } different output");
JSON_ERROR("generate : \n" << out); etk::Vector<etk::UString> tmpout = out.Split('\n');
JSON_ERROR("reference : \n" << l_list[iii].m_ref); etk::Vector<etk::UString> tmpref = l_list[iii].m_ref.Split('\n');
//JSON_ERROR("generate : \n" << out);
//JSON_ERROR("reference : \n" << l_list[iii].m_ref);
for (int32_t jjj=0; jjj<tmpout.Size() || jjj<tmpref.Size(); ++jjj) {
if (jjj<tmpref.Size()) {
JSON_INFO("[" << jjj << "] " << tmpref[jjj] );
}
if (jjj<tmpout.Size()) {
if (jjj>=tmpref.Size() || tmpref[jjj] != tmpout[jjj]) {
JSON_ERROR("[" << jjj << "] " << tmpout[jjj] );
}
}
}
countError++; countError++;
} }
continue; continue;
} }
if (l_list[iii].m_errorPos==3) { if (l_list[iii].m_errorPos==3) {
JSON_ERROR("[TEST] " << sectionID << ":" << jjj << " {ERROR} checking result might be in error..."); JSON_ERROR("[TEST] " << sectionID << ":" << jjj << " {ERROR} checking result might be in error...");
JSON_ERROR("generate : \n" << out); etk::Vector<etk::UString> tmpout = out.Split('\n');
JSON_ERROR("reference : \n" << l_list[iii].m_ref); etk::Vector<etk::UString> tmpref = l_list[iii].m_ref.Split('\n');
//JSON_ERROR("generate : \n" << out);
//JSON_ERROR("reference : \n" << l_list[iii].m_ref);
for (int32_t jjj=0; jjj<tmpout.Size() || jjj<tmpref.Size(); ++jjj) {
if (jjj<tmpref.Size()) {
JSON_INFO("[" << jjj << "] " << tmpref[jjj] );
}
if (jjj<tmpout.Size()) {
if (jjj>=tmpref.Size() || tmpref[jjj] != tmpout[jjj]) {
JSON_ERROR("[" << jjj << "] " << tmpout[jjj] );
}
}
}
countError++; countError++;
continue; continue;
} }