summaryrefslogtreecommitdiff
path: root/templates/data.cpp.ede
blob: ac6012ce45acd25bbdf028556cdc6b7ed3ccb96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// generated file, do not modify!
// {{ dateTime }}

#include <string>
#include <iostream>

#include "{{ moduleName }}.hpp"

{% for t in definitions %}
template<> json toJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v) {
  json obj;
  switch (v.tag) { {% for c in t.value.constructors %}
    case ::{{ t.value.dataName }}::tag::{{ c.value.name }}:
      obj["tag"] = "{{ c.value.name }}";{% if !(c.value.fields | empty) %}
      {
        auto tv = static_cast<::data::{{ c.value.name }}&>(v);{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
        obj["{{ f.value.fieldName }}"] = toJSON(tv.{{ f.value.fieldName }});{% else %}
        obj["arg{{ f.index0 }}"] = toJSON(tv._{{ f.index0 }});{% endif %}{% endfor %}
      }{% endif %}
      break;{% endfor %}
  }
  return obj;
}

template<> {{ t.value.dataName }} fromJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v, json &obj) {
  std::cout << "fromJSON: {{ t.value.dataName }}\n";
  enum ::{{ t.value.dataName }}::tag tagType;
  std::string tag = obj["tag"];
  {% for c in t.value.constructors %}
  {% if !c.first %}else {% endif %}if (tag == "{{ c.value.name }}") {
    tagType = ::{{ t.value.dataName }}::tag::{{ c.value.name }};
    {% if !(c.value.fields | empty) %}
    ::data::{{ c.value.name }} tv;
    tv.tag = tagType;{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %}
    tv.{{ f.value.fieldName }} = fromJSON(tv.{{ f.value.fieldName }}, obj["{{ f.value.fieldName }}"]);{% else %}
    tv._{{ f.index0 }} = fromJSON(tv._{{ f.index0 }}, obj["arg{{ f.index0 }}"]);{% endif %}{% endfor %}
    return tv;
    {% endif %}
  }
{% endfor %}
  else throw "unknown constructor: " + tag;
  {{ t.value.dataName }} o;
  o.tag = tagType;
  return o;
}

{% endfor %}