1 /*
2  * Copyright (c) 2024 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 RENDER_SERVICE_PROFILER_JSONWRAPPER_H
17 #define RENDER_SERVICE_PROFILER_JSONWRAPPER_H
18 
19 #include <sstream>
20 
21 namespace OHOS::Rosen {
22 class JsonWriter {
23 public:
24     JsonWriter() = default;
25     ~JsonWriter() = default;
26 
PushObject()27     void PushObject()
28     {
29         stream_ << "{";
30     }
31 
PopObject()32     void PopObject()
33     {
34         char last;
35         stream_.seekg(-1, std::ios::end);
36         stream_ >> last;
37         if (last == ',') {
38             stream_.seekp(-1, std::ios::end);
39         }
40         stream_ << "},";
41     }
42 
PushArray()43     void PushArray()
44     {
45         stream_ << "[";
46     }
47 
PopArray()48     void PopArray()
49     {
50         char last;
51         stream_.seekg(-1, std::ios::end);
52         stream_ >> last;
53         if (last == ',') {
54             stream_.seekp(-1, std::ios::end);
55         }
56         stream_ << "],";
57     }
58 
59     template<typename T>
Append(const T & value)60     void Append(const T& value)
61     {
62         Write(value);
63         stream_ << ",";
64     }
65 
66     JsonWriter& operator[](const std::string& key)
67     {
68         stream_ << "\"" << key << "\":";
69         return *this;
70     }
71 
72     template<typename T>
73     JsonWriter& operator=(const T& value)
74     {
75         Write(value);
76         stream_ << ",";
77         return *this;
78     }
79 
80     template<typename T>
81     JsonWriter& operator=(std::initializer_list<T>&& values)
82     {
83         PushArray();
84         for (const auto& value : values) {
85             Write(value);
86             stream_ << ",";
87         }
88         PopArray();
89         return *this;
90     }
91 
GetDumpString()92     std::string GetDumpString()
93     {
94         char last;
95         stream_.seekg(-1, std::ios::end);
96         stream_ >> last;
97         if (last == ',') {
98             stream_.seekp(-1, std::ios::end);
99             stream_ << '\0';
100         }
101         return stream_.str();
102     }
103 
104 private:
105     template<typename T>
Write(const T & value)106     void Write(const T& value)
107     {
108         stream_ << value;
109     }
110 
111     template<>
Write(const float & value)112     void Write(const float& value)
113     {
114         if (std::isinf(value) || std::isnan(value)) {
115             stream_ << "null";
116         } else {
117             stream_ << value;
118         }
119     }
120 
121     template<>
Write(const std::string & value)122     void Write(const std::string& value)
123     {
124         stream_ << "\"" << value << "\"";
125     }
126 
127 private:
128     std::stringstream stream_;
129 };
130 } // namespace OHOS::Rosen
131 
132 #endif // RENDER_SERVICE_PROFILER_JSONWRAPPER_H