Advertisement
klebermo

Untitled

Jun 2nd, 2023 (edited)
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <map>
  4.  
  5. enum Type {
  6.     object,
  7.     array,
  8.     string,
  9.     number,
  10.     boolean,
  11.     null
  12. };
  13.  
  14. class Value {
  15. public:
  16.     virtual ~Value() = 0;
  17.     Type getType();
  18.     virtual std::string toString() const = 0;
  19.     virtual Value* parse(const std::string& jsonString) = 0;
  20. };
  21.  
  22. class JSONString : public Value {
  23. public:
  24.     JSONString(std::string value);
  25.     ~JSONString();
  26.     std::string getValue();
  27.     std::string toString() const override;
  28.     JSONString* parse(const std::string& json_string) override;
  29. };
  30.  
  31. class JSONNumber : public Value {
  32. public:
  33.     JSONNumber(double value);
  34.     ~JSONNumber();
  35.     double getValue();
  36.     std::string toString() const override;
  37.     JSONNumber* parse(const std::string& json_string) override;
  38. };
  39.  
  40. class JSONBoolean : public Value {
  41. public:
  42.     JSONBoolean(bool value);
  43.     ~JSONBoolean();
  44.     bool getValue();
  45.     std::string toString() const override;
  46.     JSONBoolean* parse(const std::string& json_string) override;
  47. };
  48.  
  49. class JSONArray : public Value {
  50. private:
  51.     std::vector<const Value*> values;
  52. public:
  53.     JSONArray();
  54.     ~JSONArray();
  55.     std::vector<const Value*> getValues();
  56.     std::string toString() const override;
  57.     JSONArray* parse(const std::string& json_string) override;
  58.     void add(const Value* value);
  59.     const Value* operator[](int index);
  60. };
  61.  
  62. class JSONObject : public Value {
  63. private:
  64.     std::map<std::string, const Value*> values;
  65. public:
  66.     JSONObject();
  67.     ~JSONObject();
  68.     std::map<std::string, const Value*> getValues();
  69.     std::string toString() const override;
  70.     JSONObject* parse(const std::string& json_string) override;
  71.     void add(const std::string& key, const Value* value);
  72.     const Value* operator[](const std::string key);
  73. };
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement