Argo  1.0
A C++ library for handling JSON.
parser.hpp
Go to the documentation of this file.
1 #ifndef _json_parser_hpp_
2 #define _json_parser_hpp_
3 
4 /*
5  * Copyright (c) 2017 Andrew Haisley
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
27 
28 #include <istream>
29 
30 #include "common.hpp"
31 #include "json.hpp"
32 #include "reader.hpp"
33 #include "lexer.hpp"
34 
35 namespace NAMESPACE
36 {
41  std::istream &operator>>(std::istream &stream, json &e);
42 
47  void operator>>(std::string &stream, json &e);
48 
55  class parser
56  {
57  public:
58 
60  static const size_t max_message_length = 10*1024*1024;
61 
66  static const size_t max_token_length = 100*1024;
67 
72  static const size_t max_nesting_depth = 1000;
73 
83  static std::unique_ptr<json> parse(std::istream &i);
84 
85 #ifndef _ARGO_WINDOWS_
86 
95  static std::unique_ptr<json> parse(int fd);
96 #endif
97 
107  static std::unique_ptr<json> parse(FILE *f);
108 
118  static std::unique_ptr<json> parse(const char *s);
119 
129  static std::unique_ptr<json> parse(const std::string &s);
130 
140  static std::unique_ptr<json> load(const std::string &file_name);
141 
180  parser(
181  reader &r,
182  bool read_all = true,
183  size_t p_max_token_length = max_token_length,
184  size_t p_max_nesting_depth = max_nesting_depth,
185  bool p_convert_numbers = true,
186  bool p_fallback_to_double = true,
187  bool p_convert_strings = true);
188 
194  std::unique_ptr<json> parse();
195 
196  private:
197 
198  std::unique_ptr<json> parse_number_int(const token &t);
199  std::unique_ptr<json> parse_number_double(const token &t);
200  std::unique_ptr<json> parse_string(const token &t);
201  std::unique_ptr<json> parse_value(lexer &l, size_t nesting_depth);
202  std::unique_ptr<json> parse_array(lexer &l, size_t nesting_depth);
203  void parse_name_value_pair(lexer &l, std::unique_ptr<json> &object, size_t nesting_depth);
204  std::unique_ptr<json> parse_object(lexer &l, size_t nesting_depth);
205 
207  reader &m_reader;
208 
213  bool m_read_all;
214 
216  size_t m_max_token_length;
217 
219  size_t m_max_nesting_depth;
220 
222  bool m_convert_numbers;
223 
225  bool m_fallback_to_double;
226 
228  bool m_convert_strings;
229 
230  };
231 }
232 
233 #endif
std::istream & operator>>(std::istream &stream, json &e)
Definition: parser.cpp:379
A class to read JSON messages from various types of input stream.
Definition: reader.hpp:42
The lexer class.
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
A recursive decent parser for JSON messages.
Definition: parser.hpp:55
The json class.
Common defs needed everywhere and, as far as is possible, platform specific changes.
Lexical tokens read from a JSON message.
Definition: token.hpp:41
A lexical analyser for JSON messages.
Definition: lexer.hpp:41
The reader class.