|
1 |
| -#pragma once |
2 |
| - |
3 |
| -#include <google/protobuf/util/json_util.h> |
4 |
| - |
5 |
| -#include <filesystem> |
6 |
| -#include <fstream> |
7 |
| -#include <string> |
8 |
| - |
9 |
| -namespace frame::proto |
10 |
| -{ |
11 |
| - |
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <google/protobuf/util/json_util.h> |
| 4 | + |
| 5 | +#include <filesystem> |
| 6 | +#include <fstream> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace frame::proto |
| 10 | +{ |
| 11 | + |
12 | 12 | template <typename T>
|
13 |
| -T LoadProtoFromJson(const std::string& json) |
14 |
| -{ |
15 |
| - T proto{}; |
16 |
| - google::protobuf::util::JsonParseOptions options{}; |
17 |
| - options.ignore_unknown_fields = false; |
18 |
| - auto status = |
19 |
| - google::protobuf::util::JsonStringToMessage(json, &proto, options); |
20 |
| - if (!status.ok()) |
21 |
| - { |
22 |
| - throw std::runtime_error( |
23 |
| - "Couldn't parse json status error: " + |
24 |
| - status.ToString()); |
25 |
| - } |
26 |
| - return proto; |
27 |
| -} |
28 |
| - |
29 |
| -template <typename T> |
30 |
| -T LoadProtoFromJsonFile(const std::filesystem::path& filename) |
31 |
| -{ |
32 |
| - // Empty case (no such file return an empty structure). |
33 |
| - if (filename.empty()) |
34 |
| - return T{}; |
35 |
| - // Try to open it. |
36 |
| - std::ifstream ifs(filename.string(), std::ios::in); |
37 |
| - if (!ifs.is_open()) |
38 |
| - { |
39 |
| - throw std::runtime_error("Couldn't open file: " + filename.string()); |
40 |
| - } |
41 |
| - std::string contents(std::istreambuf_iterator<char>(ifs), {}); |
42 |
| - return LoadProtoFromJson<T>(contents); |
43 |
| -} |
44 |
| - |
45 |
| -} // End namespace frame::proto. |
| 13 | +T LoadProtoFromJson(const std::string& json) |
| 14 | +{ |
| 15 | + T proto{}; |
| 16 | + google::protobuf::util::JsonParseOptions options{}; |
| 17 | + options.ignore_unknown_fields = false; |
| 18 | + auto status = |
| 19 | + google::protobuf::util::JsonStringToMessage(json, &proto, options); |
| 20 | + if (!status.ok()) |
| 21 | + { |
| 22 | + throw std::runtime_error( |
| 23 | + "Couldn't parse json status error: " + |
| 24 | + status.ToString()); |
| 25 | + } |
| 26 | + return proto; |
| 27 | +} |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +T LoadProtoFromJsonFile(const std::filesystem::path& filename) |
| 31 | +{ |
| 32 | + // Empty case (no such file return an empty structure). |
| 33 | + if (filename.empty()) |
| 34 | + return T{}; |
| 35 | + // Try to open it. |
| 36 | + std::ifstream ifs(filename.string(), std::ios::in); |
| 37 | + if (!ifs.is_open()) |
| 38 | + { |
| 39 | + throw std::runtime_error("Couldn't open file: " + filename.string()); |
| 40 | + } |
| 41 | + std::string contents(std::istreambuf_iterator<char>(ifs), {}); |
| 42 | + return LoadProtoFromJson<T>(contents); |
| 43 | +} |
| 44 | + |
| 45 | +} // End namespace frame::proto. |
0 commit comments