diff --git a/space/spacetojson.cpp b/space/spacetojson.cpp index 70533ce..b8b46ae 100644 --- a/space/spacetojson.cpp +++ b/space/spacetojson.cpp @@ -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 ; iname.empty() ) - return true; - -return false; + table.insert(space_name); } -bool SpaceToJSON::HasSpace(Space & space, const std::wstring & name) -{ - for(size_t i=0 ; iname == 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::iterator i = table.find(name); + return i != table.end(); +} diff --git a/space/spacetojson.h b/space/spacetojson.h index a13cbb7..7cf4f28 100644 --- a/space/spacetojson.h +++ b/space/spacetojson.h @@ -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 + void Serialize(Space & space, Stream & out, bool use_indents = false); + + +private: + + std::set numeric, boolean, table; template - 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 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 spaces_to_table; - std::vector space_used; - std::set numeric, boolean; }; @@ -221,7 +225,8 @@ bool is_special; template -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,74 +238,63 @@ 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": "; } } - out << L"{\n"; + if( treat_as_table ) + out << L"[\n"; + else + out << L"{\n"; bool printed_something = false; - 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 ; iname == spaces_to_table[z] ) - { - space_used[i] = true; - Serialize(*space.spaces[i], out, use_indents, level+1, i>0, true); - } - } - - out << L"]\n"; + if( !space.table_single.empty() || !space.table.empty() ) printed_something = true; - } } - + /* + * !! 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 ; i0); - - if( i + 1 < space.spaces.size() ) - { - PrintLevel(out, use_indents, level); - out << L",\n"; - } - - printed_something = 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); - out << L"}\n"; + + if( treat_as_table ) + out << L"]\n"; + else + out << L"}\n"; +} + + + +template +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); }