A library for working with phylogenetic and population genetic data.
v0.27.0
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-2020 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 
35 
36 #include <iosfwd>
37 #include <string>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Forward declarations
44 // =================================================================================================
45 
46 class JsonDocument;
47 
48 // =================================================================================================
49 // Json Writer
50 // =================================================================================================
51 
56 {
57 public:
58 
59  // ---------------------------------------------------------------------
60  // Constructor and Rule of Five
61  // ---------------------------------------------------------------------
62 
63  JsonWriter() = default;
64  ~JsonWriter() = default;
65 
66  JsonWriter( JsonWriter const& ) = default;
67  JsonWriter( JsonWriter&& ) = default;
68 
69  JsonWriter& operator= ( JsonWriter const& ) = default;
70  JsonWriter& operator= ( JsonWriter&& ) = default;
71 
72  // ---------------------------------------------------------------------
73  // Writing
74  // ---------------------------------------------------------------------
75 
76 public:
77 
84  void write( JsonDocument const& document, std::shared_ptr<utils::BaseOutputTarget> target ) const;
85 
89  std::string to_string ( JsonDocument const& document) const;
90 
91  // ---------------------------------------------------------------------
92  // Printing
93  // ---------------------------------------------------------------------
94 
95 private:
96 
100  void print_value ( JsonDocument const& value, std::ostream& out) const;
101 
105  void print_array ( JsonDocument const& value, std::ostream& out, int indent_level) const;
106 
110  void print_object ( JsonDocument const& value, std::ostream& out, int indent_level) const;
111 
112  // ---------------------------------------------------------------------
113  // Settings
114  // ---------------------------------------------------------------------
115 
116 public:
117 
121  size_t precision() const
122  {
123  return precision_;
124  }
125 
131  JsonWriter& precision( size_t value )
132  {
133  precision_ = value;
134  return *this;
135  }
136 
140  size_t indent() const
141  {
142  return indent_;
143  }
144 
150  JsonWriter& indent( size_t value )
151  {
152  indent_ = value;
153  return *this;
154  }
155 
156  // ---------------------------------------------------------------------
157  // Data Members
158  // ---------------------------------------------------------------------
159 
160 private:
161 
162  size_t precision_ = 6;
163  size_t indent_ = 4;
164 
165 };
166 
167 } // namespace utils
168 } // namespace genesis
169 
170 #endif // include guard
genesis::utils::JsonWriter::to_string
std::string to_string(JsonDocument const &document) const
Return the Json representation of a JsonDocument.
Definition: utils/formats/json/writer.cpp:57
genesis::utils::JsonWriter::precision
JsonWriter & precision(size_t value)
Set the precision used for printing floating point numbers.
Definition: utils/formats/json/writer.hpp:131
genesis::utils::JsonWriter::operator=
JsonWriter & operator=(JsonWriter const &)=default
genesis::utils::JsonWriter::write
void write(JsonDocument const &document, std::shared_ptr< utils::BaseOutputTarget > target) const
Write a JsonDocument to an output target, using the JSON format.
Definition: utils/formats/json/writer.cpp:51
genesis::utils::JsonWriter::precision
size_t precision() const
Get the precision used for printing floating point numbers.
Definition: utils/formats/json/writer.hpp:121
genesis::utils::JsonDocument
Store a Json value of any kind.
Definition: json/document.hpp:114
genesis::utils::JsonWriter
Write Json data from a JsonDocument.
Definition: utils/formats/json/writer.hpp:55
genesis::utils::JsonWriter::indent
JsonWriter & indent(size_t value)
Set the indent used for printing the elements of Json arrays and objects.
Definition: utils/formats/json/writer.hpp:150
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::JsonWriter::JsonWriter
JsonWriter()=default
genesis::utils::JsonWriter::~JsonWriter
~JsonWriter()=default
output_target.hpp
genesis::utils::JsonWriter::indent
size_t indent() const
Get the indent used for printing the elements of Json arrays and objects.
Definition: utils/formats/json/writer.hpp:140