• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphia-app / graphia / 41725894897

03 Nov 2026 09:48PM UTC coverage: 71.364% (-0.03%) from 71.391%
41725894897

push

github

timangus
CLAUDE: Vendor QCustomPlot's split sources with the update script

11762 of 18036 branches covered (65.21%)

Branch coverage included in aggregate %.

29960 of 40428 relevant lines covered (74.11%)

695585.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/source/shared/utils/json.cpp
1
/* Copyright © 2013-2025 Tim Angus
2
 * Copyright © 2013-2025 Tom Freeman
3
 *
4
 * This file is part of Graphia.
5
 *
6
 * Graphia is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Graphia is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with Graphia.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#include "json.h"
21

22
#include "shared/loading/iparser.h"
23
#include "shared/graph/elementid.h"
24
#include "shared/utils/progressable.h"
25
#include "shared/loading/progress_iterator.h"
26

27
#include <QString>
28
#include <QUrl>
29
#include <QFile>
30
#include <QByteArray>
31
#include <QVariant>
32
#include <QVariantList>
33
#include <QVariantMap>
34

35
#include <string>
36
#include <vector>
37

38
#include <json.hpp>
39
using json = nlohmann::json;
40

41
void to_json(json &j, const QString &s)
42
{
3,340 ✔
43
    j = s.toStdString();
3,340 ✔
44
}
3,340 ✔
45

46
void to_json(json &j, NodeId nodeId)
47
{
1,566 ✔
48
    j = static_cast<int>(nodeId);
1,566 ✔
49
}
1,566 ✔
50

51
void to_json(json &j, EdgeId edgeId)
52
{
364 ✔
53
    j = static_cast<int>(edgeId);
364 ✔
54
}
364 ✔
55

56
void to_json(json &j, ComponentId componentId)
57
{
2 ✔
58
    j = static_cast<int>(componentId);
2 ✔
59
}
2 ✔
60

61
void from_json(const json &j, QString &s)
62
{
3,908 ✔
63
    s = QString::fromStdString(j.get<std::string>());
3,908 ✔
64
}
3,908 ✔
65

66
void from_json(const json &j, QUrl &url)
67
{
2 ✔
68
    url = QString::fromStdString(j.get<std::string>());
2 ✔
69
}
2 ✔
70

71
void from_json(const json &j, QVariant &v)
72
{
60 ✔
73
    switch(j.type())
60 ✔
74
    {
60 ✔
75
    default:
×
76
    case json::value_t::null:               v = {}; break;
2 ✔
77
    case json::value_t::boolean:            v = QVariant::fromValue(j.get<bool>()); break;
2 ✔
78
    case json::value_t::number_integer:     v = QVariant::fromValue(j.get<qint64>()); break;
2 ✔
79
    case json::value_t::number_unsigned:    v = QVariant::fromValue(j.get<quint64>()); break;
14 ✔
80
    case json::value_t::number_float:       v = QVariant::fromValue(j.get<double>()); break;
6 ✔
81
    case json::value_t::string:             v = QVariant::fromValue(QString::fromStdString(j.get<std::string>())); break;
12 ✔
82
    case json::value_t::array:
8 ✔
83
    {
8 ✔
84
        QVariantList variantList;
8 ✔
85
        variantList.reserve(static_cast<int>(j.size()));
8 ✔
86

87
        for(const auto& element : j)
8 ✔
88
        {
12 ✔
89
            QVariant variantElement;
12 ✔
90
            from_json(element, variantElement);
12 ✔
91
            variantList.append(variantElement);
12 ✔
92
        }
12 ✔
93

94
        v = variantList;
8 ✔
95
        break;
8 ✔
96
    }
×
97
    case json::value_t::object:
14 ✔
98
    {
14 ✔
99
        QVariantMap variantMap;
14 ✔
100

101
        for(const auto& element : j.items())
14 ✔
102
        {
34 ✔
103
            auto key = QString::fromStdString(element.key());
34 ✔
104
            QVariant variantValue;
34 ✔
105
            from_json(element.value(), variantValue);
34 ✔
106
            variantMap.insert(key, variantValue);
34 ✔
107
        }
34 ✔
108

109
        v = QVariant::fromValue(variantMap);
14 ✔
110
        break;
14 ✔
111
    }
×
112
    }
60 ✔
113
}
60 ✔
114

115
json jsonArrayFrom(const std::vector<QString> &container, Progressable *progressable)
116
{
92 ✔
117
    json array;
92 ✔
118

119
    uint64_t i = 0;
92 ✔
120
    for(const auto& value : container)
92 ✔
121
    {
530 ✔
122
        array.emplace_back(value);
530 ✔
123

124
        if(progressable != nullptr)
530 ✔
125
            progressable->setProgress(static_cast<int>((i++) * 100 / container.size()));
526 ✔
126
    }
530 ✔
127

128
    if(progressable != nullptr)
92 ✔
129
        progressable->setProgress(-1);
90 ✔
130

131
    return array;
92 ✔
132
}
92 ✔
133

134
namespace
135
{
136
// This exists to avoid having to use exceptions for error handling
137
template<typename InputAdapter>
138
class JsonDomParser : public nlohmann::detail::json_sax_dom_parser<json, InputAdapter>
139
{
140
public:
141
    explicit JsonDomParser(json& result) :
142
        nlohmann::detail::json_sax_dom_parser<json, InputAdapter>(result, false)
802 ✔
143
    {}
802 ✔
144

145
    template<typename Exception>
146
    bool parse_error(std::size_t position, const std::string& lastToken, const Exception& exception)
147
    {
20 ✔
148
        _failureReason = exception.what();
20 ✔
149
        return nlohmann::detail::json_sax_dom_parser<json, InputAdapter>::parse_error(
20 ✔
150
            position, lastToken, exception);
20 ✔
151
    }
20 ✔
152

153
    const std::string& failureReason() const { return _failureReason; }
10 ✔
154

155
private:
156
    std::string _failureReason;
157
};
158
} // namespace
159

160
json parseJsonFrom(const QByteArray &byteArray, IParser *parser)
161
{
802 ✔
162
    json result;
802 ✔
163

164
    using JSONByteArrayIterator = progress_iterator<QByteArray::const_iterator>;
802 ✔
165
    JSONByteArrayIterator it(byteArray.begin());
802 ✔
166
    const JSONByteArrayIterator end(byteArray.end());
802 ✔
167

168
    if(parser != nullptr)
802 ✔
169
    {
366 ✔
170
        it.onPositionChanged(
366 ✔
171
            [&byteArray, &parser](size_t position)
366 ✔
172
            {
366 ✔
173
                parser->setProgress(static_cast<int>((position * 100) /
50 ✔
174
                    static_cast<size_t>(byteArray.size())));
50 ✔
175
            });
50 ✔
176

177
        it.setCancelledFn([&parser] { return parser->cancelled(); });
366 ✔
178
    }
366 ✔
179

180
    using InputAdapter = decltype(nlohmann::detail::input_adapter(it, end));
802 ✔
181
    JsonDomParser<InputAdapter> domParser(result);
802 ✔
182

183
    if(!json::sax_parse(it, end, &domParser))
802 ✔
184
    {
20 ✔
185
        result = {};
20 ✔
186

187
        if(parser != nullptr)
20 ✔
188
            parser->setFailureReason(QString::fromStdString(domParser.failureReason()));
10 ✔
189
    }
20 ✔
190

191
    return result;
802 ✔
192
}
802 ✔
193

194
json parseJsonFrom(const QString &filename)
195
{
36 ✔
196
    QFile file(filename);
36 ✔
197
    if(!file.open(QIODeviceBase::ReadOnly))
36 !
198
        return {};
×
199

200
    return parseJsonFrom(file.readAll());
36 ✔
201
}
36 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc