LCOV - code coverage report
Current view: top level - shared_model/backend/protobuf/impl - proto_block_json_converter.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 16 17 94.1 %
Date: 2019-03-07 14:46:43 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "backend/protobuf/proto_block_json_converter.hpp"
       7             : 
       8             : #include <google/protobuf/util/json_util.h>
       9             : #include <string>
      10             : 
      11             : #include "backend/protobuf/block.hpp"
      12             : 
      13             : using namespace shared_model;
      14             : using namespace shared_model::proto;
      15             : 
      16             : iroha::expected::Result<interface::types::JsonType, std::string>
      17             : ProtoBlockJsonConverter::serialize(const interface::Block &block) const
      18             :     noexcept {
      19        1293 :   const auto &proto_block_v1 = static_cast<const Block &>(block).getTransport();
      20        1293 :   iroha::protocol::Block proto_block;
      21        1293 :   *proto_block.mutable_block_v1() = proto_block_v1;
      22        1293 :   std::string result;
      23             :   auto status =
      24        1293 :       google::protobuf::util::MessageToJsonString(proto_block, &result);
      25             : 
      26        1293 :   if (not status.ok()) {
      27           0 :     return iroha::expected::makeError(status.error_message());
      28             :   }
      29        1293 :   return iroha::expected::makeValue(result);
      30        1293 : }
      31             : 
      32             : iroha::expected::Result<std::unique_ptr<interface::Block>, std::string>
      33             : ProtoBlockJsonConverter::deserialize(
      34             :     const interface::types::JsonType &json) const noexcept {
      35        1576 :   iroha::protocol::Block block;
      36        1571 :   auto status = google::protobuf::util::JsonStringToMessage(json, &block);
      37        1576 :   if (not status.ok()) {
      38           2 :     return iroha::expected::makeError(status.error_message());
      39             :   }
      40        1574 :   std::unique_ptr<interface::Block> result =
      41        1574 :       std::make_unique<Block>(std::move(block.block_v1()));
      42        1574 :   return iroha::expected::makeValue(std::move(result));
      43        1576 : }

Generated by: LCOV version 1.13