• 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 <string>
20 #include <map>
21 
22 class Json {
23 public:
24     // 代理类,用于设置值
25     class JsonValueProxy {
26     public:
27         JsonValueProxy(Json& parent, const std::string& key);
28         void operator=(const float value);
29         void operator=(const char* value);
30         void operator=(const std::string& value);
31         void operator=(const Json& value);
32 
33     private:
34         Json& parent_;
35         std::string key_;
36     };
37 
38     // 重载operator[]以返回代理对象
39     JsonValueProxy operator[](const std::string& key);
40 
41     // 设置浮点值
42     void Set(const std::string& key, float value);
43     // 设置字符串值
44     void Set(const std::string& key, const std::string& value);
45     // 设置const char*值
46     void Set(const std::string& key, const char* value);
47     // 设置json值
48     void Set(const std::string& key, const Json& value);
49 
50     // 将json对象转成字符串
51     std::string Dump() const;
52 
53     // 解析json字符串获取json格式的key和value
54     static std::map<std::string, std::string> Parse(const std::string& jsonStr);
55 
56 private:
57     std::map<std::string, std::string> values_;
58 };
59 
60 #endif // JSON_UTILS_H
61