Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for optionals #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/json5/json5_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ inline error parser::parse_value( value &result )
else if ( lit == token_type::literal_false )
result = value( false );
else if ( lit == token_type::literal_null )
result = value();
result = value(nullptr);
else
return make_error( error::invalid_literal );
}
Expand Down
101 changes: 71 additions & 30 deletions include/json5/json5_reflect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ template <typename T> error from_file( std::string_view fileName, T &out );

namespace detail {

template <typename T> struct is_optional : public std::false_type { };

template <typename T> struct is_optional<std::optional<T>> : public std::true_type { };

template <typename T> constexpr static bool is_optional_v = is_optional<T>::value;

// Pre-declare so compiler knows it exists before it is attempted to be used
template <typename T> error read(const json5::value& in, T& out);

Expand Down Expand Up @@ -149,6 +155,26 @@ inline json5::value write_enum( writer &w, T in )
return json5::value();
}

//---------------------------------------------------------------------------------------------------------------------
template <typename Type>
inline void write_tuple_value(writer& w, std::string_view name, const Type& in)
{
if constexpr (is_optional_v<Type>)
{
if (in)
write_tuple_value<typename Type::value_type>(w, name, *in);
}
else if constexpr (std::is_enum_v<Type>)
{
if constexpr ( enum_table<Type>() )
w[name] = write_enum( w, in );
else
w[name] = write( w, std::underlying_type_t<Type>( in ) );
}
else
w[name] = write( w, in );
}

//---------------------------------------------------------------------------------------------------------------------
template <size_t Index = 0, typename... Types>
inline void write_tuple( writer &w, const char *names, const std::tuple<Types...> &t )
Expand All @@ -158,15 +184,7 @@ inline void write_tuple( writer &w, const char *names, const std::tuple<Types...

if ( auto name = get_name_slice( names, Index ); !name.empty() )
{
if constexpr ( std::is_enum_v<Type> )
{
if constexpr ( enum_table<Type>() )
w[name] = write_enum( w, in );
else
w[name] = write( w, std::underlying_type_t<Type>( in ) );
}
else
w[name] = write( w, in );
write_tuple_value<Type>(w, name, in);
}

if constexpr ( Index + 1 != sizeof...( Types ) )
Expand Down Expand Up @@ -349,6 +367,48 @@ inline error read_enum( const json5::value &in, T &out )
return { error::invalid_enum };
}

//---------------------------------------------------------------------------------------------------------------------
template <typename Type>
inline error read_tuple_value( const json5::object_view::iterator iter, Type &out )
{
if constexpr (is_optional_v<Type>)
{
if ((*iter).second.is_null())
{
out = std::nullopt;
}
else
{
out = typename Type::value_type();
if (auto err = read_tuple_value<typename Type::value_type>(iter, *out))
return err;
}
}
else if constexpr (std::is_enum_v<Type>)
{
if constexpr ( enum_table<Type>() )
{
if ( auto err = read_enum( ( *iter ).second, out ) )
return err;
}
else
{
std::underlying_type_t<Type> temp;
if ( auto err = read( ( *iter ).second, temp ) )
return err;

out = Type( temp );
}
}
else
{
if ( auto err = read( ( *iter ).second, out ) )
return err;
}

return { error::none };
}

//---------------------------------------------------------------------------------------------------------------------
template <size_t Index = 0, typename... Types>
inline error read_tuple( const json5::object_view &obj, const char *names, std::tuple<Types...> &t )
Expand All @@ -361,27 +421,8 @@ inline error read_tuple( const json5::object_view &obj, const char *names, std::
auto iter = obj.find( name );
if ( iter != obj.end() )
{
if constexpr ( std::is_enum_v<Type> )
{
if constexpr ( enum_table<Type>() )
{
if ( auto err = read_enum( ( *iter ).second, out ) )
return err;
}
else
{
std::underlying_type_t<Type> temp;
if ( auto err = read( ( *iter ).second, temp ) )
return err;

out = Type( temp );
}
}
else
{
if ( auto err = read( ( *iter ).second, out ) )
return err;
}
if ( auto err = read_tuple_value<Type>( iter, out ) )
return err;
}

if constexpr ( Index + 1 != sizeof...( Types ) )
Expand Down