Browse Source

!361 bugfix for json

Merge pull request !361 from yangyongqiang/master
pull/364/head
i-robot Gitee 4 years ago
parent
commit
2ced438587
3 changed files with 21 additions and 3 deletions
  1. +6
    -2
      parser/common/convert/pb2json.cc
  2. +1
    -1
      tests/ut/parser/CMakeLists.txt
  3. +14
    -0
      tests/ut/parser/testcase/onnx_parser_testcase/message2operator_unittest.cc

+ 6
- 2
parser/common/convert/pb2json.cc View File

@@ -18,6 +18,7 @@
// Description: This imply file for protobuf message and json interconversion

#include "common/convert/pb2json.h"
#include <google/protobuf/text_format.h>
#include <set>
#include <string>
#include "securec.h"
@@ -129,8 +130,11 @@ void Pb2Json::OneField2Json(const ProtobufMsg &message, const ProtobufFieldDescr

case ProtobufFieldDescriptor::TYPE_BYTES: {
string field_name = field->name();
string type_bytes = reflection->GetString(message, field);
json[field_name] = TypeBytes2String(field_name, type_bytes);
std::string scratch;
std::string value = reflection->GetStringReference(message, field, &scratch);
std::string cescape_value = google::protobuf::CEscape(value);
GELOGD("After cescape data:%s", cescape_value.c_str());
json[field_name] = cescape_value;
break;
}



+ 1
- 1
tests/ut/parser/CMakeLists.txt View File

@@ -356,5 +356,5 @@ target_link_libraries(ut_parser
ut_parser_proto
-Wl,--whole-archive ut_parser_common -Wl,--no-whole-archive
ut_parser_graph ut_parser_register error_manager_stub mmpa_stub attr_util_stub
gtest gtest_main slog_stub ascend_protobuf c_sec -lrt -ldl -lgcov
gtest gtest_main slog_stub ascend_protobuf c_sec json -lrt -ldl -lgcov
)

+ 14
- 0
tests/ut/parser/testcase/onnx_parser_testcase/message2operator_unittest.cc View File

@@ -19,6 +19,7 @@
#include <gtest/gtest.h>

#include "proto/onnx/ge_onnx.pb.h"
#include "parser/common/convert/pb2json.h"

namespace ge {
class UtestMessage2Operator : public testing::Test {
@@ -55,4 +56,17 @@ TEST_F(UtestMessage2Operator, message_to_operator_fail) {
ret = Message2Operator::ParseOperatorAttrs(attribute, 1, op_src);
EXPECT_EQ(ret, FAILED);
}

TEST_F(UtestMessage2Operator, pb2json_one_field_json) {
ge::onnx::NodeProto input_node;
ge::onnx::AttributeProto *attribute = input_node.add_attribute();
attribute->set_name("attribute");
attribute->set_type(onnx::AttributeProto::AttributeType(1));
ge::onnx::TensorProto *attribute_tensor = attribute->mutable_t();
attribute_tensor->set_data_type(1);
attribute_tensor->add_dims(4);
attribute_tensor->set_raw_data("\007");
Json json;
ge::Pb2Json::Message2Json(input_node, std::set<std::string>{}, json, true);
}
} // namespace ge

Loading…
Cancel
Save