BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::JsonModelConverter Class Reference

Converter of SessionModel to/from json object with posibility to select one of convertion modes. More...

Inheritance diagram for ModelView::JsonModelConverter:
[legend]
Collaboration diagram for ModelView::JsonModelConverter:
[legend]

Public Member Functions

 JsonModelConverter (ConverterMode mode)
 
 ~JsonModelConverter () override
 
void from_json (const QJsonObject &json, SessionModel &model) const override
 Reads json object and build the model. More...
 
QJsonObject to_json (const SessionModel &model) const override
 Writes content of model into json. More...
 

Private Attributes

ConverterMode m_mode
 

Detailed Description

Converter of SessionModel to/from json object with posibility to select one of convertion modes.

Definition at line 29 of file jsonmodelconverter.h.

Constructor & Destructor Documentation

◆ JsonModelConverter()

JsonModelConverter::JsonModelConverter ( ConverterMode  mode)

Definition at line 44 of file jsonmodelconverter.cpp.

44 : m_mode(mode) {}

◆ ~JsonModelConverter()

JsonModelConverter::~JsonModelConverter ( )
overridedefault

Member Function Documentation

◆ from_json()

void JsonModelConverter::from_json ( const QJsonObject &  json,
SessionModel model 
) const
overridevirtual

Reads json object and build the model.

Implements ModelView::JsonModelConverterInterface.

Definition at line 69 of file jsonmodelconverter.cpp.

70 {
71  if (!model.rootItem())
72  throw std::runtime_error("JsonModel::json_to_model() -> Error. Model is not initialized.");
73 
74  JsonItemFormatAssistant assistant;
75  if (!assistant.isSessionModel(json))
76  throw std::runtime_error("JsonModel::json_to_model() -> Error. Invalid json object.");
77 
79  != QString::fromStdString(model.modelType()))
80  throw std::runtime_error(
81  "JsonModel::json_to_model() -> Unexpected model type '" + model.modelType()
82  + "', json key '"
83  + json[JsonItemFormatAssistant::sessionModelKey].toString().toStdString() + "'");
84 
85  auto itemConverter = CreateConverter(model.factory(), m_mode);
86 
87  auto rebuild_root = [&json, &itemConverter](auto parent) {
88  for (const auto ref : json[JsonItemFormatAssistant::itemsKey].toArray()) {
89  auto item = itemConverter->from_json(ref.toObject());
90  parent->insertItem(item.release(), TagRow::append());
91  }
92  };
93  model.clear(rebuild_root);
94 }
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionModel(const QJsonObject &object) const
Returns true if given json object represents SessionModel.
std::string modelType() const
Returns model type.
const ItemFactoryInterface * factory() const
Returns item factory which can generate all items supported by this model.
SessionItem * rootItem() const
Returns root item of the model.
void clear(std::function< void(SessionItem *)> callback={})
Removes all items from the model.
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
Definition: PyUtils.cpp:24

References ModelView::TagRow::append(), ModelView::SessionModel::clear(), ModelView::SessionModel::factory(), ModelView::JsonItemFormatAssistant::isSessionModel(), ModelView::JsonItemFormatAssistant::itemsKey, m_mode, ModelView::SessionModel::modelType(), ModelView::SessionModel::rootItem(), ModelView::JsonItemFormatAssistant::sessionModelKey, and PyUtils::toString().

Referenced by TEST_F().

Here is the call graph for this function:

◆ to_json()

QJsonObject JsonModelConverter::to_json ( const SessionModel model) const
overridevirtual

Writes content of model into json.

Implements ModelView::JsonModelConverterInterface.

Definition at line 48 of file jsonmodelconverter.cpp.

49 {
50  QJsonObject result;
51 
52  if (!model.rootItem())
53  throw std::runtime_error("JsonModel::to_json() -> Error. Model is not initialized.");
54 
55  result[JsonItemFormatAssistant::sessionModelKey] = QString::fromStdString(model.modelType());
56 
57  QJsonArray itemArray;
58 
59  auto itemConverter = CreateConverter(model.factory(), m_mode);
60 
61  for (auto item : model.rootItem()->children())
62  itemArray.append(itemConverter->to_json(item));
63 
64  result[JsonItemFormatAssistant::itemsKey] = itemArray;
65 
66  return result;
67 }
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.

References ModelView::SessionItem::children(), ModelView::SessionModel::factory(), ModelView::JsonItemFormatAssistant::itemsKey, m_mode, ModelView::SessionModel::modelType(), ModelView::SessionModel::rootItem(), and ModelView::JsonItemFormatAssistant::sessionModelKey.

Referenced by TEST_F().

Here is the call graph for this function:

Member Data Documentation

◆ m_mode

ConverterMode ModelView::JsonModelConverter::m_mode
private

Definition at line 41 of file jsonmodelconverter.h.

Referenced by from_json(), and to_json().


The documentation for this class was generated from the following files: