• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2022 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #pragma once
17 
18 #include <string>
19 #include <string_view>
20 #include <vector>
21 
22 #include <json/json.h>
23 
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/utils/result.h"
26 
27 namespace cuttlefish {
28 
29 Result<Json::Value> ParseJson(std::string_view input);
30 
31 Result<Json::Value> LoadFromFile(SharedFD json_fd);
32 Result<Json::Value> LoadFromFile(const std::string& path_to_file);
33 
34 template <typename T>
35 Result<T> As(const Json::Value& v);
36 
37 template <>
As(const Json::Value & v)38 inline Result<int> As(const Json::Value& v) {
39   CF_EXPECT(v.isInt());
40   return v.asInt();
41 }
42 
43 template <>
As(const Json::Value & v)44 inline Result<std::string> As(const Json::Value& v) {
45   CF_EXPECT(v.isString());
46   return v.asString();
47 }
48 
49 template <>
As(const Json::Value & v)50 inline Result<bool> As(const Json::Value& v) {
51   CF_EXPECT(v.isBool());
52   return v.asBool();
53 }
54 
55 template <>
As(const Json::Value & v)56 inline Result<Json::Value> As(const Json::Value& v) {
57   return v;
58 }
59 
60 template <typename T>
GetValue(const Json::Value & root,const std::vector<std::string> & selectors)61 Result<T> GetValue(const Json::Value& root,
62                    const std::vector<std::string>& selectors) {
63   const Json::Value* traversal = &root;
64   for (const auto& selector : selectors) {
65     CF_EXPECTF(traversal->isMember(selector),
66                "JSON selector \"{}\" does not exist", selector);
67     traversal = &(*traversal)[selector];
68   }
69   return CF_EXPECT(As<T>(*traversal));
70 }
71 
72 template <typename T>
GetArrayValues(const Json::Value & array,const std::vector<std::string> & selectors)73 Result<std::vector<T>> GetArrayValues(
74     const Json::Value& array, const std::vector<std::string>& selectors) {
75   std::vector<T> result;
76   for (const auto& element : array) {
77     result.emplace_back(CF_EXPECT(GetValue<T>(element, selectors)));
78   }
79   return result;
80 }
81 
HasValue(const Json::Value & root,const std::vector<std::string> & selectors)82 inline bool HasValue(const Json::Value& root,
83                      const std::vector<std::string>& selectors) {
84   const Json::Value* traversal = &root;
85   for (const auto& selector : selectors) {
86     if (!traversal->isMember(selector)) {
87       return false;
88     }
89     traversal = &(*traversal)[selector];
90   }
91   return true;
92 }
93 
94 }  // namespace cuttlefish
95