1 /*
2 * Copyright (C) 2021 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
17 #include <pixelstats/JsonConfigUtils.h>
18
19 #include <fstream>
20 #include <iostream>
21
22 namespace android {
23 namespace hardware {
24 namespace google {
25 namespace pixel {
26
27 // Helper function to read string vectors from JSON
readStringVectorFromJson(const Json::Value & jsonArr)28 std::vector<std::string> readStringVectorFromJson(const Json::Value &jsonArr) {
29 std::vector<std::string> vec;
30 if (jsonArr.isArray()) { // Check if jsonArr is an array
31 for (unsigned int i = 0; i < jsonArr.size(); ++i) {
32 vec.push_back(jsonArr[i].asString());
33 }
34 }
35 return vec;
36 }
37
38 // Helper function to read pairs of strings from JSON
39 std::vector<std::pair<std::string, std::string>>
readStringPairVectorFromJson(const Json::Value & jsonArr)40 readStringPairVectorFromJson(const Json::Value &jsonArr) {
41 std::vector<std::pair<std::string, std::string>> vec;
42 if (jsonArr.isArray()) { // Check if jsonArr is an array
43 for (unsigned int i = 0; i < jsonArr.size(); ++i) {
44 const Json::Value& innerArr = jsonArr[i];
45 if (innerArr.isArray() && innerArr.size() == 2) { // Check if inner array is valid
46 vec.push_back({innerArr[0].asString(), innerArr[1].asString()});
47 }
48 }
49 }
50 return vec;
51 }
52
getCStringOrDefault(const Json::Value configData,const std::string & key)53 std::string getCStringOrDefault(const Json::Value configData, const std::string& key) {
54 if (configData.isMember(key)) {
55 return configData[key].asString();
56 } else {
57 return "";
58 }
59 }
60
getIntOrDefault(const Json::Value configData,const std::string & key)61 int getIntOrDefault(const Json::Value configData, const std::string& key) {
62 if (configData.isMember(key) && configData[key].isInt()) {
63 return configData[key].asInt();
64 } else {
65 return 0;
66 }
67 }
68
69 } // namespace pixel
70 } // namespace google
71 } // namespace hardware
72 } // namespace android
73