Zserio C++ runtime library  1.0.0
Built for Zserio 2.13.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 <sstream>
17 #include <fstream>
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(const T& object, std::ostream& os, uint8_t indent, WALK_FILTER&& walkFilter,
36  const ALLOC& allocator)
37 {
38  static_assert(has_reflectable<T>::value, "DebugStringUtil.toJsonStream: "
39  "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!");
40 
41  BasicJsonWriter<ALLOC> jsonWriter(os, indent);
42  BasicWalker<ALLOC> walker(jsonWriter, walkFilter);
43  walker.walk(object.reflectable(allocator));
44 }
45 
46 template <typename T, typename WALK_FILTER, typename ALLOC>
47 string<ALLOC> toJsonString(const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator)
48 {
49  auto os = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>(
50  string<ALLOC>(allocator));
51  detail::toJsonStream(object, os, indent, walkFilter, allocator);
52  return os.str();
53 }
54 
55 template <typename T, typename WALK_FILTER, typename ALLOC>
56 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
57  const ALLOC& allocator)
58 {
59  std::ofstream os = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc);
60  if (!os)
61  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to open '") << fileName << "'!";
62 
63  detail::toJsonStream(object, os, indent, walkFilter, allocator);
64 
65  if (!os)
66  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to write '") << fileName << "'!";
67 }
68 
69 // needed due to GCC compilation problems, GCC tries to instantiate return type even though the
70 // particular fuction template has different number of arguments, this prevents the return type instantiation
71 // in case that the ALLOC is not an allocator
72 template <typename ALLOC,
73  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& os, const ALLOC& allocator = ALLOC())
101 {
102  detail::toJsonStream(object, os, 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& os, uint8_t indent, const ALLOC& allocator = ALLOC())
129 {
130  detail::toJsonStream(object, os, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
131 }
132 
155 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
156  typename std::enable_if<std::is_base_of<IBasicWalkFilter<ALLOC>,
157  typename std::decay<WALK_FILTER>::type>::value, int>::type = 0>
158 void toJsonStream(const T& object, std::ostream& os, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
159 {
160  detail::toJsonStream(object, os, 4, walkFilter, allocator);
161 }
162 
187 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
188  typename std::enable_if<std::is_base_of<IBasicWalkFilter<ALLOC>,
189  typename std::decay<WALK_FILTER>::type>::value, int>::type = 0>
190 void toJsonStream(const T& object, std::ostream& os, uint8_t indent, WALK_FILTER&& walkFilter,
191  const ALLOC& allocator = ALLOC())
192 {
193  detail::toJsonStream(object, os, indent, walkFilter, allocator);
194 }
195 
213 template <typename T, typename ALLOC = typename T::allocator_type,
214  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
215 string<ALLOC> toJsonString(const T& object, const ALLOC& allocator = ALLOC())
216 {
217  return detail::toJsonString(object, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
218 }
219 
241 template <typename T, typename ALLOC = typename T::allocator_type,
242  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
243 string<ALLOC> toJsonString(const T& object, uint8_t indent, const ALLOC& allocator = ALLOC())
244 {
245  return detail::toJsonString(object, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
246 }
247 
269 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
270  typename std::enable_if<std::is_base_of<IBasicWalkFilter<ALLOC>,
271  typename std::decay<WALK_FILTER>::type>::value, int>::type = 0>
272 string<ALLOC> toJsonString(const T& object, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
273 {
274  return detail::toJsonString(object, 4, walkFilter, allocator);
275 }
276 
300 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
301  typename std::enable_if<std::is_base_of<IBasicWalkFilter<ALLOC>,
302  typename std::decay<WALK_FILTER>::type>::value, int>::type = 0>
303 string<ALLOC> toJsonString(const T& object, uint8_t indent, WALK_FILTER&& walkFilter,
304  const ALLOC& allocator = ALLOC())
305 {
306  return detail::toJsonString(object, indent, walkFilter, allocator);
307 }
308 
324 template <typename T, typename ALLOC = typename T::allocator_type,
325  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
326 void toJsonFile(const T& object, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
327 {
328  return detail::toJsonFile(object, fileName, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
329 }
330 
352 template <typename T, typename ALLOC = typename T::allocator_type,
353  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
354 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent,
355  const ALLOC& allocator = ALLOC())
356 {
357  return detail::toJsonFile(object, fileName, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
358 }
359 
381 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
382  typename std::enable_if<std::is_base_of<IBasicWalkFilter<ALLOC>,
383  typename std::decay<WALK_FILTER>::type>::value, int>::type = 0>
384 void toJsonFile(const T& object, const string<ALLOC>& fileName, WALK_FILTER&& walkFilter,
385  const ALLOC& allocator = ALLOC())
386 {
387  return detail::toJsonFile(object, fileName, 4, walkFilter, allocator);
388 }
389 
411 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
412  typename std::enable_if<std::is_base_of<IBasicWalkFilter<ALLOC>,
413  typename std::decay<WALK_FILTER>::type>::value, int>::type = 0>
414 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
415  const ALLOC& allocator = ALLOC())
416 {
417  return detail::toJsonFile(object, fileName, indent, walkFilter, allocator);
418 }
419 
444 template <typename ALLOC = std::allocator<uint8_t>>
445 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
446 fromJsonStream(const IBasicTypeInfo<ALLOC>& typeInfo, std::istream& is, const ALLOC& allocator = ALLOC())
447 {
448  BasicJsonReader<ALLOC> jsonReader(is, allocator);
449  return jsonReader.read(typeInfo);
450 }
451 
476 template <typename T, typename ALLOC = typename T::allocator_type>
477 T fromJsonStream(std::istream& is, const ALLOC& allocator = ALLOC())
478 {
479  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonStream(T::typeInfo(), is, allocator)));
480 }
481 
506 template <typename ALLOC = std::allocator<uint8_t>>
507 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
509  const ALLOC& allocator = ALLOC())
510 {
511  std::basic_istringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>> is(json);
512  return fromJsonStream(typeInfo, is, allocator);
513 }
514 
539 template <typename T, typename ALLOC = typename T::allocator_type>
540 T fromJsonString(const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
541 {
542  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonString(T::typeInfo(), json, allocator)));
543 }
544 
568 template <typename ALLOC = std::allocator<uint8_t>>
569 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
570 fromJsonFile(const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& fileName,
571  const ALLOC& allocator = ALLOC())
572 {
573  std::ifstream is = std::ifstream(fileName.c_str());
574  if (!is)
575  throw CppRuntimeException("DebugStringUtil.fromJsonFile: Failed to open '") << fileName + "'!";
576  return fromJsonStream(typeInfo, is, allocator);
577 }
578 
602 template <typename T, typename ALLOC = typename T::allocator_type>
603 T fromJsonFile(const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
604 {
605  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonFile(T::typeInfo(), fileName, allocator)));
606 }
607 
608 } // namespace zserio
609 
610 #endif // ZSERIO_DEBUG_STRING_UTIL_H_INC
detail::DebugStringTraits< ALLOC >::ReflectablePtr fromJsonFile(const IBasicTypeInfo< ALLOC > &typeInfo, const string< ALLOC > &fileName, const ALLOC &allocator=ALLOC())
IBasicReflectablePtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
Definition: JsonReader.h:201
void toJsonStream(const T &object, std::ostream &os, uint8_t indent, WALK_FILTER &&walkFilter, const ALLOC &allocator=ALLOC())
detail::DebugStringTraits< ALLOC >::ReflectablePtr fromJsonString(const IBasicTypeInfo< ALLOC > &typeInfo, const string< ALLOC > &json, const ALLOC &allocator=ALLOC())
string< ALLOC > toJsonString(const T &object, uint8_t indent, WALK_FILTER &&walkFilter, const ALLOC &allocator=ALLOC())
string< ALLOC > toJsonString(const T &object, const ALLOC &allocator=ALLOC())
void toJsonFile(const T &object, const string< ALLOC > &fileName, const ALLOC &allocator=ALLOC())
void toJsonStream(const T &object, std::ostream &os, const ALLOC &allocator=ALLOC())
void toJsonFile(const T &object, const string< ALLOC > &fileName, uint8_t indent, WALK_FILTER &&walkFilter, const ALLOC &allocator=ALLOC())
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char >> string
Definition: String.h:16
detail::DebugStringTraits< ALLOC >::ReflectablePtr fromJsonStream(const IBasicTypeInfo< ALLOC > &typeInfo, std::istream &is, const ALLOC &allocator=ALLOC())
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
Definition: RebindAlloc.h:10