Zserio C++ runtime library  1.0.1
Built for Zserio 2.14.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/Walker.h"
25 
26 namespace zserio
27 {
28 
29 namespace detail
30 {
31 
32 // Implementations needs to be in detail because old MSVC compiler 2015 has problems with calling overload.
33 
34 template <typename T, typename WALK_FILTER, typename ALLOC>
35 void toJsonStream(
36  const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator)
37 {
38  static_assert(has_reflectable<T>::value,
39  "DebugStringUtil.toJsonStream: "
40  "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!");
41 
42  BasicJsonWriter<ALLOC> jsonWriter(stream, indent);
43  BasicWalker<ALLOC> walker(jsonWriter, walkFilter);
44  walker.walk(object.reflectable(allocator));
45 }
46 
47 template <typename T, typename WALK_FILTER, typename ALLOC>
48 string<ALLOC> toJsonString(const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator)
49 {
50  auto stream = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>(
51  string<ALLOC>(allocator));
52  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
53  return stream.str();
54 }
55 
56 template <typename T, typename WALK_FILTER, typename ALLOC>
57 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
58  const ALLOC& allocator)
59 {
60  std::ofstream stream = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc);
61  if (!stream)
62  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to open '") << fileName << "'!";
63 
64  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
65 
66  if (!stream)
67  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to write '") << fileName << "'!";
68 }
69 
70 // needed due to GCC compilation problems, GCC tries to instantiate return type even though the
71 // particular function template has different number of arguments, this prevents the return type instantiation
72 // in case that the ALLOC is not an allocator
73 template <typename ALLOC, typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
74 struct DebugStringTraits
75 {
76  using ReflectablePtr = IBasicReflectablePtr<ALLOC>;
77 };
78 
79 } // namespace detail
80 
98 template <typename T, typename ALLOC = typename T::allocator_type,
99  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
100 void toJsonStream(const T& object, std::ostream& stream, const ALLOC& allocator = ALLOC())
101 {
102  detail::toJsonStream(object, stream, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
103 }
104 
126 template <typename T, typename ALLOC = typename T::allocator_type,
127  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
128 void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, const ALLOC& allocator = ALLOC())
129 {
130  detail::toJsonStream(object, stream, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
131 }
132 
155 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
156  typename std::enable_if<
157  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
158  int>::type = 0>
160  const T& object, std::ostream& stream, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
161 {
162  detail::toJsonStream(object, stream, 4, walkFilter, allocator);
163 }
164 
189 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
190  typename std::enable_if<
191  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
192  int>::type = 0>
193 void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
194  const ALLOC& allocator = ALLOC())
195 {
196  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
197 }
198 
216 template <typename T, typename ALLOC = typename T::allocator_type,
217  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
218 string<ALLOC> toJsonString(const T& object, const ALLOC& allocator = ALLOC())
219 {
220  return detail::toJsonString(object, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
221 }
222 
244 template <typename T, typename ALLOC = typename T::allocator_type,
245  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
246 string<ALLOC> toJsonString(const T& object, uint8_t indent, const ALLOC& allocator = ALLOC())
247 {
248  return detail::toJsonString(object, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
249 }
250 
272 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
273  typename std::enable_if<
274  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
275  int>::type = 0>
276 string<ALLOC> toJsonString(const T& object, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
277 {
278  return detail::toJsonString(object, 4, walkFilter, allocator);
279 }
280 
304 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
305  typename std::enable_if<
306  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
307  int>::type = 0>
309  const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
310 {
311  return detail::toJsonString(object, indent, walkFilter, allocator);
312 }
313 
329 template <typename T, typename ALLOC = typename T::allocator_type,
330  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
331 void toJsonFile(const T& object, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
332 {
333  return detail::toJsonFile(object, fileName, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
334 }
335 
357 template <typename T, typename ALLOC = typename T::allocator_type,
358  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
360  const T& object, const string<ALLOC>& fileName, uint8_t indent, const ALLOC& allocator = ALLOC())
361 {
362  return detail::toJsonFile(object, fileName, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
363 }
364 
386 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
387  typename std::enable_if<
388  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
389  int>::type = 0>
390 void toJsonFile(const T& object, const string<ALLOC>& fileName, WALK_FILTER&& walkFilter,
391  const ALLOC& allocator = ALLOC())
392 {
393  return detail::toJsonFile(object, fileName, 4, walkFilter, allocator);
394 }
395 
417 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
418  typename std::enable_if<
419  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
420  int>::type = 0>
421 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
422  const ALLOC& allocator = ALLOC())
423 {
424  return detail::toJsonFile(object, fileName, indent, walkFilter, allocator);
425 }
426 
451 template <typename ALLOC = std::allocator<uint8_t>>
452 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonStream(
453  const IBasicTypeInfo<ALLOC>& typeInfo, std::istream& is, const ALLOC& allocator = ALLOC())
454 {
455  BasicJsonReader<ALLOC> jsonReader(is, allocator);
456  return jsonReader.read(typeInfo);
457 }
458 
483 template <typename T, typename ALLOC = typename T::allocator_type>
484 T fromJsonStream(std::istream& is, const ALLOC& allocator = ALLOC())
485 {
486  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonStream(T::typeInfo(), is, allocator)));
487 }
488 
513 template <typename ALLOC = std::allocator<uint8_t>>
514 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonString(
515  const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
516 {
517  std::basic_istringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>> stream(json);
518  return fromJsonStream(typeInfo, stream, allocator);
519 }
520 
545 template <typename T, typename ALLOC = typename T::allocator_type>
546 T fromJsonString(const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
547 {
548  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonString(T::typeInfo(), json, allocator)));
549 }
550 
574 template <typename ALLOC = std::allocator<uint8_t>>
575 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonFile(
576  const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
577 {
578  std::ifstream is = std::ifstream(fileName.c_str());
579  if (!is)
580  throw CppRuntimeException("DebugStringUtil.fromJsonFile: Failed to open '") << fileName + "'!";
581  return fromJsonStream(typeInfo, is, allocator);
582 }
583 
607 template <typename T, typename ALLOC = typename T::allocator_type>
608 T fromJsonFile(const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
609 {
610  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonFile(T::typeInfo(), fileName, allocator)));
611 }
612 
613 } // namespace zserio
614 
615 #endif // ZSERIO_DEBUG_STRING_UTIL_H_INC
IBasicReflectablePtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
Definition: JsonReader.h:207
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())