• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 13111317048

03 Feb 2025 10:22AM UTC coverage: 64.724% (+9.5%) from 55.194%
13111317048

Pull #14724

github

web-flow
Merge cced151a0 into db18c3a17
Pull Request #14724: dnsdist: Add meson support

38354 of 90334 branches covered (42.46%)

Branch coverage included in aggregate %.

449 of 624 new or added lines in 40 files covered. (71.96%)

6809 existing lines in 147 files now uncovered.

128214 of 167016 relevant lines covered (76.77%)

4848378.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/pdns/json.cc
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#include "json.hh"
26
#include "namespaces.hh"
27
#include "misc.hh"
28

29
using json11::Json;
30

31
static inline int intFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const int default_value)
32
{
939✔
33
  const auto& val = container[key];
939✔
34
  if (val.is_number()) {
939✔
35
    return val.int_value();
925✔
36
  }
925✔
37

38
  if (val.is_string()) {
14✔
39
    try {
3✔
40
      return std::stoi(val.string_value());
3✔
41
    } catch (std::out_of_range&) {
3✔
42
      throw JsonException("Key '" + string(key) + "' is out of range");
3✔
43
    }
3✔
44
  }
3✔
45

46
  if (have_default) {
11!
47
    return default_value;
11✔
48
  }
11✔
UNCOV
49
  throw JsonException("Key '" + string(key) + "' not an Integer or not present");
×
50
}
11✔
51

52
int intFromJson(const Json& container, const std::string& key)
53
{
56✔
54
  return intFromJsonInternal(container, key, false, 0);
56✔
55
}
56✔
56

57
int intFromJson(const Json& container, const std::string& key, const int default_value)
58
{
554✔
59
  return intFromJsonInternal(container, key, true, default_value);
554✔
60
}
554✔
61

62
static inline unsigned int uintFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const unsigned int default_value)
63
{
329✔
64
  int intval = intFromJsonInternal(container, key, have_default, static_cast<int>(default_value));
329✔
65
  if (intval >= 0) {
329✔
66
    return intval;
322✔
67
  }
322✔
68
  throw JsonException("Key '" + string(key) + "' is not a positive Integer");
7✔
69
}
329✔
70

71
unsigned int uintFromJson(const Json& container, const std::string& key)
72
{
301✔
73
  return uintFromJsonInternal(container, key, false, 0);
301✔
74
}
301✔
75

76
unsigned int uintFromJson(const Json& container, const std::string& key, const unsigned int default_value)
77
{
28✔
78
  return uintFromJsonInternal(container, key, true, default_value);
28✔
79
}
28✔
80

81
static inline double doubleFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const double default_value)
82
{
24✔
83
  const auto& val = container[key];
24✔
84
  if (val.is_number()) {
24!
85
    return val.number_value();
×
86
  }
×
87

88
  if (val.is_string()) {
24!
89
    try {
×
90
      return std::stod(val.string_value());
×
91
    } catch (std::out_of_range&) {
×
92
      throw JsonException("Value for key '" + string(key) + "' is out of range");
×
93
    }
×
94
  }
×
95

96
  if (have_default) {
24!
97
    return default_value;
24✔
98
  }
24✔
99
  throw JsonException("Key '" + string(key) + "' not an Integer or not present");
×
100
}
24✔
101

102
double doubleFromJson(const Json& container, const std::string& key)
UNCOV
103
{
×
104
  return doubleFromJsonInternal(container, key, false, 0);
×
105
}
×
106

107
double doubleFromJson(const Json& container, const std::string& key, const double default_value)
108
{
24✔
109
  return doubleFromJsonInternal(container, key, true, default_value);
24✔
110
}
24✔
111

112
string stringFromJson(const Json& container, const std::string &key)
113
{
5,473✔
114
  const auto& val = container[key];
5,473✔
115
  if (val.is_string()) {
5,473✔
116
    return val.string_value();
4,948✔
117
  }
4,948✔
118
  throw JsonException("Key '" + string(key) + "' not present or not a String");
525✔
119
}
5,473✔
120

121
static inline bool boolFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const bool default_value)
122
{
2,156✔
123
  const auto& val = container[key];
2,156✔
124
  if (val.is_bool()) {
2,156✔
125
    return val.bool_value();
563✔
126
  }
563✔
127
  if (have_default) {
1,593✔
128
    return default_value;
572✔
129
  }
572✔
130
  throw JsonException("Key '" + string(key) + "' not present or not a Bool");
1,021✔
131
}
1,593✔
132

133
bool boolFromJson(const Json& container, const std::string& key)
134
{
1,379✔
135
  return boolFromJsonInternal(container, key, false, false);
1,379✔
136
}
1,379✔
137

138
bool boolFromJson(const Json& container, const std::string& key, const bool default_value)
139
{
777✔
140
  return boolFromJsonInternal(container, key, true, default_value);
777✔
141
}
777✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc