boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invoke(
    boost::json::try_value_to_tag<{{struct.full_name}}> /* tag */, const boost::json::value &{%- if struct.members|length or struct.base -%}jvRoot{%- else -%}/* jvRoot */{%- endif -%})
{
    {% if struct.inner_root %}
    if (!jvRoot.is_object())
    {
        EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
    }
    const auto &outerRoot = jvRoot.get_object();

    const auto *jvInnerRoot = outerRoot.if_contains("{{struct.inner_root}}");
    if (jvInnerRoot == nullptr)
    {
        EVENTSUB_BAIL_HERE(error::Kind::InnerRootMissing);
    }
    if (!jvInnerRoot->is_object())
    {
        EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
    }
    const auto &root = jvInnerRoot->get_object();
    {% elif struct.members|length %}
    if (!jvRoot.is_object())
    {
        EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
    }
    const auto &root = jvRoot.get_object();
    {% elif struct.base %}
    auto base = boost::json::try_value_to<{{struct.base}}>(jvRoot);
    if (base.has_error())
    {
        return base.error();
    }
    {% endif %}

{% for field in struct.members %}
    {% if field.trivial -%}
    static_assert(std::is_trivially_copyable_v<std::remove_reference_t<decltype(std::declval<{{struct.full_name}}>().{{field.name}})>>);
    {%- endif -%}
    {% if field.member_type == MemberType.BASIC -%}
    {% include 'field-basic.tmpl' indent content %}
    {%- elif field.member_type == MemberType.VECTOR -%}
    {% include 'field-vector.tmpl' indent content %}
    {%- elif field.member_type == MemberType.OPTIONAL -%}
    {% include 'field-optional.tmpl' indent content %}
    {%- elif field.member_type == MemberType.OPTIONAL_VECTOR -%}
    {% include 'field-optional-vector.tmpl' indent content %}
    {%- elif field.member_type == MemberType.VARIANT -%}
    {% include 'field-variant.tmpl' indent content %}
    {%- endif -%}
{% endfor %}

    return {{struct.full_name}}{
{%- for field in struct.members %}
        {% if field.member_type == MemberType.BASIC -%}
        {% include 'initializer-basic.tmpl' indent content %}
        {%- elif field.member_type == MemberType.VECTOR -%}
        {% include 'initializer-vector.tmpl' indent content %}
        {%- elif field.member_type == MemberType.OPTIONAL -%}
        {% include 'initializer-optional.tmpl' indent content %}
        {%- elif field.member_type == MemberType.OPTIONAL_VECTOR -%}
        {% include 'initializer-optional-vector.tmpl' indent content %}
        {%- elif field.member_type == MemberType.VARIANT -%}
        {% include 'initializer-variant.tmpl' indent content %}
        {%- endif -%}
{% endfor -%}
{%- if struct.base %}
        std::move(*base)
{% endif -%}
    };
}
