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