1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 
16 #ifndef OHOS_JSON_UTILS_H
17 #define OHOS_JSON_UTILS_H
18 
19 #include <algorithm>
20 #include <iterator>
21 #include <nlohmann/json.hpp>
22 #include <string>
23 #include <vector>
24 
25 #include "avsession_info.h"
26 #include "avsession_errors.h"
27 
28 namespace OHOS::AVSession {
29 using json = nlohmann::json;
30 class JsonUtils {
31 public:
32     static int32_t GetJsonCapability(const std::vector<std::vector<int32_t>>& capability, std::string& jsonCapability);
33     static int32_t GetVectorCapability(const std::string& jsonCapability,
34                                        std::vector<std::vector<int32_t>>& vectorCapability);
35     static int32_t GetAllCapability(const std::string& sessionInfo, std::string& jsonCapability);
36 
37     static int32_t SetSessionBasicInfo(std::string& sessionInfo, const AVSessionBasicInfo& basicInfo);
38     static int32_t GetSessionBasicInfo(const std::string& sessionInfo, AVSessionBasicInfo& basicInfo);
39 
40     static int32_t SetSessionDescriptors(std::string& sessionInfo, const std::vector<AVSessionDescriptor>& descriptors);
41     static int32_t GetSessionDescriptors(const std::string& sessionInfo, std::vector<AVSessionDescriptor>& descriptors);
42 
43     static int32_t SetSessionDescriptor(std::string& sessionInfo, const AVSessionDescriptor& descriptor);
44     static int32_t GetSessionDescriptor(const std::string& sessionInfo, AVSessionDescriptor& descriptor);
45 
46 private:
47     static int32_t JsonToVector(json object, std::vector<int32_t>& out);
48     static int32_t ConvertSessionType(const std::string& typeString);
49     static std::string ConvertSessionType(int32_t type);
50     static bool IsString(const json& jsonObj, const std::string& key);
51     static bool IsInt32(const json& jsonObj, const std::string& key);
52     static bool IsBool(const json& jsonObj, const std::string& key);
53 };
54 } // namespace OHOS::AVSession
55 #endif // OHOS_JSON_UTILS_H
56