Files
Francisco Geiman Thiesen 90e4a66c93 Bringing a bit more use-cases for reflection based serializations (optional). Also adding string-based enum handling as requested on X. (#2395)
* Adding type validation, enhancing optional type support and adding test a few more tests.

* Adding support for string-based enum serlalization and deserialization.

* Removing unintentional endline.

* Removing trailing whitespace.

* Adding simpler api as suggested by moisrex.

* Removing explicit optiona<int> and optional<std::string> references and using concepts instead! Credit goes to Lemire for pointing this out and suggesting a concepts based approach here.

* Removing tests that are not relevant for this branch.

* Removing api related changes. That will be done by moisrex.

* Removing unnecessary new endlines.

* Removing tests related to api changes and cleaning-up irrelevant tests.

* removing broken reference

* Removing trailing whitespace
2025-07-23 09:24:05 +02:00

40 lines
1.0 KiB
C++

#ifndef SIMDJSON_ONDEMAND_H
#define SIMDJSON_ONDEMAND_H
#include "simdjson/builtin/ondemand.h"
namespace simdjson {
/**
* @copydoc simdjson::builtin::ondemand
*/
namespace ondemand = builtin::ondemand;
/**
* @copydoc simdjson::builtin::builder
*/
namespace builder = builtin::builder;
#if SIMDJSON_STATIC_REFLECTION
/**
* Create a JSON string from any user-defined type using static reflection.
* Only available when SIMDJSON_STATIC_REFLECTION is enabled.
*/
template<typename T>
requires(!std::same_as<T, builtin::ondemand::document> &&
!std::same_as<T, builtin::ondemand::value> &&
!std::same_as<T, builtin::ondemand::object> &&
!std::same_as<T, builtin::ondemand::array>)
inline std::string to_json_string(const T& obj) {
builder::string_builder str_builder;
append(str_builder, obj);
std::string_view view;
if (str_builder.view().get(view) == SUCCESS) {
return std::string(view);
}
return "";
}
#endif
} // namespace simdjson
#endif // SIMDJSON_ONDEMAND_H