Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cargo-json.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Pawel Kubik (p.kubik@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
26 #ifndef CARGO_JSON_CARGO_JSON_HPP
27 #define CARGO_JSON_CARGO_JSON_HPP
28 
31 #include "cargo-json/fs-utils.hpp"
32 #include "logger/logger.hpp"
33 
34 namespace cargo {
35 
44 template <class Cargo>
45 void loadFromJsonString(const std::string& jsonString, Cargo& visitable)
46 {
47  static_assert(isVisitable<Cargo>::value, "Use CARGO_REGISTER macro");
48 
49  FromJsonVisitor visitor(jsonString);
50  visitable.accept(visitor);
51 }
52 
58 template <class Cargo>
59 std::string saveToJsonString(const Cargo& visitable)
60 {
61  static_assert(isVisitable<Cargo>::value, "Use CARGO_REGISTER macro");
62 
63  ToJsonVisitor visitor;
64  visitable.accept(visitor);
65  return visitor.toString();
66 }
67 
74 template <class Cargo>
75 void loadFromJsonFile(const std::string& filename, Cargo& visitable)
76 {
77  std::string content;
78  if (!fsutils::readFileContent(filename, content)) {
79  const std::string& msg = "Could not load " + filename;
80  LOGE(msg);
81  throw CargoException(msg);
82  }
83  try {
84  loadFromJsonString(content, visitable);
85  } catch (CargoException& e) {
86  const std::string& msg = "Error in " + filename + ": " + e.what();
87  LOGE(msg);
88  throw CargoException(msg);
89  }
90 }
91 
98 template <class Cargo>
99 void saveToJsonFile(const std::string& filename, const Cargo& visitable)
100 {
101  const std::string content = saveToJsonString(visitable);
102  if (!fsutils::saveFileContent(filename, content)) {
103  throw CargoException("Could not save " + filename);
104  }
105 }
106 
107 } // namespace cargo
108 
111 #endif // CARGO_JSON_CARGO_JSON_HPP
std::string saveToJsonString(const Cargo &visitable)
Creates a string representation of the visitable in json format.
Definition: cargo-json.hpp:59
Definition: from-json-visitor.hpp:43
JSON visitor.
JSON visitor.
void loadFromJsonString(const std::string &jsonString, Cargo &visitable)
Fills the visitable with data stored in the json string.
Definition: cargo-json.hpp:45
#define LOGE(MESSAGE)
Logging errors.
Definition: logger.hpp:140
bool readFileContent(const std::string &path, std::string &result)
Definition: fs-utils.cpp:34
bool saveFileContent(const std::string &path, const std::string &content)
Definition: fs-utils.cpp:59
Helper for compile-time checking against existance of template method 'accept'.
Definition: is-visitable.hpp:49
void saveToJsonFile(const std::string &filename, const Cargo &visitable)
Saves the visitable in a json file.
Definition: cargo-json.hpp:99
Base class for exceptions in libcargo.
Definition: exception.hpp:35
void loadFromJsonFile(const std::string &filename, Cargo &visitable)
Loads the visitable from a json file.
Definition: cargo-json.hpp:75
std::string toString() const
Definition: to-json-visitor.hpp:63
Definition: to-json-visitor.hpp:43
src/cargo/manager.hpp