13 #ifndef ZSERIO_DEBUG_STRING_UTIL_H_INC 14 #define ZSERIO_DEBUG_STRING_UTIL_H_INC 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)
38 static_assert(has_reflectable<T>::value,
"DebugStringUtil.toJsonStream: " 39 "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!");
41 BasicJsonWriter<ALLOC> jsonWriter(os, indent);
42 BasicWalker<ALLOC> walker(jsonWriter, walkFilter);
43 walker.walk(
object.reflectable(allocator));
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)
49 auto os = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>(
50 string<ALLOC>(allocator));
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)
59 std::ofstream os = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc);
61 throw CppRuntimeException(
"DebugStringUtil.toJsonFile: Failed to open '") << fileName <<
"'!";
66 throw CppRuntimeException(
"DebugStringUtil.toJsonFile: Failed to write '") << fileName <<
"'!";
72 template <
typename ALLOC,
73 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
74 struct DebugStringTraits
76 using ReflectablePtr = IBasicReflectablePtr<ALLOC>;
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())
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())
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())
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())
213 template <
typename T,
typename ALLOC =
typename T::allocator_type,
214 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
241 template <
typename T,
typename ALLOC =
typename T::allocator_type,
242 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
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>
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>
304 const ALLOC& allocator = ALLOC())
324 template <
typename T,
typename ALLOC =
typename T::allocator_type,
325 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
352 template <
typename T,
typename ALLOC =
typename T::allocator_type,
353 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
355 const ALLOC& allocator = ALLOC())
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>
385 const ALLOC& allocator = ALLOC())
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>
415 const ALLOC& allocator = ALLOC())
444 template <
typename ALLOC = std::allocator<u
int8_t>>
445 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
449 return jsonReader.
read(typeInfo);
476 template <
typename T,
typename ALLOC =
typename T::allocator_type>
479 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonStream(T::typeInfo(), is, allocator)));
506 template <
typename ALLOC = std::allocator<u
int8_t>>
507 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
509 const ALLOC& allocator = ALLOC())
539 template <
typename T,
typename ALLOC =
typename T::allocator_type>
542 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonString(T::typeInfo(), json, allocator)));
568 template <
typename ALLOC = std::allocator<u
int8_t>>
569 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
571 const ALLOC& allocator = ALLOC())
573 std::ifstream is = std::ifstream(fileName.c_str());
575 throw CppRuntimeException(
"DebugStringUtil.fromJsonFile: Failed to open '") << fileName +
"'!";
602 template <
typename T,
typename ALLOC =
typename T::allocator_type>
605 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonFile(T::typeInfo(), fileName, allocator)));
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)
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
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