• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 JSON_UTILS_H
17 #define JSON_UTILS_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string>
24 #include "cJSON.h"
25 
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 typedef cJSON CJson;
30 typedef std::unique_ptr<CJson, std::function<void(CJson* ptr)>> CJsonUnique;
31 
32 /* NO Need to call FreeJson to free the returned pointer when it's no longer in use. */
33 CJsonUnique CreateJsonFromString(const std::string& jsonStr);
34 /* NO Need to call FreeJson to free the returned pointer when it's no longer in use. */
35 CJsonUnique CreateJson(void);
36 /* NO Need to call FreeJson to free the returned pointer when it's no longer in use. */
37 CJsonUnique CreateJsonArray(void);
38 CJsonUnique CreateJsonString(const std::string& value);
39 void FreeJson(CJson* jsonObj);
40 
41 /* NO Need to call FreeJsonString to free the returned pointer when it's no longer in use. */
42 std::string PackJsonToString(const CJson* jsonObj);
43 std::string PackJsonToString(const CJsonUnique& jsonObj);
44 void FreeJsonString(char* jsonStr);
45 std::string JsonToStringFormatted(const CJson* jsonObj, uint32_t level = 0);
46 
47 /*
48  * Can't release the returned pointer, otherwise, an exception may occur.
49  * It refers to the parent object(param--jsonObj)'s memory.
50  * It will be recycled along with jsonObj when jsonObj is released.
51  */
52 CJson* GetObjFromJson(const CJson* jsonObj, const std::string& key);
53 CJson* GetObjFromJson(CJsonUnique& jsonObj, const std::string& key);
54 CJson* GetArrayFromJson(const CJson* jsonObj, const std::string& key);
55 CJson* GetArrayFromJson(CJsonUnique& jsonObj, const std::string& key);
56 bool GetArrayFromJson(const CJson* jsonObj, const std::string& key, std::vector<std::string>& out);
57 
58 /*
59 * Return a copy of string in jsonObj in std::string
60 */
61 bool GetStringFromJson(const CJson* jsonObj, const std::string& key, std::string& out);
62 
63 bool GetIntFromJson(const CJson* jsonObj, const std::string& key, int32_t& value);
64 bool GetIntFromJson(const CJsonUnique& jsonObj, const std::string& key, int32_t& value);
65 bool GetUnsignedIntFromJson(const CJson* jsonObj, const std::string& key, uint32_t& value);
66 bool GetUnsignedIntFromJson(const CJsonUnique& jsonObj, const std::string& key, uint32_t& value);
67 bool GetBoolFromJson(const CJson* jsonObj, const std::string& key, bool& value);
68 bool GetBoolFromJson(const CJsonUnique& jsonObj, const std::string& key, bool& value);
69 
70 bool AddObjToJson(CJson* jsonObj, const std::string& key, const CJson* childObj);
71 bool AddObjToJson(CJsonUnique& jsonObj, const std::string& key, CJsonUnique& childObj);
72 bool AddObjToArray(CJson* jsonArr, CJson* item);
73 bool AddObjToArray(CJsonUnique& jsonArr, CJsonUnique& item);
74 bool AddStringToJson(CJson* jsonObj, const std::string& key, const std::string& value);
75 bool AddStringToJson(CJsonUnique& jsonObj, const std::string& key, const std::string& value);
76 bool AddBoolToJson(CJson* jsonObj, const std::string& key, const bool value);
77 bool AddBoolToJson(CJsonUnique& jsonObj, const std::string& key, const bool value);
78 bool AddIntToJson(CJson* jsonObj, const std::string& key, const int32_t value);
79 bool AddIntToJson(CJsonUnique& jsonObj, const std::string& key, const int32_t value);
80 bool AddUnsignedIntToJson(CJson* jsonObj, const std::string& key, const uint32_t value);
81 bool AddUnsignedIntToJson(CJsonUnique& jsonObj, const std::string& key, const uint32_t value);
82 bool AddInt64ToJson(CJson* jsonObj, const std::string& key, const int64_t value);
83 bool AddInt64ToJson(CJsonUnique& jsonObj, const std::string& key, const int64_t value);
84 }  // namespace AccessToken
85 }  // namespace Security
86 }  // namespace OHOS
87 #endif
88