]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/json.cc
auth: switch circleci mssql image
[thirdparty/pdns.git] / pdns / json.cc
index ee6022afeacdf7f05eadc3b722c374b97e0524f3..b1847d06a31fe527d4b1eb1ea7f9d825878c0b69 100644 (file)
@@ -1,24 +1,24 @@
 /*
-    Copyright (C) 2002 - 2015  PowerDNS.COM BV
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License version 2
-    as published by the Free Software Foundation
-
-    Additionally, the license of this program contains a special
-    exception which allows to distribute the program in binary form when
-    it is linked against OpenSSL.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
-
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
* You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include "namespaces.hh"
 #include "misc.hh"
 
-#include "rapidjson/document.h"
-#include "rapidjson/stringbuffer.h"
-#include "rapidjson/writer.h"
-
-using namespace rapidjson;
+using json11::Json;
 
-int intFromJson(const Value& container, const char* key)
+int intFromJson(const Json container, const std::string& key)
 {
-  if (!container.IsObject()) {
-    throw JsonException("Container was not an object.");
-  }
-  const Value& val = container[key];
-  if (val.IsInt()) {
-    return val.GetInt();
-  } else if (val.IsString()) {
-    return std::stoi(val.GetString());
+  auto val = container[key];
+  if (val.is_number()) {
+    return val.int_value();
+  } else if (val.is_string()) {
+    return std::stoi(val.string_value());
   } else {
     throw JsonException("Key '" + string(key) + "' not an Integer or not present");
   }
 }
 
-int intFromJson(const Value& container, const char* key, const int default_value)
+int intFromJson(const Json container, const std::string& key, const int default_value)
 {
-  if (!container.IsObject()) {
-    throw JsonException("Container was not an object.");
-  }
-  const Value& val = container[key];
-  if (val.IsInt()) {
-    return val.GetInt();
-  } else if (val.IsString()) {
-    return std::stoi(val.GetString());
+  auto val = container[key];
+  if (val.is_number()) {
+    return val.int_value();
+  } else if (val.is_string()) {
+    try {
+      return std::stoi(val.string_value());
+    } catch (std::out_of_range&) {
+      throw JsonException("Value for key '" + string(key) + "' is out of range");
+    }
   } else {
     // TODO: check if value really isn't present
     return default_value;
   }
 }
 
-string stringFromJson(const Value& container, const char* key)
+double doubleFromJson(const Json container, const std::string& key)
 {
-  if (!container.IsObject()) {
-    throw JsonException("Container was not an object.");
-  }
-  const Value& val = container[key];
-  if (val.IsString()) {
-    return val.GetString();
+  auto val = container[key];
+  if (val.is_number()) {
+    return val.number_value();
+  } else if (val.is_string()) {
+    try {
+      return std::stod(val.string_value());
+    } catch (std::out_of_range&) {
+      throw JsonException("Value for key '" + string(key) + "' is out of range");
+    }
   } else {
-    throw JsonException("Key '" + string(key) + "' not present or not a String");
+    throw JsonException("Key '" + string(key) + "' not an Integer or not present");
   }
 }
 
-string stringFromJson(const Value& container, const char* key, const string& default_value)
+double doubleFromJson(const Json container, const std::string& key, const double default_value)
 {
-  if (!container.IsObject()) {
-    throw JsonException("Container was not an object.");
-  }
-  const Value& val = container[key];
-  if (val.IsString()) {
-    return val.GetString();
+  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;
   }
 }
 
-bool boolFromJson(const rapidjson::Value& container, const char* key)
+string stringFromJson(const Json container, const std::string &key)
 {
-  if (!container.IsObject()) {
-    throw JsonException("Container was not an object.");
-  }
-  const Value& val = container[key];
-  if (val.IsBool()) {
-    return val.GetBool();
+  const Json val = container[key];
+  if (val.is_string()) {
+    return val.string_value();
   } else {
-    throw JsonException("Key '" + string(key) + "' not present or not a Bool");
+    throw JsonException("Key '" + string(key) + "' not present or not a String");
   }
 }
 
-bool boolFromJson(const rapidjson::Value& container, const char* key, const bool default_value)
+bool boolFromJson(const Json container, const std::string& key)
 {
-  if (!container.IsObject()) {
-    throw JsonException("Container was not an object.");
-  }
-  const Value& val = container[key];
-  if (val.IsBool()) {
-    return val.GetBool();
-  } else {
-    return default_value;
+  auto val = container[key];
+  if (val.is_bool()) {
+    return val.bool_value();
   }
+  throw JsonException("Key '" + string(key) + "' not present or not a Bool");
 }
 
-string makeStringFromDocument(const Document& doc)
-{
-  StringBuffer output;
-  Writer<StringBuffer> w(output);
-  doc.Accept(w);
-  return string(output.GetString(), output.Size());
-}
-
-string returnJsonObject(const map<string, string>& items)
+bool boolFromJson(const Json container, const std::string& key, const bool default_value)
 {
-  Document doc;
-  doc.SetObject();
-  typedef map<string, string> items_t;
-  for(const items_t::value_type& val :  items) {
-    doc.AddMember(val.first.c_str(), val.second.c_str(), doc.GetAllocator());
+  auto val = container[key];
+  if (val.is_bool()) {
+    return val.bool_value();
   }
-  return makeStringFromDocument(doc);
-}
-
-string returnJsonError(const string& error)
-{
-  Document doc;
-  doc.SetObject();
-  Value jerror(error.c_str(), doc.GetAllocator()); // copy
-  doc.AddMember("error", jerror, doc.GetAllocator());
-  return makeStringFromDocument(doc);
-}
-
-/* success response */
-string returnJsonMessage(const string& message)
-{
-  Document doc;
-  doc.SetObject();
-  Value jmessage;
-  jmessage.SetString(message.c_str());
-  doc.AddMember("result", jmessage, doc.GetAllocator());
-  return makeStringFromDocument(doc);
+  return default_value;
 }