13 #ifndef ZSERIO_DEBUG_STRING_UTIL_H_INC
14 #define ZSERIO_DEBUG_STRING_UTIL_H_INC
35 template <
typename T,
typename WALK_FILTER,
typename ALLOC>
37 const T&
object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
const ALLOC& allocator)
39 static_assert(has_reflectable<T>::value,
40 "DebugStringUtil.toJsonStream: "
41 "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!");
43 BasicJsonWriter<ALLOC> jsonWriter(stream, indent);
44 BasicWalker<ALLOC> walker(jsonWriter, walkFilter);
45 walker.walk(
object.reflectable(allocator));
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)
51 auto stream = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>(
52 string<ALLOC>(allocator));
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)
61 std::ofstream stream = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc);
64 throw CppRuntimeException(
"DebugStringUtil.toJsonFile: Failed to open '") << fileName <<
"'!";
71 throw CppRuntimeException(
"DebugStringUtil.toJsonFile: Failed to write '") << fileName <<
"'!";
78 template <typename ALLOC, typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
79 struct DebugStringTraits
81 using ReflectablePtr = IBasicReflectablePtr<ALLOC>;
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())
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())
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,
165 const T&
object, std::ostream& stream, WALK_FILTER&& walkFilter,
const ALLOC& allocator = ALLOC())
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,
198 void toJsonStream(
const T&
object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
199 const ALLOC& allocator = ALLOC())
221 template <
typename T,
typename ALLOC =
typename T::allocator_type,
222 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
249 template <
typename T,
typename ALLOC =
typename T::allocator_type,
250 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
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,
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,
314 const T&
object, uint8_t indent, WALK_FILTER&& walkFilter,
const ALLOC& allocator = ALLOC())
334 template <
typename T,
typename ALLOC =
typename T::allocator_type,
335 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
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())
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,
396 const ALLOC& allocator = ALLOC())
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,
427 const ALLOC& allocator = ALLOC())
456 template <
typename ALLOC = std::allocator<u
int8_t>>
461 return jsonReader.
read(typeInfo);
488 template <
typename T,
typename ALLOC =
typename T::allocator_type>
491 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonStream(T::typeInfo(), is, allocator)));
518 template <
typename ALLOC = std::allocator<u
int8_t>>
550 template <
typename T,
typename ALLOC =
typename T::allocator_type>
553 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonString(T::typeInfo(), json, allocator)));
579 template <
typename ALLOC = std::allocator<u
int8_t>>
580 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
fromJsonFile(
583 std::ifstream is = std::ifstream(fileName.c_str());
586 throw CppRuntimeException(
"DebugStringUtil.fromJsonFile: Failed to open '") << fileName +
"'!";
615 template <
typename T,
typename ALLOC =
typename T::allocator_type>
618 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonFile(T::typeInfo(), fileName, allocator)));
IBasicReflectablePtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
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
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
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())