Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
FieldMap.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/FieldMap.cpp -------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
16 
17 namespace serialbox {
18 
19 json::json FieldMap::toJSON() const {
20  json::json jsonNode;
21 
22  if(empty())
23  return jsonNode;
24 
25  for(const_iterator it = this->begin(), end = this->end(); it != end; ++it)
26  jsonNode[it->first] = it->second->toJSON();
27 
28  return jsonNode;
29 }
30 
31 void FieldMap::fromJSON(const json::json& jsonNode) {
32  this->clear();
33 
34  if(jsonNode.is_null() || jsonNode.empty())
35  return;
36 
37  for(auto it = jsonNode.begin(), end = jsonNode.end(); it != end; ++it) {
38  try {
39  insert(it.key(), it.value());
40  } catch(Exception& e) {
41  throw Exception("cannot insert node '%s' in FieldMap: JSON node ill-formed: %s", it.key(),
42  e.what());
43  }
44  }
45 }
46 
47 std::ostream& operator<<(std::ostream& stream, const FieldMap& s) {
48  return (stream << "FieldMap = " << s.toJSON().dump(4));
49 }
50 
51 } // namespace serialbox
map_type::const_iterator const_iterator
A forward iterator to const value_type
Definition: FieldMap.h:50
iterator begin() noexcept
Returns an iterator pointing to the first element in the FieldMap.
Definition: FieldMap.h:199
bool empty() const noexcept
Returns a bool value indicating whether the FieldMap is empty.
Definition: FieldMap.h:192
Namespace of the serialbox library.
Definition: Archive.h:25
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
Definition: FieldMap.cpp:31
bool insert(StringType &&key, Args &&... args)
Insert a new field in the map.
Definition: FieldMap.h:183
iterator end() noexcept
Returns an iterator pointing to the past-the-end element in the FieldMap.
Definition: FieldMap.h:203
void clear() noexcept
All the elements in the FieldMap are dropped: their destructors are called, and they are removed from...
Definition: FieldMap.h:196
json::json toJSON() const
Convert to JSON.
Definition: FieldMap.cpp:19
Exception class which stores a human-readable error description.
Definition: Exception.h:30
Hash-map to query the meta-information of the registered fields.
Definition: FieldMap.h:28
friend std::ostream & operator<<(std::ostream &stream, const FieldMap &s)
Convert to stream.
Definition: FieldMap.cpp:47