13#ifndef CPL_JSON_STREAMING_WRITER_H
14#define CPL_JSON_STREAMING_WRITER_H
18#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
27class CPL_DLL CPLJSonStreamingWriter
30 typedef void (*SerializationFuncType)(
const char *pszTxt,
void *pUserData);
33 CPLJSonStreamingWriter(
const CPLJSonStreamingWriter &) =
delete;
34 CPLJSonStreamingWriter &operator=(
const CPLJSonStreamingWriter &) =
delete;
36 std::string m_osStr{};
37 SerializationFuncType m_pfnSerializationFunc =
nullptr;
38 void *m_pUserData =
nullptr;
39 bool m_bPretty =
true;
40 std::string m_osIndent = std::string(
" ");
41 std::string m_osIndentAcc{};
43 bool m_bNewLineEnabled =
true;
44 std::string m_osTmpForSerialize{};
45 std::string m_osTmpForFormatString{};
50 bool bFirstChild =
true;
52 explicit State(
bool bIsObjIn) : bIsObj(bIsObjIn)
57 std::vector<State> m_states{};
58 bool m_bWaitForValue =
false;
62 const std::string &FormatString(
const std::string_view &str);
63 void EmitCommaIfNeeded();
65 void Serialize(
const char *pszStr,
size_t nLength);
68 virtual void Serialize(
const std::string_view &str);
71 CPLJSonStreamingWriter(SerializationFuncType pfnSerializationFunc,
73 virtual ~CPLJSonStreamingWriter();
77 void SetPrettyFormatting(
bool bPretty)
82 void SetIndentationSize(
int nSpaces);
85 const std::string &GetString()
const
90 void Add(
const char *pszStr);
91 void Add(
const std::string &str);
92 void Add(
const std::string_view &str);
95 void AddSerializedValue(
const std::string_view &str);
99 Add(
static_cast<std::int64_t
>(nVal));
102 void Add(
unsigned int nVal)
104 Add(
static_cast<std::int64_t
>(nVal));
107 void Add(std::int64_t nVal);
108 void Add(std::uint64_t nVal);
109 void Add(GFloat16 hfVal,
int nPrecision = 5);
110 void Add(
float fVal,
int nPrecision = 9);
111 void Add(
double dfVal,
int nPrecision = 17);
116 void AddObjKey(
const std::string_view &key);
118 struct CPL_DLL ObjectContext
120 CPLJSonStreamingWriter &m_serializer;
122 ObjectContext(
const ObjectContext &) =
delete;
123 ObjectContext(ObjectContext &&) =
default;
125 explicit inline ObjectContext(CPLJSonStreamingWriter &serializer)
126 : m_serializer(serializer)
128 m_serializer.StartObj();
133 m_serializer.EndObj();
137 inline ObjectContext MakeObjectContext()
139 return ObjectContext(*
this);
145 struct CPL_DLL ArrayContext
147 CPLJSonStreamingWriter &m_serializer;
148 bool m_bForceSingleLine;
149 bool m_bNewLineEnabledBackup;
151 ArrayContext(
const ArrayContext &) =
delete;
152 ArrayContext(ArrayContext &&) =
default;
154 inline explicit ArrayContext(CPLJSonStreamingWriter &serializer,
155 bool bForceSingleLine =
false)
156 : m_serializer(serializer), m_bForceSingleLine(bForceSingleLine),
157 m_bNewLineEnabledBackup(serializer.GetNewLine())
159 if (m_bForceSingleLine)
160 serializer.SetNewline(
false);
161 m_serializer.StartArray();
166 m_serializer.EndArray();
167 if (m_bForceSingleLine)
168 m_serializer.SetNewline(m_bNewLineEnabledBackup);
172 inline ArrayContext MakeArrayContext(
bool bForceSingleLine =
false)
174 return ArrayContext(*
this, bForceSingleLine);
177 bool GetNewLine()
const
179 return m_bNewLineEnabled;
182 void SetNewline(
bool bEnabled)
184 m_bNewLineEnabled = bEnabled;
Core portability definitions for CPL.