swift
jsonexception.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/jsonexception.h"
5 
6 #include <vector>
7 
8 #include <QStringBuilder>
9 #include <QThreadStorage>
10 
11 #include "misc/range.h"
12 
13 namespace swift::misc
14 {
15  static QThreadStorage<std::vector<const CJsonScope *>> g_stack;
16 
18  auto &jsonStack() noexcept { return g_stack.localData(); }
19 
20  // pin vtables to this file
21  void CJsonException::anchor() {}
22 
23  QString CJsonException::toString(const QString &prefix) const
24  {
25  if (prefix.isEmpty()) { return QStringLiteral("%1 in '%2'").arg(what()).arg(getStackTrace()); }
26  return QStringLiteral("%1: %2 in '%3'").arg(prefix).arg(what()).arg(getStackTrace());
27  }
28 
29  QString CJsonException::stackString()
30  {
31  QStringList list;
32  for (const auto scope : std::as_const(jsonStack()))
33  {
34  list.push_back(scope->m_string ? *scope->m_string : scope->m_latin1); // clazy:exclude=reserve-candidates
35  if (scope->m_index >= 0) { list.back() += u'[' % QString::number(scope->m_index) % u']'; }
36  }
37  return list.isEmpty() ? QStringLiteral("<document root>") : list.join('.');
38  }
39 
40  void CJsonScope::push() const noexcept { jsonStack().push_back(this); }
41 
42  void CJsonScope::pop() const noexcept
43  {
44  Q_ASSERT(jsonStack().back() == this);
45  jsonStack().pop_back();
46  }
47 } // namespace swift::misc
QString toString(const QString &prefix) const
As string info.
const QString & getStackTrace() const
Get a stack trace of where in the JSON object tree the error occurred.
Definition: jsonexception.h:32
Free functions in swift::misc.