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>
36 const T&
object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
const ALLOC& allocator)
38 static_assert(has_reflectable<T>::value,
39 "DebugStringUtil.toJsonStream: "
40 "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!");
42 BasicJsonWriter<ALLOC> jsonWriter(stream, indent);
43 BasicWalker<ALLOC> walker(jsonWriter, walkFilter);
44 walker.walk(
object.reflectable(allocator));
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)
50 auto stream = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>(
51 string<ALLOC>(allocator));
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)
60 std::ofstream stream = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc);
63 throw CppRuntimeException(
"DebugStringUtil.toJsonFile: Failed to open '") << fileName <<
"'!";
70 throw CppRuntimeException(
"DebugStringUtil.toJsonFile: Failed to write '") << fileName <<
"'!";
77 template <typename ALLOC, typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
78 struct DebugStringTraits
80 using ReflectablePtr = IBasicReflectablePtr<ALLOC>;
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())
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())
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,
164 const T&
object, std::ostream& stream, WALK_FILTER&& walkFilter,
const ALLOC& allocator = ALLOC())
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,
197 void toJsonStream(
const T&
object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter,
198 const ALLOC& allocator = ALLOC())
220 template <
typename T,
typename ALLOC =
typename T::allocator_type,
221 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
248 template <
typename T,
typename ALLOC =
typename T::allocator_type,
249 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
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,
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,
313 const T&
object, uint8_t indent, WALK_FILTER&& walkFilter,
const ALLOC& allocator = ALLOC())
333 template <
typename T,
typename ALLOC =
typename T::allocator_type,
334 typename std::enable_if<is_allocator<ALLOC>::value,
int>::type = 0>
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())
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,
395 const ALLOC& allocator = ALLOC())
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,
426 const ALLOC& allocator = ALLOC())
455 template <
typename ALLOC = std::allocator<u
int8_t>>
460 return jsonReader.
read(typeInfo);
487 template <
typename T,
typename ALLOC =
typename T::allocator_type>
490 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonStream(T::typeInfo(), is, allocator)));
517 template <
typename ALLOC = std::allocator<u
int8_t>>
549 template <
typename T,
typename ALLOC =
typename T::allocator_type>
552 return std::move(ReflectableUtil::getValue<T, ALLOC>(
fromJsonString(T::typeInfo(), json, allocator)));
578 template <
typename ALLOC = std::allocator<u
int8_t>>
579 typename detail::DebugStringTraits<ALLOC>::ReflectablePtr
fromJsonFile(
582 std::ifstream is = std::ifstream(fileName.c_str());
585 throw CppRuntimeException(
"DebugStringUtil.fromJsonFile: Failed to open '") << fileName +
"'!";
614 template <
typename T,
typename ALLOC =
typename T::allocator_type>
617 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())