DAW JSON Link
daw_json_iostream.h
Go to the documentation of this file.
1 // copyright (c) darrell wright
2 //
3 // distributed under the boost software license, version 1.0. (see accompanying
4 // file license or copy at http://www.boost.org/license_1_0.txt)
5 //
6 // official repository: https://github.com/beached/daw_json_link
7 //
8 
9 #pragma once
10 
11 #include "daw_json_link.h"
12 
13 #include <daw/daw_traits.h>
14 
15 #include <ciso646>
16 #include <iostream>
17 #include <type_traits>
18 
19 namespace daw::json::json_details {
20  template<typename T>
21  using is_opted_into_json_iostreams =
22  typename json_data_contract<T>::opt_into_iostreams;
23 
24  template<typename Container>
25  using is_container_opted_into_json_iostreams =
26  is_opted_into_json_iostreams<typename Container::value_type>;
27 
28  template<typename T>
29  inline constexpr bool is_opted_into_json_iostreams_v =
30  daw::is_detected_v<is_opted_into_json_iostreams, T>;
31 
32  template<typename T>
33  inline constexpr bool is_container_opted_into_json_iostreams_v =
34  daw::is_detected_v<is_container_opted_into_json_iostreams, T>;
35 } // namespace daw::json::json_details
36 
37 template<
38  typename T,
39  std::enable_if_t<daw::json::json_details::is_opted_into_json_iostreams_v<T>,
40  std::nullptr_t> = nullptr>
41 std::ostream &operator<<( std::ostream &os, T const &value ) {
42  auto out_it = std::ostreambuf_iterator<char>( os );
43  daw::json::to_json( value, out_it );
44  return os;
45 }
46 
47 template<typename Container,
48  std::enable_if_t<daw::json::json_details::
49  is_container_opted_into_json_iostreams_v<Container>,
50  std::nullptr_t> = nullptr>
51 std::ostream &operator<<( std::ostream &os, Container const &c ) {
52  auto out_it = std::ostreambuf_iterator<char>( os );
53  *out_it++ = '[';
54  auto first = std::begin( c );
55  auto last = std::end( c );
56  if( first != last ) {
57  out_it = daw::json::to_json( *first, out_it );
58  ++first;
59  while( first != last ) {
60  *out_it = ',';
61  out_it = daw::json::to_json( *first, out_it );
62  ++first;
63  }
64  }
65  *out_it++ = ']';
66  return os;
67 }
operator<<
std::ostream & operator<<(std::ostream &os, T const &value)
Definition: daw_json_iostream.h:41
daw::json::to_json
constexpr OutputIterator to_json(Value const &value, OutputIterator out_it)
Definition: daw_json_link.h:977