changed: rewritten the Serialize method in SpaceToJSON class

git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@429 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2012-07-15 22:53:24 +00:00
parent 2b88d011f6
commit f8d5a962ed
2 changed files with 68 additions and 83 deletions

View File

@ -42,41 +42,27 @@ namespace PT
{
void SpaceToJSON::ChangeSpacesToTable(const wchar_t * space_name)
void SpaceToJSON::Clear()
{
spaces_to_table.push_back(space_name);
numeric.clear();
boolean.clear();
table.clear();
}
void SpaceToJSON::ChangeSpacesToTable(const std::wstring & space_name)
void SpaceToJSON::TreatAsTable(const wchar_t * space_name)
{
spaces_to_table.push_back(space_name);
table.insert(space_name);
}
bool SpaceToJSON::HasUnnamedSpace(Space & space)
void SpaceToJSON::TreatAsTable(const std::wstring & space_name)
{
for(size_t i=0 ; i<space.spaces.size() ; ++i)
if( space.spaces[i]->name.empty() )
return true;
return false;
table.insert(space_name);
}
bool SpaceToJSON::HasSpace(Space & space, const std::wstring & name)
{
for(size_t i=0 ; i<space.spaces.size() ; ++i)
if( space.spaces[i]->name == name )
return true;
return false;
}
void SpaceToJSON::TreatAsNumeric(const wchar_t * name)
{
numeric.insert(name);
@ -115,6 +101,11 @@ bool SpaceToJSON::IsBool(const std::wstring & name)
return i != boolean.end();
}
bool SpaceToJSON::IsTable(const std::wstring & name)
{
std::set<std::wstring>::iterator i = table.find(name);
return i != table.end();
}

View File

@ -44,6 +44,7 @@
#include "space.h"
namespace PT
{
@ -52,8 +53,10 @@ class SpaceToJSON
{
public:
void ChangeSpacesToTable(const wchar_t * space_name);
void ChangeSpacesToTable(const std::wstring & space_name);
void Clear();
void TreatAsTable(const wchar_t * space_name);
void TreatAsTable(const std::wstring & space_name);
void TreatAsNumeric(const wchar_t * name);
void TreatAsNumeric(const std::wstring & name);
@ -61,10 +64,18 @@ public:
void TreatAsBool(const wchar_t * name);
void TreatAsBool(const std::wstring & name);
template<class Stream>
void Serialize(Space & space, Stream & out, bool use_indents = false);
private:
std::set<std::wstring> numeric, boolean, table;
template<class Stream>
void Serialize(Space & space, Stream & out, bool use_indents = false, int level = 0, bool use_comma = false, bool skip_name = false);
void Serialize(Space & space, Stream & out, bool use_indents, int level,
bool use_comma, bool treat_as_table, bool skip_name);
template<class Stream>
void SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level, bool use_comma);
@ -79,17 +90,10 @@ public:
void PrintLevel(Stream & out, bool use_indents, int level);
private:
bool HasUnnamedSpace(Space & space);
bool HasSpace(Space & space, const std::wstring & name);
bool IsNumeric(const std::wstring & name);
bool IsBool(const std::wstring & name);
bool IsTable(const std::wstring & name);
std::vector<std::wstring> spaces_to_table;
std::vector<bool> space_used;
std::set<std::wstring> numeric, boolean;
};
@ -221,7 +225,8 @@ bool is_special;
template<class Stream>
void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int level, bool use_comma, bool skip_name)
void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int level,
bool use_comma, bool treat_as_table, bool skip_name)
{
if( use_comma )
{
@ -233,78 +238,67 @@ void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int l
if( !skip_name )
{
if( !space.name.empty() )
if( space.name.empty() )
{
out << L"\"empty\": ";
}
else
{
PrintToken(out, space.name);
out << L": ";
}
}
if( treat_as_table )
out << L"[\n";
else
out << L"{\n";
bool printed_something = false;
if( !treat_as_table )
{
SerializeTableSingle(space, out, use_indents, level, false);
SerializeTableMulti(space, out, use_indents, level, !space.table_single.empty());
if( !space.table_single.empty() || !space.table.empty() )
printed_something = true;
space_used.resize(space.spaces.size());
for(size_t i=0 ; i<space_used.size() ; ++i)
space_used[i] = false;
for(size_t z=0 ; z<spaces_to_table.size() ; ++z)
{
if( HasSpace(space, spaces_to_table[z]) )
{
if( printed_something )
{
out << L",\n";
}
PrintToken(out, spaces_to_table[z]);
out << L": [\n";
/*
* !! IMPROVE ME when serializing a table
* we can make a test whether a space is empty and has a name
* in such a case put it as a string
* this is the same way as the json parser works
*
*/
for(size_t i=0 ; i<space.spaces.size() ; ++i)
{
if( space.spaces[i]->name == spaces_to_table[z] )
{
space_used[i] = true;
Serialize(*space.spaces[i], out, use_indents, level+1, i>0, true);
}
bool next_skip_name = treat_as_table;
bool next_is_table = IsTable(space.spaces[i]->name);
Serialize(*space.spaces[i], out, use_indents, level+1, printed_something, next_is_table, next_skip_name);
printed_something = true;
}
PrintLevel(out, use_indents, level);
if( treat_as_table )
out << L"]\n";
printed_something = true;
}
}
for(size_t i=0 ; i<space.spaces.size() ; ++i)
{
if( !space_used[i] )
{
Serialize(*space.spaces[i], out, use_indents, level+1, printed_something || i>0);
if( i + 1 < space.spaces.size() )
{
PrintLevel(out, use_indents, level);
out << L",\n";
}
printed_something = true;
}
}
PrintLevel(out, use_indents, level);
else
out << L"}\n";
}
template<class Stream>
void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents)
{
bool treat_as_table = IsTable(space.name);
Serialize(space, out, use_indents, 0, false, treat_as_table, true);
}
} // namespace