1 /* Copyright 2020 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 // Simple program to convert from JSON to binary flatbuffers for given schema.
16 //
17 // Used for creating the binary version of a compatibility list.
18 //
19 // The flatc command line is not available in all build environments.
20 #include <fstream>
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 
25 #include "flatbuffers/flatbuffers.h"  // from @flatbuffers
26 #include "flatbuffers/idl.h"  // from @flatbuffers
27 #include "flatbuffers/reflection.h"  // from @flatbuffers
28 #include "flatbuffers/reflection_generated.h"  // from @flatbuffers
29 #include "flatbuffers/util.h"  // from @flatbuffers
30 #include "tensorflow/lite/tools/command_line_flags.h"
31 
main(int argc,char ** argv)32 int main(int argc, char** argv) {
33   std::string json_path, fbs_path, fb_path;
34   std::vector<tflite::Flag> flags = {
35       tflite::Flag::CreateFlag("json_input", &json_path,
36                                "Path to input json file."),
37       tflite::Flag::CreateFlag("fbs", &fbs_path,
38                                "Path to flatbuffer schema to use."),
39       tflite::Flag::CreateFlag("fb_output", &fb_path,
40                                "Path to a output binary flatbuffer."),
41   };
42   const bool parse_result =
43       tflite::Flags::Parse(&argc, const_cast<const char**>(argv), flags);
44   if (!parse_result || json_path.empty() || fbs_path.empty() ||
45       fb_path.empty()) {
46     std::cerr << tflite::Flags::Usage(argv[0], flags);
47     return 1;
48   }
49   std::string json_contents;
50   if (!flatbuffers::LoadFile(json_path.c_str(), false, &json_contents)) {
51     std::cerr << "Unable to load file " << json_path << std::endl;
52     return 2;
53   }
54   std::string fbs_contents;
55   if (!flatbuffers::LoadFile(fbs_path.c_str(), false, &fbs_contents)) {
56     std::cerr << "Unable to load file " << fbs_path << std::endl;
57     return 3;
58   }
59   const char* include_directories[] = {nullptr};
60   flatbuffers::Parser schema_parser;
61   if (!schema_parser.Parse(fbs_contents.c_str(), include_directories)) {
62     std::cerr << "Unable to parse schema " << schema_parser.error_ << std::endl;
63     return 4;
64   }
65   schema_parser.Serialize();
66   auto schema =
67       reflection::GetSchema(schema_parser.builder_.GetBufferPointer());
68   auto root_table = schema->root_table();
69   flatbuffers::Parser parser;
70   parser.Deserialize(schema_parser.builder_.GetBufferPointer(),
71                      schema_parser.builder_.GetSize());
72 
73   if (!parser.Parse(json_contents.c_str(), include_directories,
74                     json_path.c_str())) {
75     std::cerr << "Unable to parse json " << parser.error_ << std::endl;
76     return 5;
77   }
78 
79   // Use CopyTable() to deduplicate the strings.
80   const uint8_t* buffer = parser.builder_.GetBufferPointer();
81   flatbuffers::FlatBufferBuilder fbb;
82   auto root_offset = flatbuffers::CopyTable(
83       fbb, *schema, *root_table, *flatbuffers::GetAnyRoot(buffer), true);
84   fbb.Finish(root_offset);
85   std::string binary(reinterpret_cast<const char*>(fbb.GetBufferPointer()),
86                      fbb.GetSize());
87   std::ofstream output;
88   output.open(fb_path);
89   output << binary;
90   output.close();
91   return 0;
92 }
93