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 #include "json_utils.h"
17 #include <iostream>
18 #include <sstream>
19 #include <iomanip>
20
21 // 表示小数点后保留的位数
22 constexpr int PRECISION = 2;
23 // 构造函数
JsonValueProxy(Json & parent,const std::string & key)24 Json::JsonValueProxy::JsonValueProxy(Json& parent, const std::string& key) : parent_(parent), key_(key) {}
25
26 // 设置浮点值
operator =(const float value)27 void Json::JsonValueProxy::operator=(const float value)
28 {
29 parent_.Set(key_, value);
30 }
31
32 // 设置const char*值
operator =(const char * value)33 void Json::JsonValueProxy::operator=(const char* value)
34 {
35 parent_.Set(key_, value);
36 }
37
38 // 设置字符串值
operator =(const std::string & value)39 void Json::JsonValueProxy::operator=(const std::string& value)
40 {
41 parent_.Set(key_, value);
42 }
43
44 // 设置json值
operator =(const Json & value)45 void Json::JsonValueProxy::operator=(const Json& value)
46 {
47 parent_.Set(key_, value);
48 }
49
50 // 重载operator[]以返回代理对象
operator [](const std::string & key)51 Json::JsonValueProxy Json::operator[](const std::string& key)
52 {
53 return JsonValueProxy(*this, key);
54 }
55
56 // 设置浮点值
Set(const std::string & key,float value)57 void Json::Set(const std::string& key, float value)
58 {
59 std::ostringstream oss;
60 // 将浮点数 value 转换为字符串时,保留小数点后两位
61 oss << std::fixed << std::setprecision(PRECISION) << value;
62 values_[key] = oss.str();
63 }
64
65 // 设置字符串值
Set(const std::string & key,const std::string & value)66 void Json::Set(const std::string& key, const std::string& value)
67 {
68 values_[key] = "\"" + value + "\"";
69 }
70
71 // 设置const char*值
Set(const std::string & key,const char * value)72 void Json::Set(const std::string& key, const char* value)
73 {
74 values_[key] = "\"" + std::string(value) + "\"";
75 }
76
77 // 设置json值
Set(const std::string & key,const Json & value)78 void Json::Set(const std::string& key, const Json& value)
79 {
80 values_[key] = value.Dump();
81 }
82
83 // 将json对象转成字符串
Dump() const84 std::string Json::Dump() const
85 {
86 std::ostringstream oss;
87 oss << "{";
88 bool first = true;
89 for (const auto& pair : values_) {
90 if (!first) {
91 oss << ", ";
92 }
93 oss << "\"" << pair.first << "\": " << pair.second;
94 first = false;
95 }
96 oss << "}";
97 return oss.str();
98 }
99
100 // 解析json字符串获取json格式的key和value
Parse(const std::string & jsonStr)101 std::map<std::string, std::string> Json::Parse(const std::string& jsonStr)
102 {
103 std::map<std::string, std::string> result;
104 std::string key;
105 std::string value;
106 bool inKey = false;
107 bool inValue = false;
108 for (size_t i = 0; i < jsonStr.size(); ++i) {
109 char c = jsonStr[i];
110 if (c == '"') {
111 if (inKey) {
112 inKey = false;
113 } else if (inValue) {
114 inValue = false;
115 result[key] = value;
116 key.clear();
117 value.clear();
118 } else {
119 inKey = true;
120 }
121 } else if (c == ':') {
122 inKey = false;
123 inValue = true;
124 } else if (c == ',') {
125 inValue = false;
126 } else if (inKey) {
127 key += c;
128 } else if (inValue) {
129 value += c;
130 }
131 }
132 return result;
133 }