hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
json_proto_converter.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_JSON_PROTO_CONVERTER_HPP
7 #define IROHA_JSON_PROTO_CONVERTER_HPP
8 
9 #include <google/protobuf/util/json_util.h>
10 #include <string>
12 #include "commands.pb.h"
13 
14 namespace shared_model {
15  namespace converters {
16  namespace protobuf {
17 
24  template <typename T>
25  std::string modelToJson(const T &message) {
26  std::string result;
27  google::protobuf::util::MessageToJsonString(message.getTransport(),
28  &result);
29  return result;
30  }
31 
39  template <typename T>
40  boost::optional<T> jsonToProto(std::string json) {
41  T result;
42  auto status =
43  google::protobuf::util::JsonStringToMessage(json, &result);
44  if (status.ok()) {
45  return result;
46  }
47  return boost::none;
48  }
49 
57  template <typename T>
58  boost::optional<T> jsonToModel(std::string json) {
59  auto tx = jsonToProto<typename T::TransportType>(json);
60  if (tx) {
61  return T(std::move(tx.value()));
62  }
63  return boost::none;
64  }
65 
66  } // namespace protobuf
67  } // namespace converters
68 } // namespace shared_model
69 
70 #endif // IROHA_JSON_PROTO_CONVERTER_HPP
boost::optional< T > jsonToModel(std::string json)
Definition: json_proto_converter.hpp:58
std::string modelToJson(const T &message)
Definition: json_proto_converter.hpp:25
string(REPLACE".""/"CMAKE_SWIG_OUTDIR ${SWIG_JAVA_PKG}) else() set(CMAKE_SWIG_FLAGS"") endif() myswig_add_library(irohajava LANGUAGE java SOURCES bindings.i) unset(CMAKE_SWIG_FLAGS) swig_link_libraries(irohajava $
Definition: CMakeLists.txt:54
boost::optional< T > jsonToProto(std::string json)
Definition: json_proto_converter.hpp:40
Definition: command_executor.hpp:12