Zserio C++ runtime library  1.3.0
Built for Zserio 2.18.0
DebugStringUtil.h
Go to the documentation of this file.
1 
13 #ifndef ZSERIO_DEBUG_STRING_UTIL_H_INC
14 #define ZSERIO_DEBUG_STRING_UTIL_H_INC
15 
16 #include <fstream>
17 #include <sstream>
18 #include <utility>
19 
20 #include "zserio/JsonReader.h"
21 #include "zserio/JsonWriter.h"
22 #include "zserio/ReflectableUtil.h"
23 #include "zserio/Traits.h"
24 #include "zserio/Types.h"
25 #include "zserio/Walker.h"
26 
27 namespace zserio
28 {
29 
30 namespace detail
31 {
32 
33 // Implementations needs to be in detail because old MSVC compiler 2015 has problems with calling overload.
34 
35 template <typename T, typename WALK_FILTER, typename ALLOC>
36 void toJsonStream(
37  const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator)
38 {
39  static_assert(has_reflectable<T>::value,
40  "DebugStringUtil.toJsonStream: "
41  "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!");
42 
43  BasicJsonWriter<ALLOC> jsonWriter(stream, indent);
44  BasicWalker<ALLOC> walker(jsonWriter, walkFilter);
45  walker.walk(object.reflectable(allocator));
46 }
47 
48 template <typename T, typename WALK_FILTER, typename ALLOC>
49 string<ALLOC> toJsonString(const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator)
50 {
51  auto stream = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>(
52  string<ALLOC>(allocator));
53  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
54  return stream.str();
55 }
56 
57 template <typename T, typename WALK_FILTER, typename ALLOC>
58 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
59  const ALLOC& allocator)
60 {
61  std::ofstream stream = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc);
62  if (!stream)
63  {
64  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to open '") << fileName << "'!";
65  }
66 
67  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
68 
69  if (!stream)
70  {
71  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to write '") << fileName << "'!";
72  }
73 }
74 
75 // needed due to GCC compilation problems, GCC tries to instantiate return type even though the
76 // particular function template has different number of arguments, this prevents the return type instantiation
77 // in case that the ALLOC is not an allocator
78 template <typename ALLOC, typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
79 struct DebugStringTraits
80 {
81  using ReflectablePtr = IBasicReflectablePtr<ALLOC>;
82 };
83 
84 } // namespace detail
85 
103 template <typename T, typename ALLOC = typename T::allocator_type,
104  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
105 void toJsonStream(const T& object, std::ostream& stream, const ALLOC& allocator = ALLOC())
106 {
107  detail::toJsonStream(object, stream, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
108 }
109 
131 template <typename T, typename ALLOC = typename T::allocator_type,
132  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
133 void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, const ALLOC& allocator = ALLOC())
134 {
135  detail::toJsonStream(object, stream, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
136 }
137 
160 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
161  typename std::enable_if<
162  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
163  int>::type = 0>
165  const T& object, std::ostream& stream, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
166 {
167  detail::toJsonStream(object, stream, 4, walkFilter, allocator);
168 }
169 
194 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
195  typename std::enable_if<
196  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
197  int>::type = 0>
198 void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
199  const ALLOC& allocator = ALLOC())
200 {
201  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
202 }
203 
221 template <typename T, typename ALLOC = typename T::allocator_type,
222  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
223 string<ALLOC> toJsonString(const T& object, const ALLOC& allocator = ALLOC())
224 {
225  return detail::toJsonString(object, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
226 }
227 
249 template <typename T, typename ALLOC = typename T::allocator_type,
250  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
251 string<ALLOC> toJsonString(const T& object, uint8_t indent, const ALLOC& allocator = ALLOC())
252 {
253  return detail::toJsonString(object, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
254 }
255 
277 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
278  typename std::enable_if<
279  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
280  int>::type = 0>
281 string<ALLOC> toJsonString(const T& object, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
282 {
283  return detail::toJsonString(object, 4, walkFilter, allocator);
284 }
285 
309 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
310  typename std::enable_if<
311  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
312  int>::type = 0>
314  const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
315 {
316  return detail::toJsonString(object, indent, walkFilter, allocator);
317 }
318 
334 template <typename T, typename ALLOC = typename T::allocator_type,
335  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
336 void toJsonFile(const T& object, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
337 {
338  return detail::toJsonFile(object, fileName, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
339 }
340 
362 template <typename T, typename ALLOC = typename T::allocator_type,
363  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
365  const T& object, const string<ALLOC>& fileName, uint8_t indent, const ALLOC& allocator = ALLOC())
366 {
367  return detail::toJsonFile(object, fileName, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
368 }
369 
391 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
392  typename std::enable_if<
393  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
394  int>::type = 0>
395 void toJsonFile(const T& object, const string<ALLOC>& fileName, WALK_FILTER&& walkFilter,
396  const ALLOC& allocator = ALLOC())
397 {
398  return detail::toJsonFile(object, fileName, 4, walkFilter, allocator);
399 }
400 
422 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
423  typename std::enable_if<
424  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
425  int>::type = 0>
426 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
427  const ALLOC& allocator = ALLOC())
428 {
429  return detail::toJsonFile(object, fileName, indent, walkFilter, allocator);
430 }
431 
456 template <typename ALLOC = std::allocator<uint8_t>>
457 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonStream(
458  const IBasicTypeInfo<ALLOC>& typeInfo, std::istream& is, const ALLOC& allocator = ALLOC())
459 {
460  BasicJsonReader<ALLOC> jsonReader(is, allocator);
461  return jsonReader.read(typeInfo);
462 }
463 
488 template <typename T, typename ALLOC = typename T::allocator_type>
489 T fromJsonStream(std::istream& is, const ALLOC& allocator = ALLOC())
490 {
491  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonStream(T::typeInfo(), is, allocator)));
492 }
493 
518 template <typename ALLOC = std::allocator<uint8_t>>
519 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonString(
520  const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
521 {
522  std::basic_istringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>> stream(json);
523  return fromJsonStream(typeInfo, stream, allocator);
524 }
525 
550 template <typename T, typename ALLOC = typename T::allocator_type>
551 T fromJsonString(const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
552 {
553  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonString(T::typeInfo(), json, allocator)));
554 }
555 
579 template <typename ALLOC = std::allocator<uint8_t>>
580 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonFile(
581  const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
582 {
583  std::ifstream is = std::ifstream(fileName.c_str());
584  if (!is)
585  {
586  throw CppRuntimeException("DebugStringUtil.fromJsonFile: Failed to open '") << fileName + "'!";
587  }
588 
589  return fromJsonStream(typeInfo, is, allocator);
590 }
591 
615 template <typename T, typename ALLOC = typename T::allocator_type>
616 T fromJsonFile(const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
617 {
618  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonFile(T::typeInfo(), fileName, allocator)));
619 }
620 
621 } // namespace zserio
622 
623 #endif // ZSERIO_DEBUG_STRING_UTIL_H_INC
IBasicReflectablePtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
Definition: JsonReader.h:208
detail::DebugStringTraits< ALLOC >::ReflectablePtr fromJsonString(const IBasicTypeInfo< ALLOC > &typeInfo, const string< ALLOC > &json, const ALLOC &allocator=ALLOC())
detail::DebugStringTraits< ALLOC >::ReflectablePtr fromJsonStream(const IBasicTypeInfo< ALLOC > &typeInfo, std::istream &is, const ALLOC &allocator=ALLOC())
string< ALLOC > toJsonString(const T &object, uint8_t indent, WALK_FILTER &&walkFilter, const ALLOC &allocator=ALLOC())
void toJsonFile(const T &object, const string< ALLOC > &fileName, uint8_t indent, WALK_FILTER &&walkFilter, const ALLOC &allocator=ALLOC())
void toJsonStream(const T &object, std::ostream &stream, uint8_t indent, WALK_FILTER &&walkFilter, const ALLOC &allocator=ALLOC())
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
Definition: RebindAlloc.h:10
void toJsonFile(const T &object, const string< ALLOC > &fileName, const ALLOC &allocator=ALLOC())
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char > > string
Definition: String.h:17
void toJsonStream(const T &object, std::ostream &stream, const ALLOC &allocator=ALLOC())
detail::DebugStringTraits< ALLOC >::ReflectablePtr fromJsonFile(const IBasicTypeInfo< ALLOC > &typeInfo, const string< ALLOC > &fileName, const ALLOC &allocator=ALLOC())
string< ALLOC > toJsonString(const T &object, const ALLOC &allocator=ALLOC())