29 #ifndef CPL_JSON_STREAMING_WRITER_H
30 #define CPL_JSON_STREAMING_WRITER_H
34 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
41 class CPL_DLL CPLJSonStreamingWriter
44 typedef void (*SerializationFuncType)(
const char* pszTxt,
void* pUserData);
47 CPLJSonStreamingWriter(
const CPLJSonStreamingWriter&) =
delete;
48 CPLJSonStreamingWriter& operator=(
const CPLJSonStreamingWriter&) =
delete;
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{};
57 bool m_bNewLineEnabled =
true;
61 bool bFirstChild =
true;
62 explicit State(
bool bIsObjIn): bIsObj(bIsObjIn) {}
64 std::vector<State> m_states{};
65 bool m_bWaitForValue =
false;
67 void Print(
const std::string& text);
70 static std::string FormatString(
const std::string& str);
71 void EmitCommaIfNeeded();
74 CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc,
76 ~CPLJSonStreamingWriter();
78 void SetPrettyFormatting(
bool bPretty) { m_bPretty = bPretty; }
79 void SetIndentationSize(
int nSpaces);
82 const std::string& GetString()
const {
return m_osStr; }
84 void Add(
const std::string& str);
85 void Add(
const char* pszStr);
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);
97 void AddObjKey(
const std::string& key);
98 struct CPL_DLL ObjectContext
100 CPLJSonStreamingWriter& m_serializer;
102 ObjectContext(
const ObjectContext &) =
delete;
103 ObjectContext(ObjectContext&&) =
default;
105 explicit inline ObjectContext(CPLJSonStreamingWriter& serializer):
106 m_serializer(serializer) { m_serializer.StartObj(); }
107 ~ObjectContext() { m_serializer.EndObj(); }
109 inline ObjectContext MakeObjectContext() {
return ObjectContext(*
this); }
113 struct CPL_DLL ArrayContext
115 CPLJSonStreamingWriter& m_serializer;
116 bool m_bForceSingleLine;
117 bool m_bNewLineEnabledBackup;
119 ArrayContext(
const ArrayContext &) =
delete;
120 ArrayContext(ArrayContext&&) =
default;
122 inline explicit ArrayContext(CPLJSonStreamingWriter& serializer,
123 bool bForceSingleLine =
false):
124 m_serializer(serializer),
125 m_bForceSingleLine(bForceSingleLine),
126 m_bNewLineEnabledBackup(serializer.GetNewLine())
128 if( m_bForceSingleLine )
129 serializer.SetNewline(
false);
130 m_serializer.StartArray();
135 m_serializer.EndArray();
136 if( m_bForceSingleLine )
137 m_serializer.SetNewline(m_bNewLineEnabledBackup);
140 inline ArrayContext MakeArrayContext(
bool bForceSingleLine =
false)
141 {
return ArrayContext(*
this, bForceSingleLine); }
143 bool GetNewLine()
const {
return m_bNewLineEnabled; }
144 void SetNewline(
bool bEnabled) { m_bNewLineEnabled = bEnabled; }
147 #endif // __cplusplus
151 #endif // CPL_JSON_STREAMING_WRITER_H