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
41class 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 struct State
59 {
60 bool bIsObj = false;
61 bool bFirstChild = true;
62 explicit State(bool bIsObjIn) : bIsObj(bIsObjIn)
63 {
64 }
65 };
66 std::vector<State> m_states{};
67 bool m_bWaitForValue = false;
68
69 void Print(const std::string &text);
70 void IncIndent();
71 void DecIndent();
72 static std::string FormatString(const std::string &str);
73 void EmitCommaIfNeeded();
74
75 public:
76 CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc,
77 void *pUserData);
78 ~CPLJSonStreamingWriter();
79
80 void SetPrettyFormatting(bool bPretty)
81 {
82 m_bPretty = bPretty;
83 }
84 void SetIndentationSize(int nSpaces);
85
86 // cppcheck-suppress functionStatic
87 const std::string &GetString() const
88 {
89 return m_osStr;
90 }
91
92 void Add(const std::string &str);
93 void Add(const char *pszStr);
94 void Add(bool bVal);
95 void Add(int nVal)
96 {
97 Add(static_cast<std::int64_t>(nVal));
98 }
99 void Add(unsigned int nVal)
100 {
101 Add(static_cast<std::int64_t>(nVal));
102 }
103 void Add(std::int64_t nVal);
104 void Add(std::uint64_t nVal);
105 void Add(float fVal, int nPrecision = 9);
106 void Add(double dfVal, int nPrecision = 18);
107 void AddNull();
108
109 void StartObj();
110 void EndObj();
111 void AddObjKey(const std::string &key);
112 struct CPL_DLL ObjectContext
113 {
114 CPLJSonStreamingWriter &m_serializer;
115
116 ObjectContext(const ObjectContext &) = delete;
117 ObjectContext(ObjectContext &&) = default;
118
119 explicit inline ObjectContext(CPLJSonStreamingWriter &serializer)
120 : m_serializer(serializer)
121 {
122 m_serializer.StartObj();
123 }
124 ~ObjectContext()
125 {
126 m_serializer.EndObj();
127 }
128 };
129 inline ObjectContext MakeObjectContext()
130 {
131 return ObjectContext(*this);
132 }
133
134 void StartArray();
135 void EndArray();
136 struct CPL_DLL ArrayContext
137 {
138 CPLJSonStreamingWriter &m_serializer;
139 bool m_bForceSingleLine;
140 bool m_bNewLineEnabledBackup;
141
142 ArrayContext(const ArrayContext &) = delete;
143 ArrayContext(ArrayContext &&) = default;
144
145 inline explicit ArrayContext(CPLJSonStreamingWriter &serializer,
146 bool bForceSingleLine = false)
147 : m_serializer(serializer), m_bForceSingleLine(bForceSingleLine),
148 m_bNewLineEnabledBackup(serializer.GetNewLine())
149 {
150 if (m_bForceSingleLine)
151 serializer.SetNewline(false);
152 m_serializer.StartArray();
153 }
154 ~ArrayContext()
155 {
156 m_serializer.EndArray();
157 if (m_bForceSingleLine)
158 m_serializer.SetNewline(m_bNewLineEnabledBackup);
159 }
160 };
161 inline ArrayContext MakeArrayContext(bool bForceSingleLine = false)
162 {
163 return ArrayContext(*this, bForceSingleLine);
164 }
165
166 bool GetNewLine() const
167 {
168 return m_bNewLineEnabled;
169 }
170 void SetNewline(bool bEnabled)
171 {
172 m_bNewLineEnabled = bEnabled;
173 }
174};
175
176#endif // __cplusplus
177
180#endif // CPL_JSON_STREAMING_WRITER_H
Core portability definitions for CPL.