Metall  v0.27
A persistent memory allocator for data-centric analytics
pretty_print.hpp
Go to the documentation of this file.
1 // Copyright 2021 Lawrence Livermore National Security, LLC and other Metall
2 // Project Developers. See the top-level COPYRIGHT file for details.
3 //
4 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
5 
6 #ifndef METALL_JSON_PRETTY_PRINT_HPP
7 #define METALL_JSON_PRETTY_PRINT_HPP
8 
9 #include <iostream>
10 
11 #include <metall/json/json_fwd.hpp>
12 
13 namespace metall::json::jsndtl {
14 template <typename allocator_type, int indent_size>
15 inline void pretty_print_impl(std::ostream &os, const value<allocator_type> &jv,
16  const std::string &indent) {
17  if (jv.is_bool()) {
18  os << std::boolalpha << jv.as_bool();
19  } else if (jv.is_int64()) {
20  os << jv.as_int64();
21  } else if (jv.is_uint64()) {
22  os << jv.as_uint64();
23  } else if (jv.is_double()) {
24  os << jv.as_double();
25  } else if (jv.is_string()) {
26  os << json::serialize(jv.as_string());
27  } else if (jv.is_array()) {
28  os << "[\n";
29  const auto &arr = jv.as_array();
30  std::string new_indent = indent;
31  new_indent.append(indent_size, ' ');
32  for (std::size_t i = 0; i < arr.size(); ++i) {
33  os << new_indent;
34  pretty_print_impl<allocator_type, indent_size>(os, arr[i], new_indent);
35  if (i < arr.size() - 1) {
36  os << ",\n";
37  }
38  }
39  os << "\n" << indent << "]";
40  } else if (jv.is_object()) {
41  os << "{\n";
42  const auto &obj = jv.as_object();
43  std::string new_indent = indent;
44  new_indent.append(indent_size, ' ');
45  for (auto it = obj.begin();;) {
46  os << new_indent << it->key() << " : ";
47  pretty_print_impl<allocator_type, indent_size>(os, it->value(),
48  new_indent);
49  if (++it == obj.end()) {
50  break;
51  }
52  os << ",\n";
53  }
54  os << "\n" << indent << "}";
55 
56  } else if (jv.is_null()) {
57  os << "null";
58  }
59 }
60 
61 } // namespace metall::json::jsndtl
62 
63 namespace metall::json {
64 
70 #ifdef DOXYGEN_SKIP
71 template <typename allocator_type, int indent_size = 2>
72 #else
73 template <typename allocator_type, int indent_size>
74 #endif
75 inline void pretty_print(std::ostream &os,
76  const value<allocator_type> &json_value) {
77  std::string indent;
78  jsndtl::pretty_print_impl<allocator_type, indent_size>(os, json_value,
79  indent);
80  os << std::endl;
81 }
82 
83 } // namespace metall::json
84 
85 #endif // METALL_JSON_PRETTY_PRINT_HPP
double & as_double()
Return a reference to the underlying double, or throw an exception.
Definition: value.hpp:444
bool is_null() const noexcept
Return true if this is a null.
Definition: value.hpp:472
bool is_string() const noexcept
Return true if this is a string.
Definition: value.hpp:495
bool is_int64() const noexcept
Return true if this is a int64.
Definition: value.hpp:480
object_type & as_object()
Return a reference to the underlying object, or throw an exception.
Definition: value.hpp:465
bool is_uint64() const noexcept
Return true if this is a uint64.
Definition: value.hpp:485
bool is_bool() const noexcept
Return true if this is a bool.
Definition: value.hpp:477
std::uint64_t & as_uint64()
Return a reference to the underlying std::uint64_t, or throw an exception.
Definition: value.hpp:435
string_type & as_string()
Return a reference to the underlying string, or throw an exception.
Definition: value.hpp:451
bool is_object() const noexcept
Return true if this is a object.
Definition: value.hpp:505
std::int64_t & as_int64()
Return a reference to the underlying std::int64_t, or throw an exception.
Definition: value.hpp:425
bool is_array() const noexcept
Return true if this is an array.
Definition: value.hpp:500
array_type & as_array()
Return a reference to the underlying array, or throw an exception.
Definition: value.hpp:458
bool & as_bool()
Return a reference to the underlying bool, or throw an exception.
Definition: value.hpp:417
bool is_double() const noexcept
Return true if this is a double.
Definition: value.hpp:490
basic_string< char > string
A string container that uses char as its character type and Metall as its default allocator.
Definition: string.hpp:23
Definition: array.hpp:23
void pretty_print_impl(std::ostream &os, const value< allocator_type > &jv, const std::string &indent)
Definition: pretty_print.hpp:15
Namespace for Metall JSON container, which is in an experimental phase.
Definition: array.hpp:17
std::string serialize(const value< allocator_type > &input)
Definition: serialize.hpp:22
void pretty_print(std::ostream &os, const value< allocator_type > &json_value)
Pretty-prints a JSON value.
Definition: pretty_print.hpp:75