Guest User

Untitled

a guest
Nov 19th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. void Json::print(any _data) {
  2. string type = _data.type().name();
  3.  
  4. try {
  5. if (type == "i") {
  6. cout << any_cast<int> (_data);
  7. }
  8. else if (type == "d") {
  9. cout << any_cast<double> (_data);
  10. }
  11. else if (type == "b") {
  12. if (any_cast<bool> (_data)) std::cout << "true";
  13. else cout << "false";
  14. }
  15. else if (type == "Ss" || type == "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
  16. cout << any_cast<string> (_data);
  17. }
  18. else if (type.find("St6vector") < type.length()) {
  19. std::vector <std::any> vec;
  20. vec = any_cast<vector<any>>(_data);
  21. unsigned int count = 0;
  22. cout << "[ ";
  23. for (const auto& c : vec) {
  24. count++;
  25. if (count > 1) std::cout << " , ";
  26. print(c);
  27. }
  28. cout << " ]";
  29. }
  30. else if (type.find("St3map") < type.length()) {
  31. map <string, any> _map;
  32. _map = std::any_cast<map <string, any>>(_data);
  33. cout << "{\n" ;
  34. unsigned int count = 0;
  35. for (const auto& c: _map) {
  36. count++;
  37. if (count > 1) cout << " ,\n";
  38. cout << "\t" << c.first << " : ";
  39. print(c.second);
  40. }
  41. cout << "\n\t}";
  42. }
  43. }
  44. catch(const std::bad_any_cast& e) {
  45. cout << e.what() << '\n';
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment