JsonReader.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_JSON_READER_H
31 #define PT_JSON_JSON_READER_H
32 
33 #include <Pt/Json/Api.h>
34 #include <Pt/Json/Node.h>
35 #include <Pt/IOStream.h>
36 #include <Pt/String.h>
37 #include <Pt/NonCopyable.h>
38 
39 namespace Pt {
40 
41 namespace Json {
42 
43 class Node;
44 class InputSource;
45 class InputIterator;
46 class JsonReaderImpl;
47 
50 class PT_JSON_API JsonReader : private NonCopyable
51 {
52  public:
56 
59  explicit JsonReader(std::basic_istream<Pt::Char>& is);
60 
64 
67  std::basic_istream<Pt::Char>* input();
68 
71  void attach(std::basic_istream<Pt::Char>& is);
72 
78  void reset();
79 
86  void reset(std::basic_istream<Pt::Char>& is);
87 
93  void setChunkSize(std::size_t n);
94 
97  void setMaxSize(std::size_t n);
98 
101  std::size_t maxSize() const;
102 
105  std::size_t usedSize() const;
106 
109  std::size_t line() const;
110 
113  InputIterator current();
114 
117  InputIterator end() const;
118 
121  Node& get();
122 
125  Node& next();
126 
130 
131  public:
132  JsonReaderImpl* impl()
133  { return _impl; }
134 
135  private:
136  JsonReaderImpl* _impl;
137 };
138 
142 {
143  public:
147  : _stream(0)
148  , _node(0)
149  { }
150 
153  explicit InputIterator(JsonReader& xis)
154  : _stream(&xis)
155  , _node(0)
156  { _node = &_stream->get(); }
157 
161  : _stream(it._stream), _node(it._node)
162  { }
163 
167  { }
168 
172  {
173  _stream = it._stream;
174  _node = it._node;
175  return *this;
176  }
177 
180  inline Node& operator*()
181  { return *_node; }
182 
185  inline Node* operator->()
186  { return _node; }
187 
191  {
192  if(_node->type() == Node::EndDocument)
193  _node = 0;
194  else
195  _node = &_stream->next();
196 
197  return *this;
198  }
199 
202  inline bool operator==(const InputIterator& it) const
203  { return _node == it._node; }
204 
207  inline bool operator!=(const InputIterator& it) const
208  { return _node != it._node; }
209 
210  private:
211  JsonReader* _stream;
212  Node* _node;
213 };
214 
215 
217 {
218  return InputIterator(*this);
219 }
220 
221 
223 {
224  return InputIterator();
225 }
226 
227 } // namespace
228 
229 } // namespace
230 
231 #endif // include guard
Node & operator*()
Derefences the iterator.
Definition: JsonReader.h:180
Core module.
Definition: Allocator.h:33
JsonReader()
Default Constructor.
InputIterator & operator=(const InputIterator &it)
Assignment operator.
Definition: JsonReader.h:171
~InputIterator()
Destructor.
Definition: JsonReader.h:166
void reset()
Clears the reader state and input.
Input iterator to read JSON nodes with a reader.
Definition: JsonReader.h:142
InputIterator(JsonReader &xis)
Construct iterator to point to current document position.
Definition: JsonReader.h:153
bool operator!=(const InputIterator &it) const
Returns true if iterators point to different nodes.
Definition: JsonReader.h:207
Node & next()
Get next node.
Node & get()
Get current node.
JSON document node.
Definition: Node.h:52
void attach(std::basic_istream< Pt::Char > &is)
Sets the input source.
Reads JSON as a Stream of Nodes.
Definition: JsonReader.h:51
Node * advance()
Process availabe data from underlying input source.
InputIterator(const InputIterator &it)
Copy constructor.
Definition: JsonReader.h:160
~JsonReader()
Destructor.
InputIterator & operator++()
Increments the iterator position.
Definition: JsonReader.h:190
Node * operator->()
Derefences the iterator.
Definition: JsonReader.h:185
void reset(std::basic_istream< Pt::Char > &is)
Starts parsing with an input source.
std::size_t usedSize() const
Returns the number of characters the parser has allocated.
JsonReader(std::basic_istream< Pt::Char > &is)
Construct with input source.
std::size_t maxSize() const
Returns the number of characters the parser may allocate.
InputIterator end() const
Returns an iterator to the end of the document.
Definition: JsonReader.h:222
std::size_t line() const
Returns the current line of the primary input source.
void setChunkSize(std::size_t n)
Sets the max size of a characters block.
InputIterator current()
Returns an iterator to the current node.
Definition: JsonReader.h:216
Protects derived classes from being copied.
Definition: NonCopyable.h:54
void setMaxSize(std::size_t n)
Sets the max number of characters the parser may allocate.
std::basic_istream< Pt::Char > * input()
Returns the current input source or nullptr if none is set.
Type type() const
Returns the type of the node.
Definition: Node.h:77
InputIterator()
Default Constructor.
Definition: JsonReader.h:146
bool operator==(const InputIterator &it) const
Returns true if both iterators point at the same node.
Definition: JsonReader.h:202