Android-cuttlefish cvd tool
json.h
Go to the documentation of this file.
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
26
27namespace cuttlefish {
28
29Result<Json::Value> ParseJson(std::string_view input);
30
31Result<Json::Value> LoadFromFile(SharedFD json_fd);
32Result<Json::Value> LoadFromFile(const std::string& path_to_file);
33
34template <typename T>
35Result<T> As(const Json::Value& v);
36
37template <>
38inline Result<int> As(const Json::Value& v) {
39 CF_EXPECT(v.isInt());
40 return v.asInt();
41}
42
43template <>
44inline Result<std::string> As(const Json::Value& v) {
45 CF_EXPECT(v.isString());
46 return v.asString();
47}
48
49template <>
50inline Result<bool> As(const Json::Value& v) {
51 CF_EXPECT(v.isBool());
52 return v.asBool();
53}
54
55template <>
56inline Result<Json::Value> As(const Json::Value& v) {
57 return v;
58}
59
60template <typename T>
61Result<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
72template <typename T>
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
82inline 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
Definition: expected.h:86
#define CF_EXPECT(...)
Definition: result.h:414
#define CF_EXPECTF(RESULT, MSG,...)
Definition: result.h:417
Definition: alloc_utils.cpp:23
Result< T > As(const Json::Value &v)
bool HasValue(const Json::Value &root, const std::vector< std::string > &selectors)
Definition: json.h:82
Result< Json::Value > ParseJson(std::string_view input)
Definition: json.cpp:26
Result< std::vector< T > > GetArrayValues(const Json::Value &array, const std::vector< std::string > &selectors)
Definition: json.h:73
Result< Json::Value > LoadFromFile(SharedFD json_fd)
Definition: json.cpp:37
Result< T > GetValue(const Json::Value &root, const std::vector< std::string > &selectors)
Definition: json.h:61