From b9ed7a2b288d2ac4b39775d80b6d36a73500f32a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 10 Nov 2023 21:26:09 +0300 Subject: [PATCH] Added more get/put methods to json objects --- src/coders/json.cpp | 16 ++++++++++++++++ src/coders/json.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/src/coders/json.cpp b/src/coders/json.cpp index a53eb82f..e4453234 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -247,6 +247,13 @@ JArray& JArray::put(number_t value) { return *this; } +JArray& JArray::put(float value) { + valvalue val; + val.num = value; + values.push_back(new Value(valtype::number, val)); + return *this; +} + JArray& JArray::put(bool value) { valvalue val; val.boolean = value; @@ -286,6 +293,12 @@ void JObject::num(std::string key, number_t& dst) const { dst = found->second->value.num; } +void JObject::num(std::string key, float& dst) const { + auto found = map.find(key); + if (found != map.end()) + dst = found->second->value.num; +} + void JObject::num(std::string key, int& dst) const { auto found = map.find(key); if (found != map.end()) @@ -326,6 +339,9 @@ JObject& JObject::put(string key, int value) { return put(key, (number_t)value); } +JObject& JObject::put(string key, float value) { + return put(key, (number_t)value); +} JObject& JObject::put(string key, number_t value) { auto found = map.find(key); diff --git a/src/coders/json.h b/src/coders/json.h index a3c74985..4c75d683 100644 --- a/src/coders/json.h +++ b/src/coders/json.h @@ -72,6 +72,7 @@ namespace json { JArray& put(uint value); JArray& put(int value); + JArray& put(float value); JArray& put(number_t value); JArray& put(std::string value); JArray& put(JObject* value); @@ -86,6 +87,7 @@ namespace json { void str(std::string key, std::string& dst) const; void num(std::string key, int& dst) const; + void num(std::string key, float& 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; @@ -94,6 +96,7 @@ namespace json { JObject& put(std::string key, uint value); JObject& put(std::string key, int value); + JObject& put(std::string key, float value); JObject& put(std::string key, number_t value); JObject& put(std::string key, std::string value); JObject& put(std::string key, JObject* value);