GCC Code Coverage Report


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