项目作者: peychart

项目描述 :
A Javascript style object in C++ and serialization
高级语言: C++
项目地址: git://github.com/peychart/Untyped-cpp.git
创建时间: 2020-06-21T00:56:24Z
项目社区:https://github.com/peychart/Untyped-cpp

开源协议:

下载


Javascript data in c++

Software:

C++ source.

  • Object that manages the standard types of c ++ in the std::vector and std::map structures.

Examples of use: serialization, json objects management, backup of memory contexts, configuration files management, etc … or, MQTT communications, with a small memory footprint.

Here is a code that I wrote a few years ago, just for fun (and to learn c++) of an untyped object, standard C++ data container, now modified to handle serialization in format Json in my iot projects - this to replace the old libraries by manual reservation of memory spaces … ;-)

Allows:

  • use data similar to Javascript variables,
  • binary (de)serialization in/from files, streams or strings,
  • json (de)serialization in/from files, streams or strings,
  • and, of course, a self-managed memory space (nothing more than a c++ flavor).

Here, an intuitive example of HTML communication in an iot project : https://github.com/peychart/WiFiPowerStrip/blob/master/C%2B%2Bsource/webPage.cpp

Below, more examples of use:

  1. # define out /*
  2. g++ -std=c++0x -rdynamic -Wall -s -O2 -o test $0 untyped.cpp; exit $?
  3. # define out */
  4. # undef out
  5. #include "untyped.h"
  6. #include <fstream>
  7. int main() { // ****** Intuitive syntax ******
  8. untyped myJson('j');
  9. myJson.json(); // <-- set the JSON format management in (de)serialization methods.
  10. std::cout << myJson << std::endl;
  11. //-> 'j' // <-- type char
  12. std::cout << ( myJson + 'A' - 'a' ) << std::endl;
  13. //-> 'J'
  14. myJson = "aaaJaaa";
  15. std::cout << ( myJson + 's' + "on" - "aaa" - "aaa" ) << std::endl;
  16. //-> "Json" // <-- type string
  17. myJson.binary(); // <-- BINARY format management
  18. std::cout << ( myJson + 's' + "on" - "aaa" - "aaa" ) << std::endl;
  19. //-> Json // <-- std::string
  20. myJson.json(); // <-- Json format
  21. myJson(" \
  22. { \
  23. \"name\": \"Armstrong\", \
  24. \"firstname\": \"Neil\", \
  25. \"birthday\": { \"year\" : 1930, \"month\" : 8, \"day\": 5 }, \
  26. \"profession\":[ \"pilot\", \"engineer\", \"astronaut\" ], \
  27. \"born in\": \"Wapakoneta - Ohio\", \
  28. \"hobbies\": [ \
  29. \"astronomy\", \
  30. \"X-15\", \
  31. \"First Man to Walk on the Moon\" \
  32. ] \
  33. } \
  34. ");
  35. std::cout << myJson["birthday"]["year"] + 39 << std::endl;
  36. //-> 1969
  37. // ****** Implicit or explicit cast ******
  38. myJson = 31.4159e-1; // <-- implicite double
  39. auto myDouble = myJson;
  40. std::cout << "myDouble = " << myDouble << std::endl;
  41. //-> myDouble = 3.14159 // <-- implicite double
  42. std::cout << "cast<float>= " << myDouble.value<float>()<< std::endl;
  43. //-> cast<float>= 3.14159 // <-- explicite float
  44. std::cout << "cast<int> = " << myDouble.value<int>() << std::endl;
  45. //-> cast<int> = 3 // <-- explicite int
  46. std::cout << "cast<bool> = " << myDouble.value<bool>() << std::endl;
  47. //-> cast<bool> = 1 // <-- explicite bool
  48. // ****** Automatic memory allocation, as close as needed ******
  49. myJson.clear();
  50. myJson[0]=10;
  51. myJson[1]=11;
  52. myJson[myJson.vector().size()]=12;
  53. myJson[myJson.vector().size()]=13;
  54. myJson.json();
  55. std::cout << myJson << std::endl;
  56. //-> [10,11,12,13]
  57. untyped tmp;
  58. tmp["array1"] = myJson;
  59. tmp["array2"] = myJson; tmp["array2"][3]=23;
  60. tmp["array3"] = myJson; tmp["array3"][2]=32;
  61. tmp["array4"] = myJson; tmp["array4"][1]=41;
  62. myJson = tmp; tmp.clear();
  63. std::cout << myJson << std::endl;
  64. //-> {"array1":[10,11,12,13],"array2":[10,11,12,23],"array3":[10,11,32,13],"array4":[10,41,12,13]}
  65. // ****** Serialization ******
  66. myJson.clear().json();
  67. myJson["string"] = "abcdef";
  68. myJson["char"] = 'a';
  69. myJson["bool"] = true;
  70. myJson["int"] = 15;
  71. myJson["double"] = -3.14159;
  72. myJson["array"][3] = -13;
  73. myJson["array"][2] = -12;
  74. myJson["array"][1] = -11;
  75. myJson["array"][0] = -10;
  76. myJson["empty"] = untyped();
  77. std::cout << myJson << std::endl;
  78. //-> {"array":[-10,-11,-12,-13],"bool":true,"char":'a',"double":-3.14159,"empty":null,"int":15,"string":"abcdef"}
  79. // From a std::pair
  80. std::cout << untyped(std::pair<std::string,double>{"pi", 3.14159}).serializeJson() << std::endl;
  81. //-> {"pi":3.14159}
  82. // Deserialize from a std::stream:
  83. std::stringstream myStream( std::stringstream::in | std::stringstream::out );
  84. myStream << "{\"array\":[-10,-11,-12,-13], \"objectArray\" : [{\"o1\":false},{\"o2\":true} , {\"o3\":false}],\"bool\":true,\"char\":'a',\"double\":-3.14159,\"empty\":\"\",\"void\":null,\"int\":15,\"string\":\"abcdef\"}";
  85. std::cout << myJson.deserializeJson( myStream ) << std::endl;
  86. //-> {"array":[-10,-11,-12,-13],"bool":true,"char":'a',"double":-3.14159,"empty":"","int":15,"objectArray":[{"o1":false,"o2":true},{"o1":true,"o2":true},{"o1":true,"o2":false}],"string":"abcdef","void":null}
  87. std::cout << myJson["objectArray"][1]["o2"] << std::endl;
  88. //-> true
  89. std::cout << myJson["objectArray"][2]["o3"] << std::endl;
  90. //-> false
  91. //...
  92. myStream << "{\"array\":[-10,-11,-12,-13], \"objectArray\" : [{\"o1\":false},{\"o2\":true} , {\"o3\":false}],\"bool\":true,\"char\":'a',\"double\":-3.14159,\"empty\":\"\",\"void\":null,\"int\":15,\"string\":\"abcdef\"}";
  93. std::cout << untyped().json()( myStream )["objectArray"] << std::endl;
  94. //-> [{"o1":false},{"o2":true},{"o3":false}]
  95. // From a C string (or std::string)
  96. std::cout << myJson.deserializeJson("[0,1,2,3]") << std::endl;
  97. //-> [0,1,2,3]
  98. // <-- the same thing, with a short syntax...
  99. std::cout << myJson("[0,-1,-2,-4+1]") << std::endl;
  100. //-> [0,-1,-2,-3]
  101. // *** WARNING *** --> Different from: untyped myJson("[0,-1,-2,-3]"), a simple initialisation from a C string ; so:
  102. { untyped tmp( "[0,-1,-2,-3]" ); // <-- init with a C string
  103. tmp.json();
  104. std::cout << tmp << std::endl; // <-- std::string
  105. //-> "[0,-1,-2,-3]"
  106. std::cout << tmp.string().at(2) << std::endl; // <-- check
  107. //-> ,
  108. std::cout << "c_str()=" << tmp.c_str() << std::endl;
  109. //-> c_str()=[0,-1,-2,-3]
  110. std::cout << tmp.vector().size() << std::endl; // <-- check
  111. //-> 0
  112. tmp( tmp.c_str() ); // <-- deserialisation from a string...
  113. std::cout << tmp << std::endl; // <-- std::vector
  114. //-> [0,-1,-2,-3]
  115. std::cout << tmp.vector().at(2) << std::endl; // <-- check
  116. //-> -2
  117. std::cout << "Here: c_str()=" << tmp.c_str() << std::endl;
  118. //-> Here: c_str()=
  119. std::cout << "but: serializeJson().c_str()=" << tmp.serializeJson().c_str() is a string...<< std::endl;
  120. //-> but: serializeJson().c_str()=[0,-1,-2,-3] is a string...
  121. }
  122. // From std::string to std::stream
  123. myStream=std::stringstream();
  124. untyped().deserializeJson("[ 4 ,-5, 2 ]").serializeJson( myStream );
  125. std::cout << myStream.str() << std::endl;
  126. //-> [4,-5,2]
  127. // From std::string to std::string:
  128. std::cout << untyped().deserializeJson("[ 'a' ,'b', 'c' ]")[2].serializeJson() << std::endl;
  129. //-> 'c'
  130. // ****** Indented output ******
  131. myStream=std::stringstream();
  132. myJson.prettyJson(2); // <-- default tab size is 1 when no arg...
  133. myStream << "{\"array\":[-10,-11,-12,-13], \"objectArray\" : [{\"o1\":false},{\"o2\":true} , {\"o3\":false
  134. std::cout << myJson(myStream) << std::endl;
  135. //or, directly: std::cout << myJson.prettyJson(2)(myStream) << std::endl;
  136. //-> {
  137. //-> "array": [
  138. //-> -10,
  139. //-> -11,
  140. //-> -12,
  141. //-> -13
  142. //-> ],
  143. //-> "bool": true,
  144. //-> "char": 'a',
  145. //-> "double": -3.14159,
  146. //-> "empty": "",
  147. //-> "int": 15,
  148. //-> "objectArray": [
  149. //-> {
  150. //-> "o1": false
  151. //-> },
  152. //-> {
  153. //-> "o2": true
  154. //-> },
  155. //-> {
  156. //-> "o3": false
  157. //-> }
  158. //-> ],
  159. //-> "string": "abcdef",
  160. //-> "void": null
  161. //-> }
  162. // in/from FILE:
  163. std::ofstream outBinFile("/tmp/serialize.bin", std::ifstream::trunc|std::ifstream::binary);
  164. std::ifstream inBinFile ("/tmp/serialize.bin", std::ofstream::binary);
  165. std::ofstream outTxtFile("/tmp/serialize.json", std::ifstream::trunc);
  166. std::ifstream inTxtFile ("/tmp/serialize.json", std::ofstream::in);
  167. myJson.binary();
  168. myJson.serialize( outBinFile );
  169. myJson.serializeJson( outTxtFile );
  170. outBinFile.flush(); outTxtFile.flush();
  171. std::cout << untyped()( inBinFile )["string"] << std::endl;
  172. //-> abcdef // <-- Only what is needed...
  173. std::cout << inBinFile.tellg() << " octets deserialized from file." << std::endl;
  174. //-> 305 octets deserialized from file.
  175. std::cout << untyped().json()( inTxtFile )["double"] << std::endl << std::endl;
  176. //-> -3.14159 // <-- Extract only what is necessary...
  177. std::cout << inTxtFile.tellg() << " octets deserialized from file." << std::endl << std::endl;
  178. //-> 172 octets deserialized from file.
  179. // ****** Comments ******
  180. myStream=std::stringstream();
  181. myStream << "{/* This is a comment */ \"name\":\"Beno\u00EEt\" /* This is an other comment */ }";
  182. std::cout << untyped().json()( myStream ) << std::endl << std::endl;
  183. //-> {"name":"Benoît"}
  184. return(0);
  185. }