From 7227cd1ae113713814c783e61311287a909a915f Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Sun, 10 Jan 2016 20:22:27 +0200 Subject: [PATCH] Move special bolean feature to remotebackend --- modules/remotebackend/remotebackend.cc | 2 +- modules/remotebackend/remotebackend.hh | 10 ++++++++++ pdns/json.cc | 6 ------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/modules/remotebackend/remotebackend.cc b/modules/remotebackend/remotebackend.cc index 1b10a90954..19a85152aa 100644 --- a/modules/remotebackend/remotebackend.cc +++ b/modules/remotebackend/remotebackend.cc @@ -357,7 +357,7 @@ bool RemoteBackend::getDomainKeys(const DNSName& name, unsigned int kind, std::v DNSBackend::KeyData key; key.id = intFromJson(jsonKey, "id"); key.flags = intFromJson(jsonKey, "flags"); - key.active = boolFromJson(jsonKey, "active"); + key.active = asBool(jsonKey["active"]); key.content = stringFromJson(jsonKey, "content"); keys.push_back(key); } diff --git a/modules/remotebackend/remotebackend.hh b/modules/remotebackend/remotebackend.hh index b3c745f8d8..8e0ffa5e63 100644 --- a/modules/remotebackend/remotebackend.hh +++ b/modules/remotebackend/remotebackend.hh @@ -188,5 +188,15 @@ class RemoteBackend : public DNSBackend if (value.is_string()) return value.string_value(); throw JsonException("Json value not convertible to String"); }; + + bool asBool(const Json& value) { + if (value.is_bool()) return value.bool_value(); + try { + string val = asString(value); + if (val == "0") return false; + if (val == "1") return true; + } catch (JsonException) {}; + throw JsonException("Json value not convertible to boolean"); + }; }; #endif diff --git a/pdns/json.cc b/pdns/json.cc index cab7fb4faa..1d76b01898 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -93,9 +93,6 @@ bool boolFromJson(const Json container, const std::string& key) auto val = container[key]; if (val.is_bool()) { return val.bool_value(); - } else if (val.is_number()) { - if (val.int_value() == 1) return true; - else if (val.int_value() == 0) return false; } throw JsonException("Key '" + string(key) + "' not present or not a Bool"); } @@ -105,9 +102,6 @@ bool boolFromJson(const Json container, const std::string& key, const bool defau auto val = container[key]; if (val.is_bool()) { return val.bool_value(); - } else if (val.is_number()) { - if (val.int_value() == 1) return true; - else if (val.int_value() == 0) return false; } return default_value; } -- 2.47.2