| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/json_document.cc |
| Date: | 2026-04-05 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 45 | 61 | 73.8% |
| Branches: | 41 | 76 | 53.9% |
| 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 "util/logging.h" | ||
| 8 | #include "util/pointer.h" | ||
| 9 | |||
| 10 | using namespace std; // NOLINT | ||
| 11 | |||
| 12 | 1098 | JsonDocument *JsonDocument::Create(const string &text) { | |
| 13 |
3/6✓ Branch 1 taken 1098 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1098 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1098 times.
✗ Branch 8 not taken.
|
1098 | UniquePtr<JsonDocument> json(new JsonDocument()); |
| 14 |
3/4✓ Branch 2 taken 1098 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 154 times.
✓ Branch 5 taken 944 times.
|
1098 | if (!json->Parse(text)) |
| 15 | 154 | return NULL; | |
| 16 | 944 | return json.Release(); | |
| 17 | 1098 | } | |
| 18 | |||
| 19 | 1098 | JsonDocument::JsonDocument() : root_(JSON::value_t::null) { } | |
| 20 | |||
| 21 | 1098 | bool JsonDocument::Parse(const string &text) { | |
| 22 |
1/2✓ Branch 2 taken 1098 times.
✗ Branch 3 not taken.
|
1098 | root_ = JSON::parse(text, nullptr, false); |
| 23 | |||
| 24 |
2/2✓ Branch 1 taken 154 times.
✓ Branch 2 taken 944 times.
|
1098 | if (root_.is_discarded()) { |
| 25 | 154 | LogCvmfs(kLogUtility, kLogDebug, "Failed to parse JSON string."); | |
| 26 | 154 | root_ = JSON(JSON::value_t::null); | |
| 27 | 154 | return false; | |
| 28 | } | ||
| 29 | 944 | return true; | |
| 30 | } | ||
| 31 | |||
| 32 | 12 | string JsonDocument::PrintCanonical() { | |
| 33 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (root_.is_null()) |
| 34 | ✗ | return ""; | |
| 35 | 12 | return root_.dump(); | |
| 36 | } | ||
| 37 | |||
| 38 | 2618 | const JSON *JsonDocument::SearchInObject(const JSON *json_object, | |
| 39 | const string &name, | ||
| 40 | const JSON::value_t type) { | ||
| 41 |
5/6✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2614 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2614 times.
|
2618 | if (!json_object || !json_object->is_object()) |
| 42 | 4 | return NULL; | |
| 43 | |||
| 44 |
1/2✓ Branch 1 taken 2614 times.
✗ Branch 2 not taken.
|
2614 | auto it = json_object->find(name); |
| 45 |
3/4✓ Branch 2 taken 2614 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2278 times.
✓ Branch 5 taken 336 times.
|
2614 | if (it != json_object->end()) { |
| 46 | // if we asked for an int, accept signed or unsigned | ||
| 47 |
2/2✓ Branch 0 taken 989 times.
✓ Branch 1 taken 1289 times.
|
2278 | if (type == JSON::value_t::number_integer) { |
| 48 |
7/13✓ Branch 1 taken 989 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 959 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 959 times.
✓ Branch 13 taken 30 times.
|
989 | if (it->is_number_integer() || it->is_number_unsigned()) { |
| 49 |
1/2✓ Branch 1 taken 959 times.
✗ Branch 2 not taken.
|
959 | return &(*it); |
| 50 | } | ||
| 51 | } | ||
| 52 | // otherwise, stick to strict matching for strings, objects, etc. | ||
| 53 |
2/5✓ Branch 1 taken 1289 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1289 times.
✗ Branch 5 not taken.
|
1289 | else if (it->type() == type) { |
| 54 |
1/2✓ Branch 1 taken 1289 times.
✗ Branch 2 not taken.
|
1289 | return &(*it); |
| 55 | } | ||
| 56 | } | ||
| 57 | 366 | return NULL; | |
| 58 | } | ||
| 59 | |||
| 60 | template<> | ||
| 61 | 120 | bool GetFromJSON<string>(const JSON *object, | |
| 62 | const string &name, | ||
| 63 | string *value) { | ||
| 64 | 120 | const JSON *o = JsonDocument::SearchInObject( | |
| 65 | object, name, JSON::value_t::string); | ||
| 66 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 100 times.
|
120 | if (!o) |
| 67 | 20 | return false; | |
| 68 | |||
| 69 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | if (value) { |
| 70 | 100 | const string *s = o->get_ptr<const string *>(); | |
| 71 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | if (s) { |
| 72 | 100 | *value = *s; | |
| 73 | 100 | return true; | |
| 74 | } | ||
| 75 | ✗ | return false; | |
| 76 | } | ||
| 77 | ✗ | return true; | |
| 78 | } | ||
| 79 | |||
| 80 | template<> | ||
| 81 | 40 | bool GetFromJSON<int>(const JSON *object, const string &name, int *value) { | |
| 82 | 40 | const JSON *o = JsonDocument::SearchInObject( | |
| 83 | object, name, JSON::value_t::number_integer); | ||
| 84 | |||
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (!o) { |
| 86 | ✗ | o = JsonDocument::SearchInObject( | |
| 87 | object, name, JSON::value_t::number_unsigned); | ||
| 88 | } | ||
| 89 | |||
| 90 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
|
40 | if (!o || !value) |
| 91 | ✗ | return false; | |
| 92 | |||
| 93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
|
40 | if (auto p = o->get_ptr<const JSON::number_integer_t *>()) { |
| 94 | ✗ | *value = static_cast<int>(*p); | |
| 95 | ✗ | return true; | |
| 96 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | } else if (auto p = o->get_ptr<const JSON::number_unsigned_t *>()) { |
| 97 | 40 | *value = static_cast<int>(*p); | |
| 98 | 40 | return true; | |
| 99 | } | ||
| 100 | |||
| 101 | ✗ | return false; | |
| 102 | } | ||
| 103 | |||
| 104 | template<> | ||
| 105 | ✗ | bool GetFromJSON<float>(const JSON *object, const string &name, float *value) { | |
| 106 | ✗ | const JSON *o = JsonDocument::SearchInObject( | |
| 107 | object, name, JSON::value_t::number_float); | ||
| 108 | ✗ | if (!o || !value) | |
| 109 | ✗ | return false; | |
| 110 | |||
| 111 | ✗ | if (auto p = o->get_ptr<const JSON::number_float_t *>()) { | |
| 112 | ✗ | *value = static_cast<float>(*p); | |
| 113 | ✗ | return true; | |
| 114 | } | ||
| 115 | ✗ | return false; | |
| 116 | } | ||
| 117 |