29 #ifndef CPL_JSON_STREAMING_WRITER_H
30 #define CPL_JSON_STREAMING_WRITER_H
34 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
40 class CPL_DLL CPLJSonStreamingWriter
43 typedef void (*SerializationFuncType)(
const char* pszTxt,
void* pUserData);
46 CPLJSonStreamingWriter(
const CPLJSonStreamingWriter&) =
delete;
47 CPLJSonStreamingWriter& operator=(
const CPLJSonStreamingWriter&) =
delete;
49 std::string m_osStr{};
50 SerializationFuncType m_pfnSerializationFunc =
nullptr;
51 void* m_pUserData =
nullptr;
52 bool m_bPretty =
true;
53 std::string m_osIndent = std::string(
" ");
54 std::string m_osIndentAcc{};
56 bool m_bNewLineEnabled =
true;
60 bool bFirstChild =
true;
61 explicit State(
bool bIsObjIn): bIsObj(bIsObjIn) {}
63 std::vector<State> m_states{};
64 bool m_bWaitForValue =
false;
66 void Print(
const std::string& text);
69 static std::string FormatString(
const std::string& str);
70 void EmitCommaIfNeeded();
73 CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc,
75 ~CPLJSonStreamingWriter();
77 void SetPrettyFormatting(
bool bPretty) { m_bPretty = bPretty; }
78 void SetIndentationSize(
int nSpaces);
81 const std::string& GetString()
const {
return m_osStr; }
83 void Add(
const std::string& str);
84 void Add(
const char* pszStr);
86 void Add(
int nVal) { Add(
static_cast<GIntBig>(nVal)); }
87 void Add(
unsigned int nVal) { Add(
static_cast<GIntBig>(nVal)); }
90 void Add(
float fVal,
int nPrecision = 9);
91 void Add(
double dfVal,
int nPrecision = 18);
96 void AddObjKey(
const std::string& key);
97 struct CPL_DLL ObjectContext
99 CPLJSonStreamingWriter& m_serializer;
101 ObjectContext(
const ObjectContext &) =
delete;
102 ObjectContext(ObjectContext&&) =
default;
104 explicit inline ObjectContext(CPLJSonStreamingWriter& serializer):
105 m_serializer(serializer) { m_serializer.StartObj(); }
106 ~ObjectContext() { m_serializer.EndObj(); }
108 inline ObjectContext MakeObjectContext() {
return ObjectContext(*
this); }
112 struct CPL_DLL ArrayContext
114 CPLJSonStreamingWriter& m_serializer;
115 bool m_bForceSingleLine;
116 bool m_bNewLineEnabledBackup;
118 ArrayContext(
const ArrayContext &) =
delete;
119 ArrayContext(ArrayContext&&) =
default;
121 inline explicit ArrayContext(CPLJSonStreamingWriter& serializer,
122 bool bForceSingleLine =
false):
123 m_serializer(serializer),
124 m_bForceSingleLine(bForceSingleLine),
125 m_bNewLineEnabledBackup(serializer.GetNewLine())
127 if( m_bForceSingleLine )
128 serializer.SetNewline(
false);
129 m_serializer.StartArray();
134 m_serializer.EndArray();
135 if( m_bForceSingleLine )
136 m_serializer.SetNewline(m_bNewLineEnabledBackup);
139 inline ArrayContext MakeArrayContext(
bool bForceSingleLine =
false)
140 {
return ArrayContext(*
this, bForceSingleLine); }
142 bool GetNewLine()
const {
return m_bNewLineEnabled; }
143 void SetNewline(
bool bEnabled) { m_bNewLineEnabled = bEnabled; }
Core portability definitions for CPL.
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:265
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:244