diff --git a/space/Makefile.dep b/space/Makefile.dep index c43c31e..ad73066 100755 --- a/space/Makefile.dep +++ b/space/Makefile.dep @@ -1,4 +1,6 @@ # DO NOT DELETE +jsontospaceparser.o: jsontospaceparser.h space.h ../utf8/utf8.h space.o: space.h ../utf8/utf8.h spaceparser.o: spaceparser.h space.h ../utf8/utf8.h +spacetojson.o: spacetojson.h space.h diff --git a/space/Makefile.o.dep b/space/Makefile.o.dep index 2f519c5..12e1a4e 100755 --- a/space/Makefile.o.dep +++ b/space/Makefile.o.dep @@ -1 +1 @@ -o = space.o spaceparser.o +o = jsontospaceparser.o space.o spaceparser.o spacetojson.o diff --git a/space/spacetojson.cpp b/space/spacetojson.cpp index 657e4d9..70533ce 100644 --- a/space/spacetojson.cpp +++ b/space/spacetojson.cpp @@ -42,8 +42,21 @@ namespace PT { +void SpaceToJSON::ChangeSpacesToTable(const wchar_t * space_name) +{ + spaces_to_table.push_back(space_name); +} -bool SpaceToJSON::HasUnnamedSpace(Space & space) const + +void SpaceToJSON::ChangeSpacesToTable(const std::wstring & space_name) +{ + spaces_to_table.push_back(space_name); +} + + + + +bool SpaceToJSON::HasUnnamedSpace(Space & space) { for(size_t i=0 ; iname.empty() ) @@ -54,6 +67,56 @@ return false; +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); +} + + +void SpaceToJSON::TreatAsNumeric(const std::wstring & name) +{ + numeric.insert(name); + +} + + +void SpaceToJSON::TreatAsBool(const wchar_t * name) +{ + boolean.insert(name); +} + +void SpaceToJSON::TreatAsBool(const std::wstring & name) +{ + boolean.insert(name); +} + + + +bool SpaceToJSON::IsNumeric(const std::wstring & name) +{ + std::set::iterator i = numeric.find(name); + return i != numeric.end(); +} + + +bool SpaceToJSON::IsBool(const std::wstring & name) +{ + std::set::iterator i = boolean.find(name); + return i != boolean.end(); +} + + + diff --git a/space/spacetojson.h b/space/spacetojson.h index 9890dc5..a13cbb7 100644 --- a/space/spacetojson.h +++ b/space/spacetojson.h @@ -38,6 +38,9 @@ #ifndef headerfile_picotools_space_spacetojson #define headerfile_picotools_space_spacetojson +#include +#include +#include #include "space.h" @@ -49,26 +52,44 @@ class SpaceToJSON { public: + void ChangeSpacesToTable(const wchar_t * space_name); + void ChangeSpacesToTable(const std::wstring & space_name); + + void TreatAsNumeric(const wchar_t * name); + void TreatAsNumeric(const std::wstring & name); + + void TreatAsBool(const wchar_t * name); + void TreatAsBool(const std::wstring & name); + + template - void Serialize(Space & space, Stream & out, bool use_indents = false, int level = 0) const; + void Serialize(Space & space, Stream & out, bool use_indents = false, int level = 0, bool use_comma = false, bool skip_name = false); template - void SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level) const; + void SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level, bool use_comma); template - void SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level) const; + void SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level, bool use_comma); template - static void PrintToken(Stream & out, const StringType & str); + void PrintToken(Stream & out, const StringType & str, bool check_specials = false); template - static void PrintLevel(Stream & out, bool use_indents, int level); + void PrintLevel(Stream & out, bool use_indents, int level); + private: - bool HasUnnamedSpace(Space & space) const; + bool HasUnnamedSpace(Space & space); + bool HasSpace(Space & space, const std::wstring & name); + bool IsNumeric(const std::wstring & name); + bool IsBool(const std::wstring & name); + + std::vector spaces_to_table; + std::vector space_used; + std::set numeric, boolean; }; @@ -88,9 +109,10 @@ void SpaceToJSON::PrintLevel(Stream & out, bool use_indents, int level) template -void SpaceToJSON::PrintToken(Stream & out, const StringType & str) +void SpaceToJSON::PrintToken(Stream & out, const StringType & str, bool is_special) { - out << '\"'; + if( !is_special ) + out << '\"'; for(size_t i=0 ; i -void SpaceToJSON::SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level) const +void SpaceToJSON::SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level, bool use_comma) { Space::TableSingle::const_iterator i; size_t index = 0; +bool is_special; + + if( use_comma && !space.table_single.empty() ) + { + PrintLevel(out, use_indents, level); + out << L",\n"; + } for(i=space.table_single.begin() ; i != space.table_single.end() ; ++i, ++index) { + is_special = IsNumeric(i->first) || IsBool(i->first); + PrintLevel(out, use_indents, level); PrintToken(out, i->first); out << L": "; - PrintToken(out, i->second); + PrintToken(out, i->second, is_special); - if( index + 1 < space.table_single.size() || !space.table.empty() ) + if( index + 1 < space.table_single.size() ) out << ','; out << '\n'; @@ -137,14 +172,23 @@ size_t index = 0; template -void SpaceToJSON::SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level) const +void SpaceToJSON::SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level, bool use_comma) { Space::Table::const_iterator i2; size_t v; size_t index = 0; +bool is_special; + + if( use_comma && !space.table.empty() ) + { + PrintLevel(out, use_indents, level); + out << L",\n"; + } for(i2 = space.table.begin() ; i2 != space.table.end() ; ++i2, ++index) { + is_special = IsNumeric(i2->first) || IsBool(i2->first); + PrintLevel(out, use_indents, level); PrintToken(out, i2->first); out << L": "; @@ -157,7 +201,7 @@ size_t index = 0; if( v > 0 ) PrintLevel(out, use_indents, level + i2->first.size() + 3); - PrintToken(out, i2->second[v]); + PrintToken(out, i2->second[v], is_special); if( v + 1 < i2->second.size() ) out << L",\n"; @@ -166,7 +210,7 @@ size_t index = 0; if( i2->second.size() != 1 ) out << ']'; - if( index + 1 < space.table.size() || !space.spaces.empty() ) + if( index + 1 < space.table.size() ) out << ','; out << '\n'; @@ -177,49 +221,86 @@ size_t index = 0; template -void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int level) const +void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int level, bool use_comma, bool skip_name) { -// if( level > 0 ) -// out << '\n'; + if( use_comma ) + { + PrintLevel(out, use_indents, level); + out << L",\n"; + } PrintLevel(out, use_indents, level); - if( !space.name.empty() ) + if( !skip_name ) { - PrintToken(out, space.name); - out << L": "; + if( !space.name.empty() ) + { + PrintToken(out, space.name); + out << L": "; + } } - bool is_table = HasUnnamedSpace(space); + out << L"{\n"; - if( is_table ) - out << L"[\n"; - else - out << L"{\n"; + bool printed_something = false; - // !! what about if table or table.single has values - // and there is the table? (not supported yet) + 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"; + printed_something = true; + } + } - SerializeTableSingle(space, out, use_indents, level); - SerializeTableMulti(space, out, use_indents, level); for(size_t i=0 ; i0); + + if( i + 1 < space.spaces.size() ) + { + PrintLevel(out, use_indents, level); + out << L",\n"; + } + + printed_something = true; } } PrintLevel(out, use_indents, level); - - if( is_table ) - out << L"]\n"; - else - out << L"}\n"; + out << L"}\n"; }