Update json parser with new get/set methods

This commit is contained in:
MihailRis 2023-11-08 15:54:28 +03:00
parent 6b5b0a9347
commit 533be770d8
2 changed files with 56 additions and 12 deletions

View File

@ -232,6 +232,14 @@ JArray& JArray::put(string value) {
return *this; return *this;
} }
JArray& JArray::put(uint value) {
return put((number_t)value);
}
JArray& JArray::put(int value) {
return put((number_t)value);
}
JArray& JArray::put(number_t value) { JArray& JArray::put(number_t value) {
valvalue val; valvalue val;
val.num = value; val.num = value;
@ -266,18 +274,28 @@ JObject::~JObject() {
} }
} }
std::string JObject::str(std::string key, std::string def) const { void JObject::str(std::string key, std::string& dst) const {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) if (found != map.end())
return *found->second->value.str; dst = *found->second->value.str;
return def;
} }
number_t JObject::num(std::string key, number_t def) const { void JObject::num(std::string key, number_t& dst) const {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) if (found != map.end())
return found->second->value.num; dst = found->second->value.num;
return def; }
void JObject::num(std::string key, int& dst) const {
auto found = map.find(key);
if (found != map.end())
dst = found->second->value.num;
}
void JObject::num(std::string key, uint& dst) const {
auto found = map.find(key);
if (found != map.end())
dst = found->second->value.num;
} }
JObject* JObject::obj(std::string key) const { JObject* JObject::obj(std::string key) const {
@ -294,13 +312,21 @@ JArray* JObject::arr(std::string key) const {
return nullptr; return nullptr;
} }
bool JObject::flag(std::string key, bool def) const { void JObject::flag(std::string key, bool& dst) const {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) if (found != map.end())
return found->second->value.boolean; dst = found->second->value.boolean;
return def;
} }
JObject& JObject::put(string key, uint value) {
return put(key, (number_t)value);
}
JObject& JObject::put(string key, int value) {
return put(key, (number_t)value);
}
JObject& JObject::put(string key, number_t value) { JObject& JObject::put(string key, number_t value) {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) delete found->second; if (found != map.end()) delete found->second;
@ -637,3 +663,12 @@ Value* Parser::parseValue() {
} }
throw error("unexpected character '"+string({next})+"'"); throw error("unexpected character '"+string({next})+"'");
} }
JObject* json::parse(std::string filename, std::string source) {
Parser parser(filename, source);
return parser.parse();
}
JObject* json::parse(std::string source) {
return parse("<string>", source);
}

View File

@ -70,6 +70,8 @@ namespace json {
return values.size(); return values.size();
} }
JArray& put(uint value);
JArray& put(int value);
JArray& put(number_t value); JArray& put(number_t value);
JArray& put(std::string value); JArray& put(std::string value);
JArray& put(JObject* value); JArray& put(JObject* value);
@ -82,12 +84,16 @@ namespace json {
std::unordered_map<std::string, Value*> map; std::unordered_map<std::string, Value*> map;
~JObject(); ~JObject();
std::string str(std::string key, std::string def) const; void str(std::string key, std::string& dst) const;
number_t num(std::string key, number_t def) const; void num(std::string key, int& dst) const;
void num(std::string key, uint& dst) const;
void num(std::string key, number_t& dst) const;
JObject* obj(std::string key) const; JObject* obj(std::string key) const;
JArray* arr(std::string key) const; JArray* arr(std::string key) const;
bool flag(std::string key, bool def) const; void flag(std::string key, bool& dst) const;
JObject& put(std::string key, uint value);
JObject& put(std::string key, int value);
JObject& put(std::string key, number_t value); JObject& put(std::string key, number_t value);
JObject& put(std::string key, std::string value); JObject& put(std::string key, std::string value);
JObject& put(std::string key, JObject* value); JObject& put(std::string key, JObject* value);
@ -124,6 +130,9 @@ namespace json {
JObject* parse(); JObject* parse();
}; };
extern JObject* parse(std::string filename, std::string source);
extern JObject* parse(std::string source);
} }
#endif // CODERS_JSON_H_ #endif // CODERS_JSON_H_