Formatter.h
1 /*
2  * Copyright (C) 2020-2026 by 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_JSONRPC_FORMATTER_H
31 #define PT_JSONRPC_FORMATTER_H
32 
33 #include <Pt/JsonRpc/Api.h>
34 #include <Pt/Composer.h>
35 #include <Pt/Formatter.h>
36 #include <Pt/NonCopyable.h>
37 #include <Pt/Types.h>
38 #include <stack>
39 
40 namespace Pt {
41 
42 namespace Json {
43  class JsonWriter;
44  class Node;
45 }
46 
47 namespace JsonRpc {
48 
55 class PT_JSONRPC_API Formatter : public Pt::Formatter
56  , private NonCopyable
57 {
58  public:
62 
65  explicit Formatter(Json::JsonWriter& writer);
66 
70 
73  void attach(Json::JsonWriter& writer);
74 
77  void detach();
78 
82  { return _writer; }
83 
92  bool advance(const Json::Node& node);
93 
94  protected:
95  void onAddString(const char* name, const char* type,
96  const Pt::Char* value, const char* id);
97 
98  void onAddBool(const char* name, bool value, const char* id);
99 
100  void onAddChar(const char* name, const Pt::Char& value, const char* id);
101 
102  void onAddInt8(const char* name, Pt::int8_t value, const char* id);
103 
104  void onAddInt16(const char* name, Pt::int16_t value, const char* id);
105 
106  void onAddInt32(const char* name, Pt::int32_t value, const char* id);
107 
108  void onAddInt64(const char* name, Pt::int64_t value, const char* id);
109 
110  void onAddUInt8(const char* name, Pt::uint8_t value, const char* id);
111 
112  void onAddUInt16(const char* name, Pt::uint16_t value, const char* id);
113 
114  void onAddUInt32(const char* name, Pt::uint32_t value, const char* id);
115 
116  void onAddUInt64(const char* name, Pt::uint64_t value, const char* id);
117 
118  void onAddFloat(const char* name, float value, const char* id);
119 
120  void onAddDouble(const char* name, double value, const char* id);
121 
122  void onAddLongDouble(const char* name, long double value, const char* id);
123 
124  void onAddBinary(const char* name, const char* type,
125  const char* value, std::size_t length, const char* id);
126 
127  void onAddReference(const char* name, const char* id);
128 
129  void onBeginSequence(const char* name, const char* type, const char* id);
130 
131  virtual void onBeginElement();
132 
133  virtual void onFinishElement();
134 
136 
137  void onBeginStruct(const char* name, const char* type, const char* id);
138 
139  void onBeginMember(const char* name);
140 
142 
144 
145  protected:
146  void onBeginParse(Composer& composer);
147 
148  bool onParseSome();
149 
150  void onParse();
151 
152  private:
154  void onBeginNode(const Json::Node& node);
155 
157  void onArrayNode(const Json::Node& node);
158 
160  void onObjectNode(const Json::Node& node);
161 
162  private:
163  Json::JsonWriter* _writer;
164 
165  typedef void (Formatter::*ProcessNode)(const Json::Node&);
166  ProcessNode _parse;
167  std::stack<ProcessNode> _parseStack;
168 
169  Composer* _composer;
170 };
171 
172 } // namespace JsonRpc
173 
174 } // namespace Pt
175 
176 #endif // PT_JSONRPC_FORMATTER_H
Core module.
Definition: Allocator.h:33
void onAddUInt8(const char *name, Pt::uint8_t value, const char *id)
Formats a 8-bit unsigned integer.
void onAddInt16(const char *name, Pt::int16_t value, const char *id)
Formats a 16-bit integer.
Formatter for JSON-RPC message serialization and deserialization.
Definition: Formatter.h:57
void onFinishStruct()
Formats the end of a struct.
~Formatter()
Destructor.
void onAddUInt32(const char *name, Pt::uint32_t value, const char *id)
Formats a 32-bit unsigned integer.
void onAddString(const char *name, const char *type, const Pt::Char *value, const char *id)
Formats a string value.
void onAddUInt64(const char *name, Pt::uint64_t value, const char *id)
Formats a 64-bit unsigned integer.
void attach(Json::JsonWriter &writer)
Attach to a JsonWriter for output.
void onAddInt64(const char *name, Pt::int64_t value, const char *id)
Formats a 64-bit integer.
void onAddChar(const char *name, const Pt::Char &value, const char *id)
Formats a character value.
Formatter()
Default constructor.
bool onParseSome()
Returns true if composer completes, false if no more data available.
bool advance(const Json::Node &node)
Advances parsing with a single JSON node.
virtual void onFinishElement()
Formats the end of a sequence element.
void onAddBool(const char *name, bool value, const char *id)
Formats a bool value.
JSON document node.
Definition: Node.h:52
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
void onAddInt8(const char *name, Pt::int8_t value, const char *id)
Formats a 8-bit integer.
int_type int64_t
Signed 64-bit integer type.
Definition: Api-Types.h:55
void onAddBinary(const char *name, const char *type, const char *value, std::size_t length, const char *id)
Formats a binary value.
void onAddFloat(const char *name, float value, const char *id)
Formats a float value.
Unicode character type.
Definition: String.h:67
void onAddDouble(const char *name, double value, const char *id)
Formats a double value.
virtual void onBeginElement()
Formats the begin of a sequence element.
void onFinishSequence()
Formats the end of a sequence.
void onAddReference(const char *name, const char *id)
Formats a reference.
void onBeginMember(const char *name)
Formats the begin of a struct member.
void onBeginParse(Composer &composer)
Begin to parse to a composer.
void onFinishMember()
Formats the end of a struct member.
void onBeginSequence(const char *name, const char *type, const char *id)
Formats the begin of a sequence.
Composes types during serialization.
Definition: Composer.h:43
void onParse()
Parse until composer completes.
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Api-Types.h:20
void detach()
Detach from the current JsonWriter.
void onBeginStruct(const char *name, const char *type, const char *id)
Formats the begin of a struct.
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 onAddLongDouble(const char *name, long double value, const char *id)
Formats a long double value.
int_type int8_t
Signed 8-bit integer type.
Definition: Api-Types.h:13
Json::JsonWriter * writer()
Returns the attached JsonWriter or nullptr.
Definition: Formatter.h:81
void onAddUInt16(const char *name, Pt::uint16_t value, const char *id)
Formats a 16-bit unsigned integer.
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Api-Types.h:34
int_type int16_t
Signed 16-bit integer type.
Definition: Api-Types.h:27
void onAddInt32(const char *name, Pt::int32_t value, const char *id)
Formats a 32-bit integer.
Formatter(Json::JsonWriter &writer)
Construct with a JsonWriter for output.
Writes JSON to a text stream.
Definition: JsonWriter.h:46