JsonWriter.h
1 /*
2  Copyright (C) 2015-2024 by Dr. Marc Boris Duerner
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 
30 #ifndef PT_JSON_JSONWRITER_H
31 #define PT_JSON_JSONWRITER_H
32 
33 #include <Pt/Json/Api.h>
34 #include <Pt/IOStream.h>
35 #include <Pt/String.h>
36 #include <Pt/Types.h>
37 #include <cstddef>
38 
39 namespace Pt {
40 
41 namespace Json {
42 
45 class PT_JSON_API JsonWriter
46 {
47  public:
51 
54  JsonWriter(std::basic_ostream<Char>& os);
55 
59 
62  bool isFormatting() const;
63 
66  void setFormatting(bool value);
67 
70  const Pt::String& indent() const;
71 
74  void setIndent(const Pt::String& indent);
75 
81  void reset();
82 
88  void reset(std::basic_ostream<Char>& os);
89 
92  std::basic_ostream<Char>* output();
93 
96  void writeObject();
97 
101 
104  void writeArray();
105 
109 
112  void writeMember(const Pt::Char* name, std::size_t nameSize);
113 
116  void writeMember(const Pt::String& name);
117 
120  void writeMember(const char* name);
121 
124  void writeString(const Pt::Char* value, std::size_t valueSize);
125 
126  void writeString(const Pt::Char* value);
127 
130  void writeString(const Pt::String& value);
131 
134  void writeInt(Pt::int64_t value);
135 
138  void writeUInt(Pt::uint64_t value);
139 
142  void writeFloat(long double value);
143 
146  void writeBool(bool value);
147 
150  void writeNull();
151 
152  private:
153  class JsonWriterImpl* _impl;
154 };
155 
156 
157 inline void JsonWriter::writeMember(const Pt::String& name)
158 {
159  this->writeMember( name.c_str(), name.size() );
160 }
161 
162 
163 inline void JsonWriter::writeString(const Pt::String& value)
164 {
165  this->writeString( value.c_str(), value.size() );
166 }
167 
168 } // namespace
169 
170 } // namespace
171 
172 #endif // include guard
Core module.
Definition: Allocator.h:33
void writeObjectEnd()
Writes the end of an object.
JsonWriter(std::basic_ostream< Char > &os)
Constructs with output stream.
size_type size() const
Returns the length of the string.
Definition: Api-String.h:240
void writeBool(bool value)
Writes an boolean value.
void writeArrayEnd()
Writes the end of an array.
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: Api-String.h:265
void writeObject()
Writes the begin of an object.
void writeString(const Pt::Char *value, std::size_t valueSize)
Writes a string value.
void setFormatting(bool value)
Indicates wether indentation should be written.
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
int_type int64_t
Signed 64-bit integer type.
Definition: Api-Types.h:55
void writeMember(const Pt::Char *name, std::size_t nameSize)
Writes a member.
std::basic_ostream< Char > * output()
Returns the output stream or a nullptr if none was set.
Unicode character type.
Definition: String.h:67
const Pt::String & indent() const
Returns the indentation string.
void writeUInt(Pt::uint64_t value)
Writes an unsigned integer value.
bool isFormatting() const
Returns true if indentation should be written.
void writeArray()
Writes the begin of an array.
~JsonWriter()
Destructor.
void setIndent(const Pt::String &indent)
Sets the indentation string.
void reset(std::basic_ostream< Char > &os)
Clears the writer state and output.
JsonWriter()
Constructor.
void reset()
Clears the writer state and output.
void writeMember(const char *name)
Writes a member.
void writeNull()
Writes an null value.
void writeInt(Pt::int64_t value)
Writes an integer value.
Unicode capable basic_string.
Definition: Api-String.h:44
void writeFloat(long double value)
Writes an float value.
Writes JSON to a text stream.
Definition: JsonWriter.h:46