Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <map>
- #include <string>
- enum Type {
- object,
- array,
- string,
- number,
- boolean,
- null
- };
- class Value {
- protected:
- Type type;
- public:
- virtual ~Value() = 0;
- Type getType();
- virtual std::string toString() const = 0;
- virtual Value* parse(const std::string& jsonString) = 0;
- };
- Value::~Value() {
- //
- }
- Type Value::getType() {
- return this->type;
- }
- class JSONString : public Value {
- private:
- std::string value;
- public:
- JSONString(std::string value);
- ~JSONString();
- std::string getValue();
- std::string toString() const override;
- JSONString* parse(const std::string& json_string) override;
- };
- JSONString::JSONString(std::string value) {
- this->value = value;
- this->type = string;
- }
- JSONString::~JSONString() {
- //
- }
- std::string JSONString::getValue() {
- return this->value;
- }
- std::string JSONString::toString() const {
- return "\"" + value + "\"";
- }
- JSONString* JSONString::parse(const std::string& json_string) {
- if (json_string.length() >= 2 && json_string.front() == '"' && json_string.back() == '"') {
- value = json_string.substr(1, json_string.length() - 2);
- return this;
- }
- return nullptr;
- }
- class JSONNumber : public Value {
- private:
- double value;
- public:
- JSONNumber(double value);
- ~JSONNumber();
- double getValue();
- std::string toString() const override;
- JSONNumber* parse(const std::string& json_string) override;
- };
- JSONNumber::JSONNumber(double value) {
- this->value = value;
- this->type = number;
- }
- JSONNumber::~JSONNumber() {
- //
- }
- std::string JSONNumber::toString() const {
- return std::to_string(value);
- }
- double JSONNumber::getValue() {
- return this->value;
- }
- JSONNumber* JSONNumber::parse(const std::string& json_string) {
- try {
- value = std::stod(json_string);
- return this;
- } catch (...) {
- return nullptr;
- }
- }
- class JSONBoolean : public Value {
- private:
- bool value;
- public:
- JSONBoolean(bool value);
- ~JSONBoolean();
- bool getValue();
- std::string toString() const override;
- JSONBoolean* parse(const std::string& json_string) override;
- };
- JSONBoolean::JSONBoolean(bool value) {
- this->value = value;
- this->type = boolean;
- }
- JSONBoolean::~JSONBoolean() {
- //
- }
- std::string JSONBoolean::toString() const {
- return value ? "true" : "false";
- }
- bool JSONBoolean::getValue() {
- return this->value;
- }
- JSONBoolean* JSONBoolean::parse(const std::string& json_string) {
- if (json_string == "true") {
- value = true;
- return this;
- } else {
- value = false;
- return this;
- }
- return nullptr;
- }
- class JSONArray : public Value {
- private:
- std::vector<const Value*> values;
- public:
- JSONArray();
- ~JSONArray();
- std::vector<const Value*> getValues();
- void add(const Value* value);
- const Value* operator[](int index);
- std::string toString() const override;
- JSONArray* parse(const std::string& json_tring) override;
- };
- JSONArray::JSONArray() {
- this->type = array;
- }
- JSONArray::~JSONArray() {
- for(auto value : values) {
- delete value;
- }
- values.clear();
- }
- std::vector<const Value*> JSONArray::getValues() {
- return this->values;
- }
- void JSONArray::add(const Value* value) {
- values.push_back(value);
- }
- const Value* JSONArray::operator[](int index) {
- return values[index];
- }
- std::string JSONArray::toString() const {
- std::string result = "[";
- for (size_t i = 0; i < values.size(); ++i) {
- result += values[i]->toString();
- if (i < values.size() - 1) {
- result += ",";
- }
- }
- result += "]";
- return result;
- }
- class JSONObject : public Value {
- private:
- std::map<const std::string, const Value*> values;
- public:
- JSONObject();
- ~JSONObject();
- std::map<const std::string, const Value*> getValues();
- void add(const std::string& key, const Value* value);
- const Value* operator[](const std::string key);
- std::string toString() const override;
- JSONObject* parse(const std::string& json_tring) override;
- };
- JSONObject::JSONObject() {
- this->type = object;
- }
- JSONObject::~JSONObject() {
- for(auto it = values.begin(); it != values.end(); ++it) {
- delete it->second;
- }
- values.clear();
- }
- std::map<const std::string, const Value*> JSONObject::getValues() {
- return this->values;
- }
- void JSONObject::add(const std::string& key, const Value* value) {
- values[key] = value;
- }
- const Value* JSONObject::operator[](const std::string key) {
- return values[key];
- }
- std::string JSONObject::toString() const {
- std::string result = "{";
- for (auto it = values.begin(); it != values.end(); ++it) {
- result += "\"" + it->first + "\":" + it->second->toString();
- if (std::next(it) != values.end()) {
- result += ",";
- }
- }
- result += "}";
- return result;
- }
- JSONObject* JSONObject::parse(const std::string& json_string) {
- if (json_string.length() >= 2 && json_string.front() == '{' && json_string.back() == '}') {
- JSONObject* jsonObject = new JSONObject();
- std::string elementsString = json_string.substr(1, json_string.length() - 2);
- size_t startPos = 0;
- size_t endPos = 0;
- while (endPos != std::string::npos) {
- endPos = elementsString.find(',', startPos);
- std::string elementString = elementsString.substr(startPos, endPos - startPos);
- size_t colonPos = elementString.find(':');
- if (colonPos != std::string::npos) {
- std::string key = elementString.substr(0, colonPos);
- std::string valueString = elementString.substr(colonPos + 1);
- Value* elementValue;
- if (json_string.empty()) {
- elementValue = nullptr;
- }
- if (JSONBoolean* jsonBoolean = jsonBoolean->parse(json_string)) {
- elementValue = jsonBoolean;
- }
- if (JSONNumber* jsonNumber = jsonNumber->parse(json_string)) {
- elementValue = jsonNumber;
- }
- if (JSONString* jsonString = jsonString->parse(json_string)) {
- elementValue = jsonString;
- }
- JSONArray * jsonArray = new JSONArray();
- if (jsonArray->parse(json_string)) {
- elementValue = jsonArray;
- }
- if (JSONObject* jsonObject = jsonObject->parse(json_string)) {
- elementValue = jsonObject;
- }
- if (elementValue) {
- jsonObject->add(key, elementValue);
- }
- }
- }
- return jsonObject;
- }
- return nullptr;
- }
- JSONArray* JSONArray::parse(const std::string& json_string) {
- if (json_string.length() >= 2 && json_string.front() == '[' && json_string.back() == ']') {
- JSONArray* jsonArray = new JSONArray();
- std::string elementsString = json_string.substr(1, json_string.length() - 2);
- size_t startPos = 0;
- size_t endPos = 0;
- while (endPos != std::string::npos) {
- endPos = elementsString.find(',', startPos);
- std::string elementString = elementsString.substr(startPos, endPos - startPos);
- Value* elementValue;
- if (json_string.empty()) {
- elementValue = nullptr;
- }
- if (JSONBoolean* jsonBoolean = jsonBoolean->parse(json_string)) {
- elementValue = jsonBoolean;
- }
- if (JSONNumber* jsonNumber = jsonNumber->parse(json_string)) {
- elementValue = jsonNumber;
- }
- if (JSONString* jsonString = jsonString->parse(json_string)) {
- elementValue = jsonString;
- }
- if (JSONArray* jsonArray = jsonArray->parse(json_string)) {
- elementValue = jsonArray;
- }
- JSONObject* jsonObject = new JSONObject();
- if (jsonObject->parse(json_string)) {
- elementValue = jsonObject;
- }
- if (elementValue) {
- jsonArray->add(elementValue);
- }
- startPos = endPos + 1;
- }
- return this;
- }
- return nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement