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;
}
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) {
valvalue val;
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);
if (found != map.end())
return *found->second->value.str;
return def;
dst = *found->second->value.str;
}
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);
if (found != map.end())
return found->second->value.num;
return def;
dst = found->second->value.num;
}
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 {
@ -294,13 +312,21 @@ JArray* JObject::arr(std::string key) const {
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);
if (found != map.end())
return found->second->value.boolean;
return def;
dst = found->second->value.boolean;
}
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) {
auto found = map.find(key);
if (found != map.end()) delete found->second;
@ -636,4 +662,13 @@ Value* Parser::parseValue() {
return new Value(valtype::string, val);
}
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();
}
JArray& put(uint value);
JArray& put(int value);
JArray& put(number_t value);
JArray& put(std::string value);
JArray& put(JObject* value);
@ -82,12 +84,16 @@ namespace json {
std::unordered_map<std::string, Value*> map;
~JObject();
std::string str(std::string key, std::string def) const;
number_t num(std::string key, number_t def) const;
void str(std::string key, std::string& dst) 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;
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, std::string value);
JObject& put(std::string key, JObject* value);
@ -124,6 +130,9 @@ namespace json {
JObject* parse();
};
extern JObject* parse(std::string filename, std::string source);
extern JObject* parse(std::string source);
}
#endif // CODERS_JSON_H_