value.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <sstream>
  2. #include "value.h"
  3. namespace json {
  4. Value::Value() {
  5. _type = NullType;
  6. _data = "null";
  7. }
  8. Value::Value(bool_type const &value) {
  9. _type = BoolType;
  10. _data = value;
  11. }
  12. Value::Value(int const &value) {
  13. _type = IntType;
  14. _data = (int_type)value;
  15. }
  16. Value::Value(int_type const &value) {
  17. _type = IntType;
  18. _data = value;
  19. }
  20. Value::Value(decimal_type const &value) {
  21. _type = DecimalType;
  22. _data = value;
  23. }
  24. Value::Value(string_type const &value) {
  25. _type = StringType;
  26. _data = value;
  27. }
  28. Value::Value(const char *value) {
  29. _type = StringType;
  30. _data = std::string(value);
  31. }
  32. Value::Value(list_type const &value) {
  33. _type = ListType;
  34. _data = value;
  35. }
  36. Value::Value(dict_type const &value) {
  37. _type = DictType;
  38. _data = value;
  39. }
  40. void *Value::_value() {
  41. switch (_type) {
  42. case NullType:
  43. // null: value<string>, type_id<char>, variant has no <char> in it.
  44. return std::get_if<string_type>(&_data);
  45. case BoolType:
  46. return std::get_if<bool_type>(&_data);
  47. case IntType:
  48. return std::get_if<int_type>(&_data);
  49. case DecimalType:
  50. return std::get_if<decimal_type>(&_data);
  51. case StringType:
  52. return std::get_if<string_type>(&_data);
  53. case ListType:
  54. return std::get_if<list_type>(&_data);
  55. case DictType:
  56. return std::get_if<dict_type>(&_data);
  57. default:
  58. // will not be here
  59. throw std::logic_error("Got an unrecognized type_id");
  60. }
  61. }
  62. constexpr TYPE Value::type_id() const {
  63. return _type;
  64. }
  65. std::string Value::type_name() const {
  66. return _typeIdToName[_type];
  67. }
  68. constexpr bool Value::is_basic() const {
  69. return _type == NullType ||
  70. _type == BoolType ||
  71. _type == IntType ||
  72. _type == DecimalType ||
  73. _type == StringType;
  74. }
  75. std::string Value::str(bool const &format_need, int const &indent, int layer) {
  76. void *v = _value();
  77. std::ostringstream buff;
  78. switch (_type) {
  79. case NullType:
  80. buff << "null";
  81. break;
  82. case BoolType:
  83. if (GET_VALUE(bool_type, v)) buff << "true";
  84. else buff << "false";
  85. break;
  86. case IntType:
  87. buff << GET_VALUE(int_type, v);
  88. break;
  89. case DecimalType:
  90. buff << GET_VALUE(decimal_type, v);
  91. break;
  92. case StringType:
  93. buff << '"' << GET_VALUE(string_type, v) << '"';
  94. break;
  95. case ListType: {
  96. list_type &list = GET_VALUE(list_type, v);
  97. buff << '[';
  98. bool first = true;
  99. for (Value &item: list) {
  100. if (first) {
  101. first = false;
  102. if (format_need) buff << item.str(format_need, indent, layer + 1);
  103. else buff << item.str();
  104. } else {
  105. if (format_need) buff << ", " << item.str(format_need, indent, layer + 1);
  106. else buff << ',' << item.str();
  107. }
  108. }
  109. buff << ']';
  110. break;
  111. }
  112. case DictType: {
  113. dict_type &dict = GET_VALUE(dict_type, v);
  114. if (format_need) {
  115. buff << "{\n";
  116. for (auto it = dict.begin(); it != dict.end(); ++it) {
  117. if (it != dict.begin()) buff << ",\n";
  118. for (int i = 0; i < layer * indent; ++i) buff << ' ';
  119. buff << '"' << it->first << "\": " << it->second.str(format_need, indent, layer + 1);
  120. }
  121. buff << '\n';
  122. for (int i = 0; i < (layer - 1) * indent; ++i) buff << ' ';
  123. buff << '}';
  124. } else {
  125. buff << '{';
  126. for (auto it = dict.begin(); it != dict.end(); it++) {
  127. if (it != dict.begin()) buff << ',';
  128. buff << '"' << it->first << "\":" << it->second.str();
  129. }
  130. buff << '}';
  131. }
  132. break;
  133. }
  134. default:
  135. // will not be here
  136. throw std::logic_error("Got an unrecognized type_id");
  137. }
  138. return buff.str();
  139. }
  140. void Value::push_back(Value const &val) {
  141. auto &list = value<list_type>();
  142. list.push_back(val);
  143. }
  144. void Value::pop_back() {
  145. auto &list = value<list_type>();
  146. list.pop_back();
  147. }
  148. void Value::erase(int const &index) {
  149. auto &list = value<list_type>();
  150. list.erase(list.begin() + index);
  151. }
  152. void Value::erase(std::string const &key) {
  153. auto &dict = value<dict_type>();
  154. if (dict.find(key) != dict.end()) dict.erase(key);
  155. }
  156. Value &Value::operator[](int const &index) {
  157. auto &list = value<list_type>();
  158. return list.at(index);
  159. }
  160. Value &Value::operator[](std::string const &key) {
  161. auto &dict = value<dict_type>();
  162. return dict[key];
  163. }
  164. }