/src/PROJ/src/proj_json_streaming_writer.hpp
Line | Count | Source (jump to first uncovered line) |
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 PROJ_JSON_STREAMING_WRITER_H |
30 | | #define PROJ_JSON_STREAMING_WRITER_H |
31 | | |
32 | | /*! @cond Doxygen_Suppress */ |
33 | | |
34 | | #include <cstdint> |
35 | | #include <string> |
36 | | #include <vector> |
37 | | |
38 | | #define CPL_DLL |
39 | | |
40 | | #include "proj/util.hpp" |
41 | | NS_PROJ_START |
42 | | |
43 | | typedef std::int64_t GIntBig; |
44 | | typedef std::uint64_t GUInt64; |
45 | | |
46 | | class CPL_DLL CPLJSonStreamingWriter { |
47 | | public: |
48 | | typedef void (*SerializationFuncType)(const char *pszTxt, void *pUserData); |
49 | | |
50 | | private: |
51 | | CPLJSonStreamingWriter(const CPLJSonStreamingWriter &) = delete; |
52 | | CPLJSonStreamingWriter &operator=(const CPLJSonStreamingWriter &) = delete; |
53 | | |
54 | | std::string m_osStr{}; |
55 | | SerializationFuncType m_pfnSerializationFunc = nullptr; |
56 | | void *m_pUserData = nullptr; |
57 | | bool m_bPretty = true; |
58 | | std::string m_osIndent = std::string(" "); |
59 | | std::string m_osIndentAcc{}; |
60 | | int m_nLevel = 0; |
61 | | bool m_bNewLineEnabled = true; |
62 | | struct State { |
63 | | bool bIsObj = false; |
64 | | bool bFirstChild = true; |
65 | 0 | explicit State(bool bIsObjIn) : bIsObj(bIsObjIn) {} |
66 | | }; |
67 | | std::vector<State> m_states{}; |
68 | | bool m_bWaitForValue = false; |
69 | | |
70 | | void Print(const std::string &text); |
71 | | void IncIndent(); |
72 | | void DecIndent(); |
73 | | static std::string FormatString(const std::string &str); |
74 | | void EmitCommaIfNeeded(); |
75 | | |
76 | | public: |
77 | | CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc, |
78 | | void *pUserData); |
79 | | ~CPLJSonStreamingWriter(); |
80 | | |
81 | 0 | void SetPrettyFormatting(bool bPretty) { m_bPretty = bPretty; } |
82 | | void SetIndentationSize(int nSpaces); |
83 | | |
84 | | // cppcheck-suppress functionStatic |
85 | 0 | const std::string &GetString() const { return m_osStr; } |
86 | | |
87 | | void Add(const std::string &str); |
88 | | void Add(const char *pszStr); |
89 | | void AddUnquoted(const char *pszStr); |
90 | | void Add(bool bVal); |
91 | 0 | void Add(int nVal) { Add(static_cast<GIntBig>(nVal)); } |
92 | 0 | void Add(unsigned int nVal) { Add(static_cast<GIntBig>(nVal)); } |
93 | | void Add(GIntBig nVal); |
94 | | void Add(GUInt64 nVal); |
95 | | void Add(float fVal, int nPrecision = 9); |
96 | | void Add(double dfVal, int nPrecision = 18); |
97 | | void AddNull(); |
98 | | |
99 | | void StartObj(); |
100 | | void EndObj(); |
101 | | void AddObjKey(const std::string &key); |
102 | | struct CPL_DLL ObjectContext { |
103 | | CPLJSonStreamingWriter &m_serializer; |
104 | | |
105 | | ObjectContext(const ObjectContext &) = delete; |
106 | | ObjectContext(ObjectContext &&) = default; |
107 | | |
108 | | explicit inline ObjectContext(CPLJSonStreamingWriter &serializer) |
109 | 0 | : m_serializer(serializer) { |
110 | 0 | m_serializer.StartObj(); |
111 | 0 | } |
112 | 0 | ~ObjectContext() { m_serializer.EndObj(); } |
113 | | }; |
114 | 0 | inline ObjectContext MakeObjectContext() { return ObjectContext(*this); } |
115 | | |
116 | | void StartArray(); |
117 | | void EndArray(); |
118 | | struct CPL_DLL ArrayContext { |
119 | | CPLJSonStreamingWriter &m_serializer; |
120 | | bool m_bForceSingleLine; |
121 | | bool m_bNewLineEnabledBackup; |
122 | | |
123 | | ArrayContext(const ArrayContext &) = delete; |
124 | | ArrayContext(ArrayContext &&) = default; |
125 | | |
126 | | inline explicit ArrayContext(CPLJSonStreamingWriter &serializer, |
127 | | bool bForceSingleLine = false) |
128 | 0 | : m_serializer(serializer), m_bForceSingleLine(bForceSingleLine), |
129 | 0 | m_bNewLineEnabledBackup(serializer.GetNewLine()) { |
130 | 0 | if (m_bForceSingleLine) |
131 | 0 | serializer.SetNewline(false); |
132 | 0 | m_serializer.StartArray(); |
133 | 0 | } |
134 | 0 | ~ArrayContext() { |
135 | 0 | m_serializer.EndArray(); |
136 | 0 | if (m_bForceSingleLine) |
137 | 0 | m_serializer.SetNewline(m_bNewLineEnabledBackup); |
138 | 0 | } |
139 | | }; |
140 | 0 | inline ArrayContext MakeArrayContext(bool bForceSingleLine = false) { |
141 | 0 | return ArrayContext(*this, bForceSingleLine); |
142 | 0 | } |
143 | | |
144 | 0 | bool GetNewLine() const { return m_bNewLineEnabled; } |
145 | 0 | void SetNewline(bool bEnabled) { m_bNewLineEnabled = bEnabled; } |
146 | | }; |
147 | | |
148 | | NS_PROJ_END |
149 | | |
150 | | /*! @endcond */ |
151 | | |
152 | | #endif // PROJ_JSON_STREAMING_WRITER_H |