#ifndef {{model.namespace|replace(".","_")|upper}}_{{model.name|upper}}_NLJS_HPP
#define {{model.namespace|replace(".","_")|upper}}_{{model.name|upper}}_NLJS_HPP

#include <nlohmann/json.hpp>
{% set pkgincdir = model.pkgincdir+"/" if model.pkgincdir is defined else "" %}
{% set structinc = model.structinc if model.structinc is defined else model.name +"_structs.hpp" %}
#include "{{pkgincdir}}{{structinc}}"

namespace {{ model.namespace }} {

    using json = nlohmann::json;

    {% for t in model.types %}
    {% if t.type == "enum" %}
    NLOHMANN_JSON_SERIALIZE_ENUM( {{t.name}}, {
            {% for sname in t.symbols %}
            { {{model.namespace}}::{{t.name}}::{{sname}}, "{{sname}}" },
            {% endfor %}
        })
    {% endif %}
    {% endfor %}

    {% for t in model.types %}
    {% if t.type == "record" %}
    // Converters for {{t.name}}
    // {{t.doc}}
    inline void to_json(json& j, const {{t.name}}& obj) {
        {% for b in t.fields if b.name.startswith("_base_") %}
        to_json(j, (const {{b.type}}&)obj);
        {% endfor %}
        {% for f in t.fields if not f.name.startswith("_base_") %}
        j["{{f.name}}"] = obj.{{f.name}};
        {% endfor%}
    }
    inline void from_json(const json& j, {{t.name}}& obj) {
        {% for b in t.fields if b.name.startswith("_base_") %}
        from_json(j, ({{b.type}}&)obj);
        {% endfor %}
        {% for f in t.fields if not f.name.startswith("_base_") %}
        if (j.contains("{{f.name}}"))
            j.at("{{f.name}}").get_to(obj.{{f.name}});    
        {% endfor%}
    }
    {% endif %}
    {% endfor %}


} // namespace {{model.namespace}}

#endif
