From: Aki Tuomi Date: Fri, 8 Jan 2016 12:37:47 +0000 (+0200) Subject: Add doubleFromJson X-Git-Tag: dnsdist-1.0.0-alpha2~36^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=81399d19c42bbf1d5b2e05c31bd1b14b31e8013f;p=thirdparty%2Fpdns.git Add doubleFromJson --- diff --git a/pdns/json.cc b/pdns/json.cc index c0fda4e7ae..29e24f2caf 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -53,6 +53,31 @@ int intFromJson(const Json container, const std::string& key, const int default_ } } +double doubleFromJson(const Json container, const std::string& key) +{ + auto val = container[key]; + if (val.is_number()) { + return val.number_value(); + } else if (val.is_string()) { + return std::stod(val.string_value()); + } else { + throw JsonException("Key '" + string(key) + "' not an Integer or not present"); + } +} + +double doubleFromJson(const Json container, const std::string& key, const double default_value) +{ + auto val = container[key]; + if (val.is_number()) { + return val.number_value(); + } else if (val.is_string()) { + return std::stod(val.string_value()); + } else { + // TODO: check if value really isn't present + return default_value; + } +} + string stringFromJson(const Json container, const std::string &key) { const Json val = container[key]; diff --git a/pdns/json.hh b/pdns/json.hh index ad214c5d61..9d08f27b08 100644 --- a/pdns/json.hh +++ b/pdns/json.hh @@ -27,6 +27,8 @@ int intFromJson(const json11::Json container, const std::string& key); int intFromJson(const json11::Json container, const std::string& key, const int default_value); +double doubleFromJson(const json11::Json container, const std::string& key); +double doubleFromJson(const json11::Json container, const std::string& key, const double default_value); std::string stringFromJson(const json11::Json container, const std::string &key); bool boolFromJson(const json11::Json container, const std::string& key); bool boolFromJson(const json11::Json container, const std::string& key, const bool default_value);