diff --git a/src/coders/json.cpp b/src/coders/json.cpp index 7c96ed73..a53eb82f 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -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("", source); } \ No newline at end of file diff --git a/src/coders/json.h b/src/coders/json.h index dc0343cb..a3c74985 100644 --- a/src/coders/json.h +++ b/src/coders/json.h @@ -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 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_ \ No newline at end of file