• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/platform/human_readable_json.h"
17 #include "tensorflow/core/lib/core/errors.h"
18 #include "tensorflow/core/lib/core/status.h"
19 #include "tensorflow/core/lib/strings/strcat.h"
20 
21 namespace tensorflow {
22 
ProtoToHumanReadableJson(const protobuf::Message & proto,string * result)23 Status ProtoToHumanReadableJson(const protobuf::Message& proto,
24                                 string* result) {
25 #ifdef TENSORFLOW_LITE_PROTOS
26   *result = "[human readable output not available on Android]";
27   return Status::OK();
28 #else
29   result->clear();
30 
31   auto status = protobuf::util::MessageToJsonString(proto, result);
32   if (!status.ok()) {
33     // Convert error_msg google::protobuf::StringPiece to
34     // tensorflow::StringPiece.
35     auto error_msg = status.error_message();
36     return errors::Internal(
37         strings::StrCat("Could not convert proto to JSON string: ",
38                         StringPiece(error_msg.data(), error_msg.length())));
39   }
40   return Status::OK();
41 #endif
42 }
43 
HumanReadableJsonToProto(const string & str,protobuf::Message * proto)44 Status HumanReadableJsonToProto(const string& str, protobuf::Message* proto) {
45 #ifdef TENSORFLOW_LITE_PROTOS
46   return errors::Internal("Cannot parse JSON protos on Android");
47 #else
48   proto->Clear();
49   auto status = protobuf::util::JsonStringToMessage(str, proto);
50   if (!status.ok()) {
51     // Convert error_msg google::protobuf::StringPiece to
52     // tensorflow::StringPiece.
53     auto error_msg = status.error_message();
54     return errors::Internal(
55         strings::StrCat("Could not convert JSON string to proto: ",
56                         StringPiece(error_msg.data(), error_msg.length())));
57   }
58   return Status::OK();
59 #endif
60 }
61 
62 }  // namespace tensorflow
63