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