A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils/formats/json/writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_JSON_WRITER_H_
2 #define GENESIS_UTILS_FORMATS_JSON_WRITER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <iosfwd>
35 #include <string>
36 
37 namespace genesis {
38 namespace utils {
39 
40 // =================================================================================================
41 // Forward declarations
42 // =================================================================================================
43 
44 class JsonDocument;
45 
46 // =================================================================================================
47 // Json Writer
48 // =================================================================================================
49 
54 {
55 public:
56 
57  // ---------------------------------------------------------------------
58  // Constructor and Rule of Five
59  // ---------------------------------------------------------------------
60 
61  JsonWriter() = default;
62  ~JsonWriter() = default;
63 
64  JsonWriter( JsonWriter const& ) = default;
65  JsonWriter( JsonWriter&& ) = default;
66 
67  JsonWriter& operator= ( JsonWriter const& ) = default;
68  JsonWriter& operator= ( JsonWriter&& ) = default;
69 
70  // ---------------------------------------------------------------------
71  // Writing
72  // ---------------------------------------------------------------------
73 
74 public:
75 
79  void to_stream ( JsonDocument const& document, std::ostream& out ) const;
80 
89  void to_file ( JsonDocument const& document, std::string const& filename) const;
90 
94  void to_string ( JsonDocument const& document, std::string& output) const;
95 
99  std::string to_string ( JsonDocument const& document) const;
100 
101  // ---------------------------------------------------------------------
102  // Printing
103  // ---------------------------------------------------------------------
104 
105 private:
106 
110  void print_value ( JsonDocument const& value, std::ostream& out) const;
111 
115  void print_array ( JsonDocument const& value, std::ostream& out, int indent_level) const;
116 
120  void print_object ( JsonDocument const& value, std::ostream& out, int indent_level) const;
121 
122  // ---------------------------------------------------------------------
123  // Settings
124  // ---------------------------------------------------------------------
125 
126 public:
127 
131  size_t precision() const
132  {
133  return precision_;
134  }
135 
141  JsonWriter& precision( size_t value )
142  {
143  precision_ = value;
144  return *this;
145  }
146 
150  size_t indent() const
151  {
152  return indent_;
153  }
154 
160  JsonWriter& indent( size_t value )
161  {
162  indent_ = value;
163  return *this;
164  }
165 
166  // ---------------------------------------------------------------------
167  // Data Members
168  // ---------------------------------------------------------------------
169 
170 private:
171 
172  size_t precision_ = 6;
173  size_t indent_ = 4;
174 
175 };
176 
177 } // namespace utils
178 } // namespace genesis
179 
180 #endif // include guard
JsonWriter & operator=(JsonWriter const &)=default
size_t indent() const
Get the indent used for printing the elements of Json arrays and objects.
Write Json data from a JsonDocument.
JsonWriter & precision(size_t value)
Set the precision used for printing floating point numbers.
void to_string(JsonDocument const &document, std::string &output) const
Give the Json string representation of a JsonDocument.
JsonWriter & indent(size_t value)
Set the indent used for printing the elements of Json arrays and objects.
size_t precision() const
Get the precision used for printing floating point numbers.
Store a Json value of any kind.
void to_stream(JsonDocument const &document, std::ostream &out) const
Write a JsonDocument to a stream.
void to_file(JsonDocument const &document, std::string const &filename) const
Write a JsonDocument to a file.