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 
40 namespace Pt {
41 
42 namespace Json {
43 
44 class JsonReader;
45 class JsonWriter;
46 class Node;
47 
50 class PT_JSON_API JsonFormatter : public Pt::Formatter
51  , private NonCopyable
52 {
53  public:
57 
61 
65 
69 
72  void attach(JsonReader& reader);
73 
76  void attach(JsonWriter& writer);
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 
146  bool onParseSome();
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
Core module.
Definition: Allocator.h:33
void onAddFloat(const char *name, float value, const char *id)
Formats a float value.
JsonFormatter()
Default constructor.
void onBeginMember(const char *name)
Formats the begin of a struct member.
void onAddBool(const char *name, bool value, const char *id)
Formats a bool value.
void onAddInt8(const char *name, Pt::int8_t value, const char *id)
Formats a 8-bit integer.
void onAddUInt32(const char *name, Pt::uint32_t value, const char *id)
Formats a 32-bit unsigned integer.
void attach(JsonWriter &writer)
Attach to an JsonWriter.
void onAddBinary(const char *name, const char *type, const char *value, std::size_t length, const char *id)
Formats a binary value.
void onAddUInt8(const char *name, Pt::uint8_t value, const char *id)
Formats a 8-bit unsigned integer.
void onFinishSequence()
Formats the end of a sequence.
JSON Formatter.
Definition: JsonFormatter.h:52
void onBeginParse(Composer &)
Begin to parse to a composer.
void onAddChar(const char *name, const Pt::Char &value, const char *id)
Formats a character value.
JsonReader * reader()
Returns the attached JsonReader or a nullptr.
Definition: JsonFormatter.h:80
void onAddInt32(const char *name, Pt::int32_t value, const char *id)
Formats a 32-bit integer.
bool onParseSome()
Returns true if composer completes, false if no more data available.
JSON document node.
Definition: Node.h:52
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
Reads JSON as a Stream of Nodes.
Definition: JsonReader.h:51
void onAddUInt64(const char *name, Pt::uint64_t value, const char *id)
Formats a 64-bit unsigned integer.
virtual void onBeginElement()
Formats the begin of a sequence element.
Unicode character type.
Definition: String.h:67
void onAddLongDouble(const char *name, long double value, const char *id)
Formats a long double value.
void onFinishMember()
Formats the end of a struct member.
void onFinishStruct()
Formats the end of a struct.
~JsonFormatter()
Destructor.
JsonFormatter(JsonWriter &writer)
Construct with a JsonWriter.
Composes types during serialization.
Definition: Composer.h:43
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Api-Types.h:20
void onBeginSequence(const char *name, const char *type, const char *id)
Formats the begin of a sequence.
void onAddDouble(const char *name, double value, const char *id)
Formats a double value.
JsonFormatter(JsonReader &reader)
Construct with a JsonReader.
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Api-Types.h:48
int_type int32_t
Signed 32-bit integer type.
Definition: Api-Types.h:41
Support for serialization to different formats.
Definition: Formatter.h:46
Protects derived classes from being copied.
Definition: NonCopyable.h:54
void onAddString(const char *name, const char *type, const Pt::Char *value, const char *id)
Formats a string value.
void onAddReference(const char *name, const char *id)
Formats a reference.
int_type int8_t
Signed 8-bit integer type.
Definition: Api-Types.h:13
void onParse()
Parse until composer completes.
void onAddInt16(const char *name, Pt::int16_t value, const char *id)
Formats a 16-bit integer.
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Api-Types.h:34
JsonWriter * writer()
Returns the attached JsonWriter or a nullptr.
Definition: JsonFormatter.h:85
int_type int16_t
Signed 16-bit integer type.
Definition: Api-Types.h:27
void attach(JsonReader &reader)
Attach to an JsonReader.
void detach()
Detach from its JsonReader and JsonWriter.
void onAddInt64(const char *name, Pt::int64_t value, const char *id)
Formats a 64-bit integer.
void onAddUInt16(const char *name, Pt::uint16_t value, const char *id)
Formats a 16-bit unsigned integer.
void onBeginStruct(const char *name, const char *type, const char *id)
Formats the begin of a struct.
Writes JSON to a text stream.
Definition: JsonWriter.h:46
virtual void onFinishElement()
Formats the end of a sequence element.