GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/json_document.cc
Date: 2026-10-11 02:40:02
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 2028 JsonDocument *JsonDocument::Create(const string &text) {
14
2/4
✓ Branch 1 taken 2028 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2028 times.
✗ Branch 5 not taken.
2028 std::unique_ptr<JsonDocument> json(new JsonDocument());
15
3/4
✓ Branch 2 taken 2028 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 146 times.
✓ Branch 5 taken 1882 times.
2028 if (!json->Parse(text))
16 146 return NULL;
17 1882 return json.release();
18 2028 }
19
20 2028 JsonDocument::JsonDocument() : root_(JSON::value_t::null) { }
21
22 2028 bool JsonDocument::Parse(const string &text) {
23
1/2
✓ Branch 2 taken 2028 times.
✗ Branch 3 not taken.
2028 root_ = JSON::parse(text, nullptr, false);
24
25
2/2
✓ Branch 1 taken 146 times.
✓ Branch 2 taken 1882 times.
2028 if (root_.is_discarded()) {
26 146 LogCvmfs(kLogUtility, kLogDebug, "Failed to parse JSON string.");
27 146 root_ = JSON(JSON::value_t::null);
28 146 return false;
29 }
30 1882 return true;
31 }
32
33 132 string JsonDocument::PrintCanonical() {
34
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
132 if (root_.is_null())
35 ✗ return "";
36 132 return root_.dump();
37 }
38
39 5052 const JSON *JsonDocument::SearchInObject(const JSON *json_object,
40 const string &name,
41 const JSON::value_t type) {
42
5/6
✓ Branch 0 taken 5008 times.
✓ Branch 1 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5008 times.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 5008 times.
5052 if (!json_object || !json_object->is_object())
43 44 return NULL;
44
45
1/2
✓ Branch 1 taken 5008 times.
✗ Branch 2 not taken.
5008 auto it = json_object->find(name);
46
3/4
✓ Branch 2 taken 5008 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4360 times.
✓ Branch 5 taken 648 times.
5008 if (it != json_object->end()) {
47 // if we asked for an int, accept signed or unsigned
48
2/2
✓ Branch 0 taken 1682 times.
✓ Branch 1 taken 2678 times.
4360 if (type == JSON::value_t::number_integer) {
49
7/13
✓ Branch 1 taken 1682 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
✓ Branch 5 taken 1595 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 87 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 87 times.
✓ Branch 12 taken 1595 times.
✓ Branch 13 taken 87 times.
1682 if (it->is_number_integer() || it->is_number_unsigned()) {
50
1/2
✓ Branch 1 taken 1595 times.
✗ Branch 2 not taken.
1595 return &(*it);
51 }
52 }
53 // otherwise, stick to strict matching for strings, objects, etc.
54
2/5
✓ Branch 1 taken 2678 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2678 times.
✗ Branch 5 not taken.
2678 else if (it->type() == type) {
55
1/2
✓ Branch 1 taken 2678 times.
✗ Branch 2 not taken.
2678 return &(*it);
56 }
57 }
58 735 return NULL;
59 }
60
61 template<>
62 48 bool GetFromJSON<string>(const JSON *object,
63 const string &name,
64 string *value) {
65 48 const JSON *o = JsonDocument::SearchInObject(
66 object, name, JSON::value_t::string);
67
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 40 times.
48 if (!o)
68 8 return false;
69
70
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (value) {
71 40 const string *s = o->get_ptr<const string *>();
72
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (s) {
73 40 *value = *s;
74 40 return true;
75 }
76 ✗ return false;
77 }
78 ✗ return true;
79 }
80
81 template<>
82 16 bool GetFromJSON<int>(const JSON *object, const string &name, int *value) {
83 16 const JSON *o = JsonDocument::SearchInObject(
84 object, name, JSON::value_t::number_integer);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!o) {
87 ✗ o = JsonDocument::SearchInObject(
88 object, name, JSON::value_t::number_unsigned);
89 }
90
91
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (!o || !value)
92 ✗ return false;
93
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 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 16 times.
✗ Branch 2 not taken.
16 } else if (auto p = o->get_ptr<const JSON::number_unsigned_t *>()) {
98 16 *value = static_cast<int>(*p);
99 16 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