diff --git a/HISTORY.md b/HISTORY.md index 9a4f549..82ce720 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,11 @@ +---------------------------- +Date: 23/10/2023 +Version: 2.4.1 + +- [x] Fix warning MapArrayStrings and put working example + ---------------------------- Date: 23/10/2023 Version: 2.4.0 diff --git a/README.md b/README.md index 9e5d5c9..9fea8e2 100644 --- a/README.md +++ b/README.md @@ -205,9 +205,9 @@ A json object it could be the following } ``` -**List 2.1** +**List 1.1 ** -Taking the example of list 2.1, in zetjsoncpp it defines json object using a structure in C as it shows below, +Taking the example of list 1.1, in zetjsoncpp it defines json object using a structure in C as it shows below, ```cpp typedef struct{ @@ -217,12 +217,12 @@ typedef struct{ }JsonSample; ``` -**List 2.2** +**List 1.2** Note: You may noticed that of ZJ_CONST_CHAR(s). This is a trick to pass literal string through variadic templates char by char, due that templates doesn't accept pass literal strings (i.e const char *) as a parameter. -An finally, to deserialize a json object, it is done through `ObjectJsonVar` passing the type of structure to deserialize it has seen in list 2.2, +An finally, to deserialize a json object, it is done through `ObjectJsonVar` passing the type of structure to deserialize it has seen in list 1.2, ```cpp auto json_object=zetjsoncpp::deserialize>( @@ -245,7 +245,8 @@ if(json_object->encoding.isDeserialized()){ } ``` -List 3.4 +**List 1.3** + By default, any no deserialized variable the strings are set empty, numbers and booleans will set as 0 and false respectively. ### Array of Objects @@ -263,7 +264,7 @@ A json array of objects it could be the following, }] ``` -To deserialize a array of objects, it is done through `ArrayObjectJsonVar` passing the type of structure to deserialize it has seen in list 2.2, +To deserialize a array of objects, it is done through `ArrayObjectJsonVar` passing the type of structure to deserialize it has seen in list 1.3, ```cpp zetjsoncpp::deserialize>( @@ -333,7 +334,7 @@ std::cout << zetjsoncpp::serialize(json_var); << std::enl; ## A big example -Let's suppose it has a file called sample.json with the following content, +Let's suppose it has a file called `sample.json` with the following content, ```javscript // Configuration options @@ -401,9 +402,9 @@ Let's suppose it has a file called sample.json with the following content, } ``` -**List 3.1** +**List 1.4** -To load the code saw on list 1.1 we have to declare the following C estructure, +To load the code saw on list 1.4 we have to declare the following C estructure, ```cpp #include "zetjsoncpp.h" @@ -467,50 +468,16 @@ typedef struct MapObjectJsonVar interpolations; + // Map of array of strings + zetjsoncpp::MapArrayStringJsonVar + map_array_strings; + }SampleJson; ``` -**List 3.2** - -And then we have to write the following code to load the data seen on list 1.1 into the estructure on list 1.2, - -```cpp -try{ - auto json_object=zetjsoncpp::deserialize_file>("sample.json"); - - // the values before modifications. - std::cout << "---------------------------------------------------" << std::endl; - std::cout << " Before modifications:"<< std::endl; - std::cout << zetjsoncpp::serialize(json_object); - - // From here we can operate with loaded data in our program using c++ operators - // put m_use_space to false... - json_object->indent.use_space = false; - - // iterate of all plugins and replace with random strings... - for(unsigned i = 0; i < json_object->plugins.size(); i++) { - json_object->plugins[i] = "my_randomstring"+zetjsoncpp::String::integerToString(i+1); - } - - // iterate of all interpolations and replace its data values... - for(auto it_map = json_object->interpolations.begin(); it_map != json_object->interpolations.end(); it_map++) { - for(auto it = it_map->second->data.begin(); it != it_map->second->data.end(); it++) { - *it = rand(); - } - } - - std::cout << "--------------------------------------------------" << std::endl; - std::cout << " After modifications:"<< std::endl; - std::cout << zetjsoncpp::serialize(json_object); - - // destroy json_object - delete json_object; - }catch(std::exception & ex){ - std::cerr << "Error:" << ex.what() << std::endl; - } -``` +**List 1.5** -As an example, we present a way to operate loaded json data into C++ code, +The following code it shows and example of loading and operating the data that has seen on list 1.4 into the structure of the list 1.5, ```cpp int main(int argc, char *argv[]){ @@ -538,6 +505,18 @@ int main(int argc, char *argv[]){ *it = rand(); } } + + // Modification of a map of array of strings... + int k=0; + for(auto it_map_array_strings : json_object->map_array_strings) { + int j=0; + + for(size_t i=0; i < json_object->map_array_strings[it_map_array_strings.first].size(); i++) { + json_object->map_array_strings[it_map_array_strings.first][i]="modified_string_"+std::to_string(k)+"_"+std::to_string(j); + j++; + } + k++; + } std::cout << "--------------------------------------------------" << std::endl; std::cout << " After modifications:"<< std::endl; @@ -557,121 +536,140 @@ After its execution the output shows the serialized json before and after the ch --------------------------------------------------- Before modifications: { - "encoding":"UTF-8", - "number":0.000033, - "plug-ins": - [ - "python","c++","ruby" - ], - "indent": - { - "length":3.000000, - "use_space":true - }, - "languages": - [{ - "code":"en", - "general_texts": - { - "general.hello_word":"Hello world!" - ,"general.no":"No" - ,"general.yes":"Yes" - } - },{ - "code":"es", - "general_texts": - { - "general.hello_word":"Hola mundo!" - ,"general.no":"No" - ,"general.yes":"Si" - } - },{ - "code":"zh-CN", - "general_texts": - { - "general.hello_word":"你好词" - ,"general.no":"没有" - ,"general.yes":"是" - } - }], - "interpolations": - { - "id_1":{ - "type":"material", - "channels":"rgb", - "data": - [ - 0.000000,1.000000,0.000000,1000.000000,0.000000,0.000000,0.000000,0.000000 - ] - } - ,"id_2":{ - "type":"transform", - "channels":"xyz", - "data": - [ - 0.000000,1.000000,0.000000,1000.000000,0.000000,0.000000,0.000000,0.000000 - ] - } - } -}-------------------------------------------------- + "encoding":"UTF-8" + ,"number":0.000033 + ,"plug-ins": + [ + "python","c++","ruby" + ] + ,"indent": + { + "length":3.000000 + ,"use_space":true + } + ,"languages": + [{ + "code":"en" + ,"general_texts": + { + "general.hello_word":"Hello world!" + ,"general.no":"No" + ,"general.yes":"Yes" + } + },{ + "code":"es" + ,"general_texts": + { + "general.hello_word":"Hola mundo!" + ,"general.no":"No" + ,"general.yes":"Si" + } + },{ + "code":"zh-CN" + ,"general_texts": + { + "general.hello_word":"你好?" + ,"general.no":"没有" + ,"general.yes":"是" + } + }] + ,"interpolations": + { + "id_1":{ + "type":"material" + ,"channels":"rgb" + ,"data": + [ + 0.000000,1.000000,0.000000,1000.000000,0.000000,0.000000,0.000000,0.000000 + ] + } + ,"id_2":{ + "type":"transform" + ,"channels":"xyz" + ,"data": + [ + 0.000000,1.000000,0.000000,1000.000000,0.000000,0.000000,0.000000,0.000000 + ] + } + } + ,"map_array_strings": + { + "map_array_strings_1":[ + "hello1","world1","!!" + ] + ,"map_array_strings_2":[ + "hello2","world2","!!" + ] + } +} +------------------------------------------------------------------------------ After modifications: { - "encoding":"UTF-8", - "number":0.000033, - "plug-ins": - [ - "my_randomstring1","my_randomstring2","my_randomstring3" - ], - "indent": - { - "length":3.000000, - "use_space":false - }, - "languages": - [{ - "code":"en", - "general_texts": - { - "general.hello_word":"Hello world!" - ,"general.no":"No" - ,"general.yes":"Yes" - } - },{ - "code":"es", - "general_texts": - { - "general.hello_word":"Hola mundo!" - ,"general.no":"No" - ,"general.yes":"Si" - } - },{ - "code":"zh-CN", - "general_texts": - { - "general.hello_word":"你好词" - ,"general.no":"没有" - ,"general.yes":"是" - } - }], - "interpolations": - { - "id_1":{ - "type":"material", - "channels":"rgb", - "data": - [ - 41.000000,18467.000000,6334.000000,26500.000000,19169.000000,15724.000000,11478.000000,29358.000000 - ] - } - ,"id_2":{ - "type":"transform", - "channels":"xyz", - "data": - [ - 26962.000000,24464.000000,5705.000000,28145.000000,23281.000000,16827.000000,9961.000000,491.000000 - ] - } - } + "encoding":"UTF-8" + ,"number":0.000033 + ,"plug-ins": + [ + "my_randomstring1","my_randomstring2","my_randomstring3" + ] + ,"indent": + { + "length":3.000000 + ,"use_space":false + } + ,"languages": + [{ + "code":"en" + ,"general_texts": + { + "general.hello_word":"Hello world!" + ,"general.no":"No" + ,"general.yes":"Yes" + } + },{ + "code":"es" + ,"general_texts": + { + "general.hello_word":"Hola mundo!" + ,"general.no":"No" + ,"general.yes":"Si" + } + },{ + "code":"zh-CN" + ,"general_texts": + { + "general.hello_word":"你好?" + ,"general.no":"没有" + ,"general.yes":"是" + } + }] + ,"interpolations": + { + "id_1":{ + "type":"material" + ,"channels":"rgb" + ,"data": + [ + 18538.000000,12292.000000,6038.000000,24179.000000,18190.000000,29657.000000,7958.000000,6191.000000 + ] + } + ,"id_2":{ + "type":"transform" + ,"channels":"xyz" + ,"data": + [ + 19815.000000,22888.000000,19156.000000,11511.000000,16202.000000,2634.000000,24272.000000,20055.000000 + ] + } + } + ,"map_array_strings": + { + "map_array_strings_1":[ + "modified_string_0_0","modified_string_0_1","modified_string_0_2" + ] + ,"map_array_strings_2":[ + "modified_string_1_0","modified_string_1_1","modified_string_1_2" + ] + } } ``` diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0948816..0a38cc4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -96,7 +96,7 @@ if(MSVC) else() - SET( CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -D__STDC_CONSTANT_MACROS -std=gnu++0x -I -Wall -Wextra -pedantic ") + SET( CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -D__STDC_CONSTANT_MACROS -std=gnu++0x -I -Wall -Wextra -pedantic -static-libstdc++ ") if (MINGW) MESSAGE("-- Plataform: MINGW" ) diff --git a/src/jsonvar/ArrayJsonVar.h b/src/jsonvar/ArrayJsonVar.h index aace0bc..22657f2 100644 --- a/src/jsonvar/ArrayJsonVar.h +++ b/src/jsonvar/ArrayJsonVar.h @@ -18,15 +18,15 @@ namespace zetjsoncpp{ std::vector<_T_DATA> __zj_vector_data__; public: - typedef typename std::vector<_T_DATA>::iterator JsonVarIteratorVector; + typedef typename std::vector<_T_DATA>::iterator ArrayIteratorJsonVar; ArrayJsonVar() {} - JsonVarIteratorVector begin(){ + ArrayIteratorJsonVar begin(){ return __zj_vector_data__.begin(); } - JsonVarIteratorVector end(){ + ArrayIteratorJsonVar end(){ return __zj_vector_data__.end(); } @@ -67,6 +67,10 @@ namespace zetjsoncpp{ return __zj_vector_data__; } + const std::vector<_T_DATA> * getStdVectorPtr() { + return &__zj_vector_data__; + } + virtual JsonVar * getJsonVarPtr(int index) { return (JsonVar *)&__zj_vector_data__.at(index); } diff --git a/src/jsonvar/ArrayStringJsonVar.h b/src/jsonvar/ArrayStringJsonVar.h index 9978f21..6aa4a3b 100644 --- a/src/jsonvar/ArrayStringJsonVar.h +++ b/src/jsonvar/ArrayStringJsonVar.h @@ -15,7 +15,7 @@ namespace zetjsoncpp{ } - ArrayStringJsonVar<> & operator=(const std::vector & _vec_string){ + ArrayStringJsonVar & operator=(const std::vector & _vec_string){ copy(_vec_string); return *this; } diff --git a/src/jsonvar/JsonVar.h b/src/jsonvar/JsonVar.h index 2e23aad..8e9e0ed 100644 --- a/src/jsonvar/JsonVar.h +++ b/src/jsonvar/JsonVar.h @@ -71,7 +71,7 @@ #define ZJ_CAST_JSON_VAR_BOOLEAN (zetjsoncpp::BooleanJsonVar<> *) #define ZJ_CAST_JSON_VAR_NUMBER (zetjsoncpp::NumberJsonVar<> *) #define ZJ_CAST_JSON_VAR_OBJECT (zetjsoncpp::ObjectJsonVar<> *) -#define ZJ_CAST_JSON_VAR_ARRAY_OF_BOOLEANS (zetjsoncpp::ArrayBooleanJsonVar<> *) +#define ZJ_CAST_JSON_VAR_ARRAY_OF_BOOLEANS (zetjsoncpp::ArrayBooleanJsonVar<> *) #define ZJ_CAST_JSON_VAR_ARRAY_OF_NUMBERS (zetjsoncpp::ArrayNumberJsonVar<> *) #define ZJ_CAST_JSON_VAR_ARRAY_OF_STRINGS (zetjsoncpp::ArrayStringJsonVar<> *) #define ZJ_CAST_JSON_VAR_ARRAY_OF_OBJECTS (zetjsoncpp::ArrayObjectJsonVar<> *) @@ -79,21 +79,23 @@ #define ZJ_CAST_JSON_VAR_MAP_OF_NUMBERS (zetjsoncpp::MapNumberJsonVar<> *) #define ZJ_CAST_JSON_VAR_MAP_OF_STRINGS (zetjsoncpp::MapStringJsonVar<> *) #define ZJ_CAST_JSON_VAR_MAP_OF_OBJECTS (zetjsoncpp::MapObjectJsonVar<> *) - -#define ZJ_VAR_BOOLEAN(name) zetjsoncpp::BooleanJsonVar name -#define ZJ_VAR_NUMBER(name) zetjsoncpp::NumberJsonVar name -#define ZJ_VAR_STRING(name) zetjsoncpp::StringJsonVar name -#define ZJ_VAR_OBJECT(type,name) zetjsoncpp::ObjectJsonVar name - -#define ZJ_VAR_ARRAY_BOOLEAN(name) zetjsoncpp::ArrayBooleanJsonVar name -#define ZJ_VAR_ARRAY_NUMBER(name) zetjsoncpp::ArrayNumberJsonVar name -#define ZJ_VAR_ARRAY_STRING(name) zetjsoncpp::ArrayStringJsonVar name -#define ZJ_VAR_ARRAY_OBJECT(type,name) zetjsoncpp::ArrayObjectJsonVar name - -#define ZJ_VAR_MAP_BOOLEAN(name) zetjsoncpp::MapBooleanJsonVar name -#define ZJ_VAR_MAP_NUMBER(name) zetjsoncpp::MapNumberJsonVar name -#define ZJ_VAR_MAP_STRING(name) zetjsoncpp::MapStringJsonVar name -#define ZJ_VAR_MAP_OBJECT(type,name) zetjsoncpp::MapObjectJsonVar name +#define ZJ_CAST_JSON_VAR_MAP_ARRAY_OF_STRING (zetjsoncpp::MapArrayStringJsonVar<> *) + +#define ZJ_VAR_BOOLEAN(name) zetjsoncpp::BooleanJsonVar name +#define ZJ_VAR_NUMBER(name) zetjsoncpp::NumberJsonVar name +#define ZJ_VAR_STRING(name) zetjsoncpp::StringJsonVar name +#define ZJ_VAR_OBJECT(type,name) zetjsoncpp::ObjectJsonVar name + +#define ZJ_VAR_ARRAY_BOOLEAN(name) zetjsoncpp::ArrayBooleanJsonVar name +#define ZJ_VAR_ARRAY_NUMBER(name) zetjsoncpp::ArrayNumberJsonVar name +#define ZJ_VAR_ARRAY_STRING(name) zetjsoncpp::ArrayStringJsonVar name +#define ZJ_VAR_ARRAY_OBJECT(type,name) zetjsoncpp::ArrayObjectJsonVar name + +#define ZJ_VAR_MAP_BOOLEAN(name) zetjsoncpp::MapBooleanJsonVar name +#define ZJ_VAR_MAP_NUMBER(name) zetjsoncpp::MapNumberJsonVar name +#define ZJ_VAR_MAP_STRING(name) zetjsoncpp::MapStringJsonVar name +#define ZJ_VAR_MAP_OBJECT(type,name) zetjsoncpp::MapObjectJsonVar name +#define ZJ_VAR_MAP_ARRAY_STRING(type,name) zetjsoncpp::MapArrayStringJsonVar name namespace zetjsoncpp { diff --git a/src/jsonvar/StringJsonVar.h b/src/jsonvar/StringJsonVar.h index 9fa346f..acbace9 100644 --- a/src/jsonvar/StringJsonVar.h +++ b/src/jsonvar/StringJsonVar.h @@ -13,6 +13,11 @@ namespace zetjsoncpp{ __zj_value__ = s; } + StringJsonVar(const StringJsonVar & _value) { + init(); + copy(_value); + } + virtual void * getPtrValue(){ return &__zj_value__;} operator std::string(){return __zj_value__;} @@ -26,7 +31,7 @@ namespace zetjsoncpp{ return (*this); } - StringJsonVar & operator =(const StringJsonVar<> & _value){ + StringJsonVar & operator =(const StringJsonVar & _value){ this->__zj_value__ = _value.__zj_value__; return (*this); } @@ -55,7 +60,6 @@ namespace zetjsoncpp{ return s1.__zj_value__!=s2.__zj_value__; } - friend StringJsonVar operator +(const std::string & s1,const StringJsonVar & s2) { return StringJsonVar(s1+s2.__zj_value__); } @@ -76,6 +80,11 @@ namespace zetjsoncpp{ this->__zj_size_data__ = sizeof(StringJsonVar<_T_NAME...>); } + void copy(const StringJsonVar & _value){ + this->__zj_value__ = _value.__zj_value__; + this->__zj_is_parsed__ = _value.__zj_is_parsed__; + } + private: std::string __zj_value__; diff --git a/src/zetjsoncpp.h b/src/zetjsoncpp.h index 08bb1d9..ca0b0a3 100644 --- a/src/zetjsoncpp.h +++ b/src/zetjsoncpp.h @@ -27,7 +27,7 @@ #define ZETJSONCPP_VERSION_MAJOR 2 #define ZETJSONCPP_VERSION_MINOR 4 -#define ZETJSONCPP_VERSION_PATCH 0 +#define ZETJSONCPP_VERSION_PATCH 1 #ifdef __MEMMANAGER__ #include "memmgr.h" diff --git a/src/zetjsoncpp_serializer.cpp b/src/zetjsoncpp_serializer.cpp index 3e04009..e329f09 100644 --- a/src/zetjsoncpp_serializer.cpp +++ b/src/zetjsoncpp_serializer.cpp @@ -193,6 +193,7 @@ namespace zetjsoncpp{ case JsonVarType::JSON_VAR_TYPE_MAP_OF_STRINGS: case JsonVarType::JSON_VAR_TYPE_MAP_OF_NUMBERS: case JsonVarType::JSON_VAR_TYPE_MAP_OF_OBJECTS: + case JsonVarType::JSON_VAR_TYPE_MAP_OF_ARRAY_OF_STRINGS: if (_minimized==false){ _str_result += "\n"; for (int i = 0; i <= _ident; i++){ @@ -328,7 +329,16 @@ namespace zetjsoncpp{ ,_discard_non_serialized ); break; + case JSON_VAR_TYPE_MAP_OF_ARRAY_OF_STRINGS: + serialize_json_var_map>( + _str_result + , (MapArrayStringJsonVar<> *)_json_var + ,_ident,_minimized + ,_discard_non_serialized + ); + break; } + } std::string serialize( diff --git a/test/test_file.cpp b/test/test_file.cpp index 39c61b1..b41ed67 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -30,7 +30,7 @@ typedef struct{ zetjsoncpp::StringJsonVar channels; - // Vector of numbers data + // Array of numbers data zetjsoncpp::ArrayNumberJsonVar data; }Interpolation; @@ -45,7 +45,7 @@ typedef struct zetjsoncpp::NumberJsonVar number; - // Vector of strings plug-ins + // Array of strings plug-ins zetjsoncpp::ArrayStringJsonVar plugins; @@ -100,24 +100,19 @@ int main(int argc, char *argv[]){ } } - // iterate of all map array string... - std::cout << std::endl; + // Modification of a map of array of strings... + int k=0; for(auto it_map_array_strings : json_object->map_array_strings) { int j=0; - std::cout << "\"" << it_map_array_strings.first << "\": [ "; - for(auto it_array_strings : it_map_array_strings.second) { - if(j > 0){ - std::cout << ","; - } - std::cout << (std::string)it_array_strings; + for(size_t i=0; i < json_object->map_array_strings[it_map_array_strings.first].size(); i++) { + json_object->map_array_strings[it_map_array_strings.first][i]="modified_string_"+std::to_string(k)+"_"+std::to_string(j); j++; } - - std::cout << "]" << std::endl; + k++; } - std::cout << std::endl; + std::cout << std::endl; std::cout << "------------------------------------------------------------------------------" << std::endl; std::cout << " After modifications:"<< std::endl; std::cout << zetjsoncpp::serialize(json_object);