Argo  1.0
A C++ library for handling JSON.
unparser.hpp
Go to the documentation of this file.
1 #ifndef _json_unparser_hpp_
2 #define _json_unparser_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 <ostream>
29 
30 #include "common.hpp"
31 #include "json.hpp"
32 #include "writer.hpp"
33 
34 namespace NAMESPACE
35 {
40  std::ostream &operator<<(std::ostream &stream, const json &e);
41 
46  void operator<<(std::string &s, const json &e);
47 
54  class unparser
55  {
56  public:
57 
69  static void unparse(
70  std::ostream &o,
71  const json &j,
72  const char *space = " ",
73  const char *newline = "",
74  const char *indent = " ",
75  int indent_inc = 0);
76 
77 #ifndef _ARGO_WINDOWS_
78 
83  static void unparse(
84  int fd,
85  const json &j,
86  const char *space = " ",
87  const char *newline = "",
88  const char *indent = " ",
89  int indent_inc = 0);
90 #endif
91 
97  static void unparse(
98  FILE *s,
99  const json &j,
100  const char *space = " ",
101  const char *newline = "",
102  const char *indent = " ",
103  int indent_inc = 0);
104 
111  static void save(
112  const json &j,
113  const std::string &file_name,
114  const char *space = " ",
115  const char *newline = "",
116  const char *indent = " ",
117  int indent_inc = 0);
118 
129  unparser(writer &w,
130  const char *space = " ",
131  const char *newline = "",
132  const char *indent = " ",
133  int indent_inc = 0);
134 
135 
142  void unparse(const json &j, int indent_level = 0);
143 
144  private:
145 
146  void print_indent(int indent_level);
147  void unparse_object(const json &j, int indent_level);
148  void unparse_array(const json &j, int indent_level);
149 
150  writer &m_writer;
151  const char *m_space;
152  const char *m_newline;
153  const char *m_indent;
154  int m_indent_inc;
155 
156  };
157 
158 }
159 
160 #endif
Simple proxy for write operations on various types of stream.
Definition: writer.hpp:41
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
The json class.
Class to unparse json instances into JSON messages.
Definition: unparser.hpp:54
Common defs needed everywhere and, as far as is possible, platform specific changes.
std::ostream & operator<<(std::ostream &stream, const json_exception &e)
All json things are represented by instances of this class.
Definition: json.hpp:63
The writer class.