GCC Code Coverage Report


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