Zserio C++ runtime library  1.1.0
Built for Zserio 2.15.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  {
63  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to open '") << fileName << "'!";
64  }
65 
66  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
67 
68  if (!stream)
69  {
70  throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to write '") << fileName << "'!";
71  }
72 }
73 
74 // needed due to GCC compilation problems, GCC tries to instantiate return type even though the
75 // particular function template has different number of arguments, this prevents the return type instantiation
76 // in case that the ALLOC is not an allocator
77 template <typename ALLOC, typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
78 struct DebugStringTraits
79 {
80  using ReflectablePtr = IBasicReflectablePtr<ALLOC>;
81 };
82 
83 } // namespace detail
84 
102 template <typename T, typename ALLOC = typename T::allocator_type,
103  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
104 void toJsonStream(const T& object, std::ostream& stream, const ALLOC& allocator = ALLOC())
105 {
106  detail::toJsonStream(object, stream, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
107 }
108 
130 template <typename T, typename ALLOC = typename T::allocator_type,
131  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
132 void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, const ALLOC& allocator = ALLOC())
133 {
134  detail::toJsonStream(object, stream, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
135 }
136 
159 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
160  typename std::enable_if<
161  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
162  int>::type = 0>
164  const T& object, std::ostream& stream, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
165 {
166  detail::toJsonStream(object, stream, 4, walkFilter, allocator);
167 }
168 
193 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
194  typename std::enable_if<
195  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
196  int>::type = 0>
197 void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
198  const ALLOC& allocator = ALLOC())
199 {
200  detail::toJsonStream(object, stream, indent, walkFilter, allocator);
201 }
202 
220 template <typename T, typename ALLOC = typename T::allocator_type,
221  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
222 string<ALLOC> toJsonString(const T& object, const ALLOC& allocator = ALLOC())
223 {
224  return detail::toJsonString(object, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
225 }
226 
248 template <typename T, typename ALLOC = typename T::allocator_type,
249  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
250 string<ALLOC> toJsonString(const T& object, uint8_t indent, const ALLOC& allocator = ALLOC())
251 {
252  return detail::toJsonString(object, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
253 }
254 
276 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
277  typename std::enable_if<
278  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
279  int>::type = 0>
280 string<ALLOC> toJsonString(const T& object, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
281 {
282  return detail::toJsonString(object, 4, walkFilter, allocator);
283 }
284 
308 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
309  typename std::enable_if<
310  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
311  int>::type = 0>
313  const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC())
314 {
315  return detail::toJsonString(object, indent, walkFilter, allocator);
316 }
317 
333 template <typename T, typename ALLOC = typename T::allocator_type,
334  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
335 void toJsonFile(const T& object, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
336 {
337  return detail::toJsonFile(object, fileName, 4, BasicDefaultWalkFilter<ALLOC>(), allocator);
338 }
339 
361 template <typename T, typename ALLOC = typename T::allocator_type,
362  typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0>
364  const T& object, const string<ALLOC>& fileName, uint8_t indent, const ALLOC& allocator = ALLOC())
365 {
366  return detail::toJsonFile(object, fileName, indent, BasicDefaultWalkFilter<ALLOC>(), allocator);
367 }
368 
390 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
391  typename std::enable_if<
392  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
393  int>::type = 0>
394 void toJsonFile(const T& object, const string<ALLOC>& fileName, WALK_FILTER&& walkFilter,
395  const ALLOC& allocator = ALLOC())
396 {
397  return detail::toJsonFile(object, fileName, 4, walkFilter, allocator);
398 }
399 
421 template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type,
422  typename std::enable_if<
423  std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value,
424  int>::type = 0>
425 void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter,
426  const ALLOC& allocator = ALLOC())
427 {
428  return detail::toJsonFile(object, fileName, indent, walkFilter, allocator);
429 }
430 
455 template <typename ALLOC = std::allocator<uint8_t>>
456 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonStream(
457  const IBasicTypeInfo<ALLOC>& typeInfo, std::istream& is, const ALLOC& allocator = ALLOC())
458 {
459  BasicJsonReader<ALLOC> jsonReader(is, allocator);
460  return jsonReader.read(typeInfo);
461 }
462 
487 template <typename T, typename ALLOC = typename T::allocator_type>
488 T fromJsonStream(std::istream& is, const ALLOC& allocator = ALLOC())
489 {
490  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonStream(T::typeInfo(), is, allocator)));
491 }
492 
517 template <typename ALLOC = std::allocator<uint8_t>>
518 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonString(
519  const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
520 {
521  std::basic_istringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>> stream(json);
522  return fromJsonStream(typeInfo, stream, allocator);
523 }
524 
549 template <typename T, typename ALLOC = typename T::allocator_type>
550 T fromJsonString(const string<ALLOC>& json, const ALLOC& allocator = ALLOC())
551 {
552  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonString(T::typeInfo(), json, allocator)));
553 }
554 
578 template <typename ALLOC = std::allocator<uint8_t>>
579 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonFile(
580  const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
581 {
582  std::ifstream is = std::ifstream(fileName.c_str());
583  if (!is)
584  {
585  throw CppRuntimeException("DebugStringUtil.fromJsonFile: Failed to open '") << fileName + "'!";
586  }
587 
588  return fromJsonStream(typeInfo, is, allocator);
589 }
590 
614 template <typename T, typename ALLOC = typename T::allocator_type>
615 T fromJsonFile(const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC())
616 {
617  return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonFile(T::typeInfo(), fileName, allocator)));
618 }
619 
620 } // namespace zserio
621 
622 #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())