• 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 void FreeJson(CJson* jsonObj);
39 
40 /* NO Need to call FreeJsonString to free the returned pointer when it's no longer in use. */
41 std::string PackJsonToString(const CJson* jsonObj);
42 std::string PackJsonToString(const CJsonUnique& jsonObj);
43 void FreeJsonString(char* jsonStr);
44 
45 /*
46  * Can't release the returned pointer, otherwise, an exception may occur.
47  * It refers to the parent object(param--jsonObj)'s memory.
48  * It will be recycled along with jsonObj when jsonObj is released.
49  */
50 CJson* GetObjFromJson(const CJson* jsonObj, const std::string& key);
51 CJson* GetObjFromJson(CJsonUnique& jsonObj, const std::string& key);
52 CJson* GetArrayFromJson(const CJson* jsonObj, const std::string& key);
53 CJson* GetArrayFromJson(CJsonUnique& jsonObj, const std::string& key);
54 
55 /*
56 * Return a copy of string in jsonObj in std::string
57 */
58 bool GetStringFromJson(const CJson *jsonObj, const std::string& key, std::string& out);
59 
60 bool GetIntFromJson(const CJson* jsonObj, const std::string& key, int32_t& value);
61 bool GetIntFromJson(const CJsonUnique& jsonObj, const std::string& key, int32_t& value);
62 bool GetUnsignedIntFromJson(const CJson* jsonObj, const std::string& key, uint32_t& value);
63 bool GetUnsignedIntFromJson(const CJsonUnique& jsonObj, const std::string& key, uint32_t& value);
64 bool GetBoolFromJson(const CJson* jsonObj, const std::string& key, bool& value);
65 bool GetBoolFromJson(const CJsonUnique& jsonObj, const std::string& key, bool& value);
66 
67 bool AddObjToJson(CJson* jsonObj, const std::string& key, const CJson* childObj);
68 bool AddObjToJson(CJsonUnique& jsonObj, const std::string& key, CJsonUnique& childObj);
69 bool AddObjToArray(CJson* jsonArr, CJson* item);
70 bool AddObjToArray(CJsonUnique& jsonArr, CJsonUnique& item);
71 bool AddStringToJson(CJson* jsonObj, const std::string& key, const std::string& value);
72 bool AddStringToJson(CJsonUnique& jsonObj, const std::string& key, const std::string& value);
73 bool AddBoolToJson(CJson* jsonObj, const std::string& key, const bool value);
74 bool AddBoolToJson(CJsonUnique& jsonObj, const std::string& key, const bool value);
75 bool AddIntToJson(CJson* jsonObj, const std::string& key, const int value);
76 bool AddIntToJson(CJsonUnique& jsonObj, const std::string& key, const int value);
77 bool AddUnsignedIntToJson(CJson* jsonObj, const std::string& key, const uint32_t value);
78 bool AddUnsignedIntToJson(CJsonUnique& jsonObj, const std::string& key, const uint32_t value);
79 }  // namespace AccessToken
80 }  // namespace Security
81 }  // namespace OHOS
82 #endif
83