DAW JSON Link
daw_to_json.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_to_json_fwd.h"
14 #include "impl/version.h"
15 
16 #include <daw/daw_traits.h>
17 
18 #include <iterator>
19 #include <string>
20 #include <string_view>
21 #include <type_traits>
22 
23 namespace daw::json {
24  inline namespace DAW_JSON_VER {
25  template<typename Value, typename JsonClass, typename OutputIterator>
26  [[maybe_unused]] constexpr OutputIterator to_json( Value const &value,
27  OutputIterator out_it ) {
28  if constexpr( std::is_pointer<OutputIterator>::value ) {
29  daw_json_assert( out_it, ErrorReason::NullOutputIterator );
30  }
31  out_it = json_details::member_to_string( template_arg<JsonClass>, out_it,
32  value );
33  return out_it;
34  }
35 
36  template<typename Result, typename Value, typename JsonClass>
37  [[maybe_unused, nodiscard]] constexpr Result to_json( Value const &value ) {
38  Result result{ };
39  if constexpr( std::is_same_v<Result, std::string> ) {
40  result.reserve( 4096 );
41  }
43  template_arg<JsonClass>, std::back_inserter( result ), value );
44  if constexpr( std::is_same_v<Result, std::string> ) {
45  result.shrink_to_fit( );
46  }
47  return result;
48  }
49 
50  template<typename JsonElement, typename Container, typename OutputIterator>
51  [[maybe_unused]] constexpr OutputIterator
52  to_json_array( Container const &c, OutputIterator out_it ) {
53  static_assert(
54  traits::is_container_like_v<daw::remove_cvref_t<Container>>,
55  "Supplied container must support begin( )/end( )" );
56 
57  if constexpr( std::is_pointer<OutputIterator>::value ) {
58  daw_json_assert( out_it, ErrorReason::NullOutputIterator );
59  }
60  *out_it++ = '[';
61  bool is_first = true;
62  // Not const & as some types(vector<bool>::const_reference are not ref
63  // types
64  for( auto &&v : c ) {
65 
66  using v_type = daw::remove_cvref_t<decltype( v )>;
67  constexpr bool is_auto_detect_v =
68  std::is_same<JsonElement,
69  json_details::auto_detect_array_element>::value;
70  using JsonMember =
71  std::conditional_t<is_auto_detect_v,
72  json_details::json_deduced_type<v_type>,
73  JsonElement>;
74 
75  static_assert(
76  not std::is_same_v<JsonMember,
78  "Unable to detect unnamed mapping" );
79  // static_assert( not std::is_same_v<JsonElement, JsonMember> );
80  if( is_first ) {
81  is_first = false;
82  } else {
83  *out_it++ = ',';
84  }
85  out_it =
86  json_details::member_to_string( template_arg<JsonMember>, out_it, v );
87  }
88  // The last character will be a ',' prior to this
89  *out_it++ = ']';
90  return out_it;
91  }
92 
93  template<typename Result, typename JsonElement, typename Container>
94  [[maybe_unused, nodiscard]] constexpr Result
95  to_json_array( Container &&c ) {
96  static_assert(
97  traits::is_container_like_v<daw::remove_cvref_t<Container>>,
98  "Supplied container must support begin( )/end( )" );
99 
100  Result result{ };
101  auto out_it = json_details::basic_appender<Result>( result );
102  to_json_array<JsonElement>( c, out_it );
103  return result;
104  }
105  } // namespace DAW_JSON_VER
106 } // namespace daw::json
#define daw_json_assert(Bool,...)
Definition: daw_json_assert.h:178
constexpr OutputIterator member_to_string(template_param< JsonMember >, OutputIterator it, T const &value)
Definition: to_daw_json_string.h:1259
constexpr OutputIterator to_json(Value const &value, OutputIterator out_it)
Definition: daw_to_json.h:26
constexpr OutputIterator to_json_array(Container const &c, OutputIterator out_it)
Definition: daw_to_json.h:52
Definition: daw_from_json.h:22
Definition: daw_json_traits.h:117
#define DAW_JSON_VER
Definition: version.h:11