diff --git a/parser/common/convert/pb2json.cc b/parser/common/convert/pb2json.cc index af13ed2..4a0ab94 100644 --- a/parser/common/convert/pb2json.cc +++ b/parser/common/convert/pb2json.cc @@ -18,6 +18,7 @@ // Description: This imply file for protobuf message and json interconversion #include "common/convert/pb2json.h" +#include #include #include #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; } diff --git a/tests/ut/parser/CMakeLists.txt b/tests/ut/parser/CMakeLists.txt index b93e231..65804fc 100644 --- a/tests/ut/parser/CMakeLists.txt +++ b/tests/ut/parser/CMakeLists.txt @@ -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 ) diff --git a/tests/ut/parser/testcase/onnx_parser_testcase/message2operator_unittest.cc b/tests/ut/parser/testcase/onnx_parser_testcase/message2operator_unittest.cc index 39e1480..59ae9b1 100644 --- a/tests/ut/parser/testcase/onnx_parser_testcase/message2operator_unittest.cc +++ b/tests/ut/parser/testcase/onnx_parser_testcase/message2operator_unittest.cc @@ -19,6 +19,7 @@ #include #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{}, json, true); +} } // namespace ge \ No newline at end of file