aboutsummaryrefslogtreecommitdiff
path: root/src/proj_json_streaming_writer.hpp
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2019-07-06 02:03:50 +0200
committerEven Rouault <even.rouault@spatialys.com>2019-07-06 02:27:46 +0200
commit17f0b0b3bc65ffba39bf6f22a12b2cc7fcb9bafd (patch)
tree5993d701145e2117fb8598faa186312b98d54f00 /src/proj_json_streaming_writer.hpp
parent1da55c8be619a21153845607a553c9d1206bc792 (diff)
downloadPROJ-17f0b0b3bc65ffba39bf6f22a12b2cc7fcb9bafd.tar.gz
PROJ-17f0b0b3bc65ffba39bf6f22a12b2cc7fcb9bafd.zip
Proof-of-concept of JSON export limited to PrimeMeridian (refs #1545)
Diffstat (limited to 'src/proj_json_streaming_writer.hpp')
-rw-r--r--src/proj_json_streaming_writer.hpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/proj_json_streaming_writer.hpp b/src/proj_json_streaming_writer.hpp
new file mode 100644
index 00000000..d1510449
--- /dev/null
+++ b/src/proj_json_streaming_writer.hpp
@@ -0,0 +1,145 @@
+/******************************************************************************
+ *
+ * Project: CPL - Common Portability Library
+ * Purpose: JSon streaming writer
+ * Author: Even Rouault, even.rouault at spatialys.com
+ *
+ ******************************************************************************
+ * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ ****************************************************************************/
+
+#ifndef PROJ_JSON_STREAMING_WRITER_H
+#define PROJ_JSON_STREAMING_WRITER_H
+
+/*! @cond Doxygen_Suppress */
+
+#include <vector>
+#include <string>
+
+#define CPL_DLL
+
+namespace PROJ
+{
+
+typedef std::int64_t GIntBig;
+typedef std::uint64_t GUInt64;
+
+class CPL_DLL CPLJSonStreamingWriter
+{
+public:
+ typedef void (*SerializationFuncType)(const char* pszTxt, void* pUserData);
+
+private:
+ CPLJSonStreamingWriter(const CPLJSonStreamingWriter&) = delete;
+ CPLJSonStreamingWriter& operator=(const CPLJSonStreamingWriter&) = delete;
+
+ std::string m_osStr{};
+ SerializationFuncType m_pfnSerializationFunc = nullptr;
+ void* m_pUserData = nullptr;
+ bool m_bPretty = true;
+ std::string m_osIndent = std::string(" ");
+ std::string m_osIndentAcc{};
+ int m_nLevel = 0;
+ bool m_bNewLineEnabled = true;
+ struct State
+ {
+ bool bIsObj = false;
+ bool bFirstChild = true;
+ explicit State(bool bIsObjIn): bIsObj(bIsObjIn) {}
+ };
+ std::vector<State> m_states{};
+ bool m_bWaitForValue = false;
+
+ void Print(const std::string& text);
+ void IncIndent();
+ void DecIndent();
+ static std::string FormatString(const std::string& str);
+ void EmitCommaIfNeeded();
+
+public:
+ CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc,
+ void* pUserData);
+ ~CPLJSonStreamingWriter();
+
+ void SetPrettyFormatting(bool bPretty) { m_bPretty = bPretty; }
+ void SetIndentationSize(int nSpaces);
+
+ // cppcheck-suppress functionStatic
+ const std::string& GetString() const { return m_osStr; }
+
+ void Add(const std::string& str);
+ void Add(const char* pszStr);
+ void Add(bool bVal);
+ void Add(int nVal) { Add(static_cast<GIntBig>(nVal)); }
+ void Add(unsigned int nVal) { Add(static_cast<GIntBig>(nVal)); }
+ void Add(GIntBig nVal);
+ void Add(GUInt64 nVal);
+ void Add(float fVal, int nPrecision = 9);
+ void Add(double dfVal, int nPrecision = 18);
+ void AddNull();
+
+ void StartObj();
+ void EndObj();
+ void AddObjKey(const std::string& key);
+ struct CPL_DLL ObjectContext
+ {
+ CPLJSonStreamingWriter& m_serializer;
+ explicit ObjectContext(CPLJSonStreamingWriter& serializer):
+ m_serializer(serializer) { m_serializer.StartObj(); }
+ ~ObjectContext() { m_serializer.EndObj(); }
+ };
+
+ void StartArray();
+ void EndArray();
+ struct CPL_DLL ArrayContext
+ {
+ CPLJSonStreamingWriter& m_serializer;
+ bool m_bForceSingleLine;
+ bool m_bNewLineEnabledBackup;
+
+ ArrayContext(CPLJSonStreamingWriter& serializer,
+ bool bForceSingleLine = false):
+ m_serializer(serializer),
+ m_bForceSingleLine(bForceSingleLine),
+ m_bNewLineEnabledBackup(serializer.GetNewLine())
+ {
+ if( m_bForceSingleLine )
+ serializer.SetNewline(false);
+ m_serializer.StartArray();
+
+ }
+ ~ArrayContext()
+ {
+ m_serializer.EndArray();
+ if( m_bForceSingleLine )
+ m_serializer.SetNewline(m_bNewLineEnabledBackup);
+ }
+ };
+
+ bool GetNewLine() const { return m_bNewLineEnabled; }
+ void SetNewline(bool bEnabled) { m_bNewLineEnabled = bEnabled; }
+};
+
+} // namespace PROJ
+
+/*! @endcond */
+
+#endif // PROJ_JSON_STREAMING_WRITER_H