GDAL

cpl_json_streaming_writer.h
1/******************************************************************************
2 *
3 * Project: CPL - Common Portability Library
4 * Purpose: JSon streaming writer
5 * Author: Even Rouault, even.rouault at spatialys.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
28
29#ifndef CPL_JSON_STREAMING_WRITER_H
30#define CPL_JSON_STREAMING_WRITER_H
31
34#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
35
36#include <cstdint>
37#include <vector>
38#include <string>
39#include "cpl_port.h"
40
41 class CPL_DLL CPLJSonStreamingWriter
42{
43 public:
44 typedef void (*SerializationFuncType)(const char *pszTxt, void *pUserData);
45
46 private:
47 CPLJSonStreamingWriter(const CPLJSonStreamingWriter &) = delete;
48 CPLJSonStreamingWriter &operator=(const CPLJSonStreamingWriter &) = delete;
49
50 std::string m_osStr{};
51 SerializationFuncType m_pfnSerializationFunc = nullptr;
52 void *m_pUserData = nullptr;
53 bool m_bPretty = true;
54 std::string m_osIndent = std::string(" ");
55 std::string m_osIndentAcc{};
56 int m_nLevel = 0;
57 bool m_bNewLineEnabled = true;
58
59 struct State
60 {
61 bool bIsObj = false;
62 bool bFirstChild = true;
63
64 explicit State(bool bIsObjIn) : bIsObj(bIsObjIn)
65 {
66 }
67 };
68
69 std::vector<State> m_states{};
70 bool m_bWaitForValue = false;
71
72 void Print(const std::string &text);
73 void IncIndent();
74 void DecIndent();
75 static std::string FormatString(const std::string &str);
76 void EmitCommaIfNeeded();
77
78 public:
79 CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc,
80 void *pUserData);
81 ~CPLJSonStreamingWriter();
82
83 void SetPrettyFormatting(bool bPretty)
84 {
85 m_bPretty = bPretty;
86 }
87
88 void SetIndentationSize(int nSpaces);
89
90 // cppcheck-suppress functionStatic
91 const std::string &GetString() const
92 {
93 return m_osStr;
94 }
95
96 void Add(const std::string &str);
97 void Add(const char *pszStr);
98 void Add(bool bVal);
99
100 void Add(int nVal)
101 {
102 Add(static_cast<std::int64_t>(nVal));
103 }
104
105 void Add(unsigned int nVal)
106 {
107 Add(static_cast<std::int64_t>(nVal));
108 }
109
110 void Add(std::int64_t nVal);
111 void Add(std::uint64_t nVal);
112 void Add(float fVal, int nPrecision = 9);
113 void Add(double dfVal, int nPrecision = 18);
114 void AddNull();
115
116 void StartObj();
117 void EndObj();
118 void AddObjKey(const std::string &key);
119
120 struct CPL_DLL ObjectContext
121 {
122 CPLJSonStreamingWriter &m_serializer;
123
124 ObjectContext(const ObjectContext &) = delete;
125 ObjectContext(ObjectContext &&) = default;
126
127 explicit inline ObjectContext(CPLJSonStreamingWriter &serializer)
128 : m_serializer(serializer)
129 {
130 m_serializer.StartObj();
131 }
132
133 ~ObjectContext()
134 {
135 m_serializer.EndObj();
136 }
137 };
138
139 inline ObjectContext MakeObjectContext()
140 {
141 return ObjectContext(*this);
142 }
143
144 void StartArray();
145 void EndArray();
146
147 struct CPL_DLL ArrayContext
148 {
149 CPLJSonStreamingWriter &m_serializer;
150 bool m_bForceSingleLine;
151 bool m_bNewLineEnabledBackup;
152
153 ArrayContext(const ArrayContext &) = delete;
154 ArrayContext(ArrayContext &&) = default;
155
156 inline explicit ArrayContext(CPLJSonStreamingWriter &serializer,
157 bool bForceSingleLine = false)
158 : m_serializer(serializer), m_bForceSingleLine(bForceSingleLine),
159 m_bNewLineEnabledBackup(serializer.GetNewLine())
160 {
161 if (m_bForceSingleLine)
162 serializer.SetNewline(false);
163 m_serializer.StartArray();
164 }
165
166 ~ArrayContext()
167 {
168 m_serializer.EndArray();
169 if (m_bForceSingleLine)
170 m_serializer.SetNewline(m_bNewLineEnabledBackup);
171 }
172 };
173
174 inline ArrayContext MakeArrayContext(bool bForceSingleLine = false)
175 {
176 return ArrayContext(*this, bForceSingleLine);
177 }
178
179 bool GetNewLine() const
180 {
181 return m_bNewLineEnabled;
182 }
183
184 void SetNewline(bool bEnabled)
185 {
186 m_bNewLineEnabled = bEnabled;
187 }
188};
189
190#endif // __cplusplus
191
194#endif // CPL_JSON_STREAMING_WRITER_H
cpl_port.h
Core portability definitions for CPL.