JsonFormatter.h
1/*
2 Copyright (C) 2015-2023 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_FORMATTER_H
31#define PT_JSON_FORMATTER_H
32
33#include <Pt/Json/Api.h>
34#include <Pt/String.h>
35#include <Pt/Formatter.h>
36#include <Pt/NonCopyable.h>
37
38#include <stack>
39
40namespace Pt {
41
42namespace Json {
43
44class JsonReader;
45class JsonWriter;
46class Node;
47
50class PT_JSON_API JsonFormatter : public Pt::Formatter
51 , private NonCopyable
52{
53 public:
57
61
65
69
73
77
81 { return _reader; }
82
86 { return _writer; }
87
90 void detach();
91
92 protected:
93 void onAddString(const char* name, const char* type,
94 const Pt::Char* value, const char* id);
95
96 void onAddBool(const char* name, bool value, const char* id);
97
98 void onAddChar(const char* name, const Pt::Char& value, const char* id);
99
100 void onAddInt8(const char* name, Pt::int8_t value, const char* id);
101
102 void onAddInt16(const char* name, Pt::int16_t value, const char* id);
103
104 void onAddInt32(const char* name, Pt::int32_t value, const char* id);
105
106 void onAddInt64(const char* name, Pt::int64_t value, const char* id);
107
108 void onAddUInt8(const char* name, Pt::uint8_t value, const char* id);
109
110 void onAddUInt16(const char* name, Pt::uint16_t value, const char* id);
111
112 void onAddUInt32(const char* name, Pt::uint32_t value, const char* id);
113
114 void onAddUInt64(const char* name, Pt::uint64_t value, const char* id);
115
116 void onAddFloat(const char* name, float value, const char* id);
117
118 void onAddDouble(const char* name, double value, const char* id);
119
120 void onAddLongDouble(const char* name, long double value, const char* id);
121
122 void onAddBinary(const char* name, const char* type,
123 const char* value, std::size_t length, const char* id);
124
125 void onAddReference(const char* name, const char* id);
126
127 void onBeginSequence(const char* name, const char* type, const char* id);
128
129 virtual void onBeginElement();
130
131 virtual void onFinishElement();
132
134
135 void onBeginStruct(const char* name, const char* type, const char* id);
136
137 void onBeginMember(const char* name);
138
140
142
143 protected:
145
147
148 void onParse();
149
150 protected:
152 void OnBegin(const Node& node);
153
155 void onArray(const Node& node);
156
158 void onObject(const Node& node);
159
160 private:
162 JsonReader* _reader;
163
165 JsonWriter* _writer;
166
168 typedef void (JsonFormatter::*ProcessNode)(const Node&);
169
171 ProcessNode _parse;
172 std::stack<ProcessNode> _parseStack;
173
175 Composer* _composer;
176};
177
178} // namespace
179
180} // namespace
181
182#endif
Composes types during serialization.
Definition Composer.h:101
Support for serialization to different formats.
Definition Formatter.h:46
bool onParseSome()
Returns true if composer completes, false if no more data available.
void onAddChar(const char *name, const Pt::Char &value, const char *id)
Formats a character value.
void onAddBinary(const char *name, const char *type, const char *value, std::size_t length, const char *id)
Formats a binary value.
void onAddBool(const char *name, bool value, const char *id)
Formats a bool value.
~JsonFormatter()
Destructor.
JsonFormatter(JsonReader &reader)
Construct with a JsonReader.
void onFinishMember()
Formats the end of a struct member.
void onBeginParse(Composer &)
Begin to parse to a composer.
void onAddReference(const char *name, const char *id)
Formats a reference.
void onParse()
Parse until composer completes.
JsonFormatter(JsonWriter &writer)
Construct with a JsonWriter.
void onAddInt8(const char *name, Pt::int8_t value, const char *id)
Formats a 8-bit integer.
void onAddInt64(const char *name, Pt::int64_t value, const char *id)
Formats a 64-bit integer.
void onFinishSequence()
Formats the end of a sequence.
virtual void onBeginElement()
Formats the begin of a sequence element.
void onAddInt32(const char *name, Pt::int32_t value, const char *id)
Formats a 32-bit integer.
JsonFormatter()
Default constructor.
JsonWriter * writer()
Returns the attached JsonWriter or a nullptr.
Definition JsonFormatter.h:85
void onFinishStruct()
Formats the end of a struct.
void attach(JsonWriter &writer)
Attach to an JsonWriter.
void onAddUInt8(const char *name, Pt::uint8_t value, const char *id)
Formats a 8-bit unsigned integer.
void onAddUInt16(const char *name, Pt::uint16_t value, const char *id)
Formats a 16-bit unsigned integer.
void onAddUInt32(const char *name, Pt::uint32_t value, const char *id)
Formats a 32-bit unsigned integer.
void detach()
Detach from its JsonReader and JsonWriter.
void onBeginSequence(const char *name, const char *type, const char *id)
Formats the begin of a sequence.
void onAddInt16(const char *name, Pt::int16_t value, const char *id)
Formats a 16-bit integer.
void onAddUInt64(const char *name, Pt::uint64_t value, const char *id)
Formats a 64-bit unsigned integer.
void attach(JsonReader &reader)
Attach to an JsonReader.
JsonReader * reader()
Returns the attached JsonReader or a nullptr.
Definition JsonFormatter.h:80
void onAddString(const char *name, const char *type, const Pt::Char *value, const char *id)
Formats a string value.
void onAddFloat(const char *name, float value, const char *id)
Formats a float value.
void onBeginStruct(const char *name, const char *type, const char *id)
Formats the begin of a struct.
void onAddDouble(const char *name, double value, const char *id)
Formats a double value.
void onAddLongDouble(const char *name, long double value, const char *id)
Formats a long double value.
void onBeginMember(const char *name)
Formats the begin of a struct member.
virtual void onFinishElement()
Formats the end of a sequence element.
Reads JSON as a Stream of Nodes.
Definition JsonReader.h:51
Writes JSON to a text stream.
Definition JsonWriter.h:46
JSON document node.
Definition Node.h:52
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
uint_type uint16_t
Unsigned 16-bit integer type.
Definition Api-Types.h:38
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
int_type int32_t
Signed 32-bit integer type.
Definition Api-Types.h:45
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
uint_type uint64_t
Unsigned 64-bit integer type.
Definition Api-Types.h:66
int_type int16_t
Signed 16-bit integer type.
Definition Api-Types.h:31
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
int_type int8_t
Signed 8-bit integer type.
Definition Api-Types.h:17
Read and write JSON ducuments.
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67