GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/json_document.cc
Date: 2026-10-04 02:40:33
Exec Total Coverage
Lines: 45 61 73.8%
Branches: 40 74 54.1%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "json_document.h"
6
7 #include <memory>
8
9 #include "util/logging.h"
10
11 using namespace std; // NOLINT
12
13 1773 JsonDocument *JsonDocument::Create(const string &text) {
14
2/4
✓ Branch 1 taken 1773 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1773 times.
✗ Branch 5 not taken.
1773 std::unique_ptr<JsonDocument> json(new JsonDocument());
15
3/4
✓ Branch 2 taken 1773 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 192 times.
✓ Branch 5 taken 1581 times.
1773 if (!json->Parse(text))
16 192 return NULL;
17 1581 return json.release();
18 1773 }
19
20 1773 JsonDocument::JsonDocument() : root_(JSON::value_t::null) { }
21
22 1773 bool JsonDocument::Parse(const string &text) {
23
1/2
✓ Branch 2 taken 1773 times.
✗ Branch 3 not taken.
1773 root_ = JSON::parse(text, nullptr, false);
24
25
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 1581 times.
1773 if (root_.is_discarded()) {
26 192 LogCvmfs(kLogUtility, kLogDebug, "Failed to parse JSON string.");
27 192 root_ = JSON(JSON::value_t::null);
28 192 return false;
29 }
30 1581 return true;
31 }
32
33 6 string JsonDocument::PrintCanonical() {
34
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (root_.is_null())
35 ✗ return "";
36 6 return root_.dump();
37 }
38
39 4698 const JSON *JsonDocument::SearchInObject(const JSON *json_object,
40 const string &name,
41 const JSON::value_t type) {
42
5/6
✓ Branch 0 taken 4696 times.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4696 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 4696 times.
4698 if (!json_object || !json_object->is_object())
43 2 return NULL;
44
45
1/2
✓ Branch 1 taken 4696 times.
✗ Branch 2 not taken.
4696 auto it = json_object->find(name);
46
3/4
✓ Branch 2 taken 4696 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4067 times.
✓ Branch 5 taken 629 times.
4696 if (it != json_object->end()) {
47 // if we asked for an int, accept signed or unsigned
48
2/2
✓ Branch 0 taken 1716 times.
✓ Branch 1 taken 2351 times.
4067 if (type == JSON::value_t::number_integer) {
49
7/13
✓ Branch 1 taken 1716 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 1668 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 48 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 48 times.
✓ Branch 12 taken 1668 times.
✓ Branch 13 taken 48 times.
1716 if (it->is_number_integer() || it->is_number_unsigned()) {
50
1/2
✓ Branch 1 taken 1668 times.
✗ Branch 2 not taken.
1668 return &(*it);
51 }
52 }
53 // otherwise, stick to strict matching for strings, objects, etc.
54
2/5
✓ Branch 1 taken 2351 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2351 times.
✗ Branch 5 not taken.
2351 else if (it->type() == type) {
55
1/2
✓ Branch 1 taken 2351 times.
✗ Branch 2 not taken.
2351 return &(*it);
56 }
57 }
58 677 return NULL;
59 }
60
61 template<>
62 24 bool GetFromJSON<string>(const JSON *object,
63 const string &name,
64 string *value) {
65 24 const JSON *o = JsonDocument::SearchInObject(
66 object, name, JSON::value_t::string);
67
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (!o)
68 4 return false;
69
70
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (value) {
71 20 const string *s = o->get_ptr<const string *>();
72
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (s) {
73 20 *value = *s;
74 20 return true;
75 }
76 ✗ return false;
77 }
78 ✗ return true;
79 }
80
81 template<>
82 8 bool GetFromJSON<int>(const JSON *object, const string &name, int *value) {
83 8 const JSON *o = JsonDocument::SearchInObject(
84 object, name, JSON::value_t::number_integer);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!o) {
87 ✗ o = JsonDocument::SearchInObject(
88 object, name, JSON::value_t::number_unsigned);
89 }
90
91
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (!o || !value)
92 ✗ return false;
93
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (auto p = o->get_ptr<const JSON::number_integer_t *>()) {
95 ✗ *value = static_cast<int>(*p);
96 ✗ return true;
97
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 } else if (auto p = o->get_ptr<const JSON::number_unsigned_t *>()) {
98 8 *value = static_cast<int>(*p);
99 8 return true;
100 }
101
102 ✗ return false;
103 }
104
105 template<>
106 ✗ bool GetFromJSON<float>(const JSON *object, const string &name, float *value) {
107 ✗ const JSON *o = JsonDocument::SearchInObject(
108 object, name, JSON::value_t::number_float);
109 ✗ if (!o || !value)
110 ✗ return false;
111
112 ✗ if (auto p = o->get_ptr<const JSON::number_float_t *>()) {
113 ✗ *value = static_cast<float>(*p);
114 ✗ return true;
115 }
116 ✗ return false;
117 }
118