BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitemformatassistant.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/serialization/jsonitemformatassistant.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <QJsonObject>
17 #include <QStringList>
18 
19 using namespace ModelView;
20 
21 namespace {
22 //! Returns list of keys which should be in QJsonObject to represent SessionItem.
23 QStringList expected_item_keys()
24 {
27  std::sort(result.begin(), result.end());
28  return result;
29 }
30 
31 //! Returns list of keys which should be in QJsonObject to represent SessionItemData.
32 
33 QStringList expected_itemdata_keys()
34 {
36  std::sort(result.begin(), result.end());
37  return result;
38 }
39 
40 //! Returns list of keys which should be in QJsonObject to represent SessionItemTags.
41 
42 QStringList expected_tags_keys()
43 {
44  QStringList result{JsonItemFormatAssistant::defaultTagKey,
46  std::sort(result.begin(), result.end());
47  return result;
48 }
49 
50 //! Returns list of keys which should be in QJsonObject to represent SessionItemContainer.
51 
52 QStringList expected_itemcontainer_keys()
53 {
55  std::sort(result.begin(), result.end());
56  return result;
57 }
58 
59 //! Returns list of keys which should be in QJsonObject to represent SessionModel.
60 
61 QStringList expected_sessionmodel_keys()
62 {
64  std::sort(result.begin(), result.end());
65  return result;
66 }
67 
68 } // namespace
69 
70 //! Returns true if given json object represents SessionItem.
71 
72 bool JsonItemFormatAssistant::isSessionItem(const QJsonObject& json) const
73 {
74  static const QStringList expected = expected_item_keys();
75 
76  if (json.keys() != expected)
77  return false;
78 
79  if (!json[itemDataKey].isArray())
80  return false;
81 
82  if (!json[itemTagsKey].isObject())
83  return false;
84 
85  return true;
86 }
87 
88 bool JsonItemFormatAssistant::isSessionItemData(const QJsonObject& json) const
89 {
90  static const QStringList expected = expected_itemdata_keys();
91  return json.keys() == expected;
92 }
93 
94 //! Returns true if given json object represents SessionItemTags.
95 
96 bool JsonItemFormatAssistant::isSessionItemTags(const QJsonObject& json) const
97 {
98  static const QStringList expected = expected_tags_keys();
99 
100  if (json.keys() != expected)
101  return false;
102 
103  if (!json[containerKey].isArray())
104  return false;
105 
106  return true;
107 }
108 
109 //! Returns true if given json object represents SessionItemContainer.
110 
111 bool JsonItemFormatAssistant::isSessionItemContainer(const QJsonObject& json) const
112 {
113  static const QStringList expected = expected_itemcontainer_keys();
114 
115  if (json.keys() != expected)
116  return false;
117 
118  if (!json[tagInfoKey].isObject())
119  return false;
120 
121  if (!json[itemsKey].isArray())
122  return false;
123 
124  return true;
125 }
126 
127 //! Returns true if given json object represents SessionModel.
128 
129 bool JsonItemFormatAssistant::isSessionModel(const QJsonObject& object) const
130 {
131  static const QStringList expected = expected_sessionmodel_keys();
132 
133  if (object.keys() != expected)
134  return false;
135 
136  if (!object[itemsKey].isArray())
137  return false;
138 
139  return true;
140 }
bool isSessionModel(const QJsonObject &object) const
Returns true if given json object represents SessionModel.
bool isSessionItemContainer(const QJsonObject &json) const
Returns true if given json object represents SessionItemContainer.
bool isSessionItem(const QJsonObject &json) const
Returns true if given json object represents SessionItem.
bool isSessionItemTags(const QJsonObject &json) const
Returns true if given json object represents SessionItemTags.
bool isSessionItemData(const QJsonObject &json) const
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.