Advertisement
klebermo

Untitled

Jun 2nd, 2023
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.53 KB | None | 0 0
  1. #include <vector>
  2. #include <map>
  3. #include <string>
  4.  
  5. enum Type {
  6.     object,
  7.     array,
  8.     string,
  9.     number,
  10.     boolean,
  11.     null
  12. };
  13.  
  14. class Value {
  15. protected:
  16.     Type type;
  17. public:
  18.     virtual ~Value() = 0;
  19.     Type getType();
  20.     virtual std::string toString() const = 0;
  21.     virtual Value* parse(const std::string& jsonString) = 0;
  22. };
  23.  
  24. Value::~Value() {
  25.     //
  26. }
  27.  
  28. Type Value::getType()  {
  29.         return this->type;
  30. }
  31.  
  32. class JSONString : public Value {
  33. private:
  34.     std::string value;
  35. public:
  36.     JSONString(std::string value);
  37.     ~JSONString();
  38.     std::string getValue();
  39.     std::string toString() const override;
  40.     JSONString* parse(const std::string& json_string) override;
  41. };
  42.  
  43. JSONString::JSONString(std::string value) {
  44.     this->value = value;
  45.     this->type = string;
  46. }
  47.  
  48. JSONString::~JSONString() {
  49.     //
  50. }
  51.  
  52. std::string JSONString::getValue() {
  53.     return this->value;
  54. }
  55.  
  56. std::string JSONString::toString() const {
  57.     return "\"" + value + "\"";
  58. }
  59.  
  60. JSONString* JSONString::parse(const std::string& json_string) {
  61.     if (json_string.length() >= 2 && json_string.front() == '"' && json_string.back() == '"') {
  62.         value = json_string.substr(1, json_string.length() - 2);
  63.         return this;
  64.     }
  65.     return nullptr;
  66. }
  67.  
  68. class JSONNumber : public Value {
  69. private:
  70.     double value;
  71. public:
  72.     JSONNumber(double value);
  73.     ~JSONNumber();
  74.     double getValue();
  75.     std::string toString() const override;
  76.     JSONNumber* parse(const std::string& json_string) override;
  77. };
  78.  
  79. JSONNumber::JSONNumber(double value) {
  80.     this->value = value;
  81.     this->type = number;
  82. }
  83.  
  84. JSONNumber::~JSONNumber() {
  85.     //
  86. }
  87.  
  88. std::string JSONNumber::toString() const {
  89.     return std::to_string(value);
  90. }
  91.  
  92. double JSONNumber::getValue() {
  93.     return this->value;
  94. }    
  95.  
  96. JSONNumber* JSONNumber::parse(const std::string& json_string) {
  97.     try {
  98.         value = std::stod(json_string);
  99.         return this;
  100.     } catch (...) {
  101.         return nullptr;
  102.     }
  103. }
  104.  
  105. class JSONBoolean : public Value {
  106. private:
  107.     bool value;
  108. public:
  109.     JSONBoolean(bool value);
  110.     ~JSONBoolean();
  111.     bool getValue();
  112.     std::string toString() const override;
  113.     JSONBoolean* parse(const std::string& json_string) override;
  114. };
  115.  
  116. JSONBoolean::JSONBoolean(bool value) {
  117.     this->value = value;
  118.     this->type = boolean;
  119. }
  120.  
  121. JSONBoolean::~JSONBoolean() {
  122.     //
  123. }
  124.  
  125. std::string JSONBoolean::toString() const {
  126.     return value ? "true" : "false";
  127. }
  128.  
  129. bool JSONBoolean::getValue() {
  130.     return this->value;
  131. }    
  132.  
  133. JSONBoolean* JSONBoolean::parse(const std::string& json_string) {
  134.     if (json_string == "true") {
  135.         value = true;
  136.         return this;
  137.     } else {
  138.         value = false;
  139.         return this;
  140.     }
  141.     return nullptr;
  142. }
  143.  
  144. class JSONArray : public Value {
  145. private:
  146.     std::vector<const Value*> values;
  147. public:
  148.     JSONArray();
  149.     ~JSONArray();
  150.     std::vector<const Value*> getValues();
  151.  
  152.     void add(const Value* value);
  153.     const Value* operator[](int index);
  154.  
  155.     std::string toString() const override;
  156.     JSONArray* parse(const std::string& json_tring) override;
  157. };
  158.  
  159. JSONArray::JSONArray() {
  160.     this->type = array;
  161. }
  162.  
  163. JSONArray::~JSONArray() {
  164.     for(auto value : values) {
  165.         delete value;
  166.     }
  167.     values.clear();
  168. }
  169.  
  170. std::vector<const Value*> JSONArray::getValues() {
  171.     return this->values;
  172. }
  173.  
  174. void JSONArray::add(const Value* value) {
  175.     values.push_back(value);
  176. }
  177.  
  178. const Value* JSONArray::operator[](int index) {
  179.     return values[index];
  180. }
  181.  
  182. std::string JSONArray::toString() const {
  183.     std::string result = "[";
  184.     for (size_t i = 0; i < values.size(); ++i) {
  185.         result += values[i]->toString();
  186.         if (i < values.size() - 1) {
  187.             result += ",";
  188.         }
  189.     }
  190.     result += "]";
  191.     return result;
  192. }
  193.  
  194. class JSONObject : public Value {
  195. private:
  196.     std::map<const std::string, const Value*> values;
  197. public:
  198.     JSONObject();
  199.     ~JSONObject();
  200.     std::map<const std::string, const Value*> getValues();
  201.  
  202.     void add(const std::string& key, const Value* value);
  203.     const Value* operator[](const std::string key);
  204.  
  205.     std::string toString() const override;
  206.     JSONObject* parse(const std::string& json_tring) override;
  207. };
  208.  
  209. JSONObject::JSONObject() {
  210.     this->type = object;
  211. }
  212.  
  213. JSONObject::~JSONObject() {
  214.     for(auto it = values.begin(); it != values.end(); ++it) {
  215.         delete it->second;
  216.     }
  217.     values.clear();
  218. }
  219.  
  220. std::map<const std::string, const Value*> JSONObject::getValues() {
  221.     return this->values;
  222. }
  223.  
  224. void JSONObject::add(const std::string& key, const Value* value) {
  225.     values[key] = value;
  226. }
  227.  
  228. const Value* JSONObject::operator[](const std::string key) {
  229.     return values[key];
  230. }
  231.  
  232. std::string JSONObject::toString() const {
  233.     std::string result = "{";
  234.     for (auto it = values.begin(); it != values.end(); ++it) {
  235.         result += "\"" + it->first + "\":" + it->second->toString();
  236.         if (std::next(it) != values.end()) {
  237.             result += ",";
  238.         }
  239.     }
  240.     result += "}";
  241.     return result;
  242. }
  243.  
  244. JSONObject* JSONObject::parse(const std::string& json_string) {
  245.     if (json_string.length() >= 2 && json_string.front() == '{' && json_string.back() == '}') {
  246.         JSONObject* jsonObject = new JSONObject();
  247.         std::string elementsString = json_string.substr(1, json_string.length() - 2);
  248.         size_t startPos = 0;
  249.         size_t endPos = 0;
  250.         while (endPos != std::string::npos) {
  251.             endPos = elementsString.find(',', startPos);
  252.             std::string elementString = elementsString.substr(startPos, endPos - startPos);
  253.             size_t colonPos = elementString.find(':');
  254.             if (colonPos != std::string::npos) {
  255.                 std::string key = elementString.substr(0, colonPos);
  256.                 std::string valueString = elementString.substr(colonPos + 1);
  257.  
  258.                 Value* elementValue;
  259.                 if (json_string.empty()) {
  260.                     elementValue = nullptr;
  261.                 }
  262.                 if (JSONBoolean* jsonBoolean = jsonBoolean->parse(json_string)) {
  263.                     elementValue = jsonBoolean;
  264.                 }
  265.                 if (JSONNumber* jsonNumber = jsonNumber->parse(json_string)) {
  266.                     elementValue = jsonNumber;
  267.                 }
  268.                 if (JSONString* jsonString = jsonString->parse(json_string)) {
  269.                     elementValue = jsonString;
  270.                 }
  271.                 JSONArray * jsonArray = new JSONArray();
  272.                 if (jsonArray->parse(json_string)) {
  273.                     elementValue = jsonArray;
  274.                 }
  275.                 if (JSONObject* jsonObject = jsonObject->parse(json_string)) {
  276.                     elementValue = jsonObject;
  277.                 }
  278.  
  279.                 if (elementValue) {
  280.                     jsonObject->add(key, elementValue);
  281.                 }
  282.  
  283.             }
  284.         }
  285.         return jsonObject;
  286.     }
  287.     return nullptr;
  288. }
  289.  
  290. JSONArray* JSONArray::parse(const std::string& json_string) {
  291.     if (json_string.length() >= 2 && json_string.front() == '[' && json_string.back() == ']') {
  292.         JSONArray* jsonArray = new JSONArray();
  293.         std::string elementsString = json_string.substr(1, json_string.length() - 2);
  294.         size_t startPos = 0;
  295.         size_t endPos = 0;
  296.         while (endPos != std::string::npos) {
  297.             endPos = elementsString.find(',', startPos);
  298.             std::string elementString = elementsString.substr(startPos, endPos - startPos);
  299.  
  300.             Value* elementValue;
  301.             if (json_string.empty()) {
  302.                 elementValue = nullptr;
  303.             }
  304.             if (JSONBoolean* jsonBoolean = jsonBoolean->parse(json_string)) {
  305.                 elementValue = jsonBoolean;
  306.             }
  307.             if (JSONNumber* jsonNumber = jsonNumber->parse(json_string)) {
  308.                 elementValue = jsonNumber;
  309.             }
  310.             if (JSONString* jsonString = jsonString->parse(json_string)) {
  311.                 elementValue = jsonString;
  312.             }
  313.             if (JSONArray* jsonArray = jsonArray->parse(json_string)) {
  314.                 elementValue = jsonArray;
  315.             }
  316.             JSONObject* jsonObject = new JSONObject();
  317.             if (jsonObject->parse(json_string)) {
  318.                 elementValue = jsonObject;
  319.             }
  320.  
  321.             if (elementValue) {
  322.                 jsonArray->add(elementValue);
  323.             }
  324.             startPos = endPos + 1;
  325.         }
  326.         return this;
  327.     }
  328.     return nullptr;
  329. }
  330.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement