@@ -37,6 +37,12 @@ template <typename T> error from_file( std::string_view fileName, T &out );
37
37
38
38
namespace detail {
39
39
40
+ template <typename T> struct is_optional : public std ::false_type { };
41
+
42
+ template <typename T> struct is_optional <std::optional<T>> : public std::true_type { };
43
+
44
+ template <typename T> constexpr static bool is_optional_v = is_optional<T>::value;
45
+
40
46
// Pre-declare so compiler knows it exists before it is attempted to be used
41
47
template <typename T> error read (const json5::value& in, T& out);
42
48
@@ -153,7 +159,12 @@ inline json5::value write_enum( writer &w, T in )
153
159
template <typename Type>
154
160
inline void write_tuple_value (writer& w, std::string_view name, const Type& in)
155
161
{
156
- if constexpr ( std::is_enum_v<Type> )
162
+ if constexpr (is_optional_v<Type>)
163
+ {
164
+ if (in)
165
+ write_tuple_value<typename Type::value_type>(w, name, *in);
166
+ }
167
+ else if constexpr (std::is_enum_v<Type>)
157
168
{
158
169
if constexpr ( enum_table<Type>() )
159
170
w[name] = write_enum ( w, in );
@@ -360,7 +371,13 @@ inline error read_enum( const json5::value &in, T &out )
360
371
template <typename Type>
361
372
inline error read_tuple_value ( const json5::object_view::iterator iter, Type &out )
362
373
{
363
- if constexpr ( std::is_enum_v<Type> )
374
+ if constexpr (is_optional_v<Type>)
375
+ {
376
+ out = typename Type::value_type ();
377
+ if (auto err = read_tuple_value<typename Type::value_type>(iter, *out))
378
+ return err;
379
+ }
380
+ else if constexpr (std::is_enum_v<Type>)
364
381
{
365
382
if constexpr ( enum_table<Type>() )
366
383
{
0 commit comments