aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/json.hxx
blob: 816b55caf703b34bb47376e1487d616a1fe7d4ae (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// file      : libbuild2/json.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBUILD2_JSON_HXX
#define LIBBUILD2_JSON_HXX

#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/export.hxx>

namespace butl
{
  namespace json
  {
    class parser;
    class buffer_serializer;
  }
}

namespace build2
{
  // This JSON representation has two extensions compared to the standard JSON
  // model: it distinguishes between signed and unsigned numbers and
  // represents an object member as a JSON value rather than, say, a pair of a
  // string and value. The latter allows us to use the JSON value itself as an
  // element of a container.
  //
  enum class json_type
  {
    null,
    boolean,
    signed_number,
    unsigned_number,
    string,
    array,
    object,
  };

  class LIBBUILD2_SYMEXPORT json_value
  {
  public:
    using string_type = build2::string;
    using container_type = vector<json_value>;

    json_type type;

    optional<string_type> name; // If present, then this is a member with value.

    union
    {
      bool           boolean;
      int64_t        signed_number;
      uint64_t       unsigned_number;
      string_type    string;
      container_type container; // arrary and object
    };

    // Throws invalid_json_input.
    //
    explicit
    json_value (butl::json::parser&);

    explicit
    json_value (json_type t = json_type::null)
        : type (t)
    {
      switch (type)
      {
      case json_type::null:                                          break;
      case json_type::boolean:         boolean = false;              break;
      case json_type::signed_number:   signed_number = 0;            break;
      case json_type::unsigned_number: unsigned_number = 0;          break;
      case json_type::string:          new (&string) string_type (); break;
      case json_type::array:
      case json_type::object:          new (&container) container_type (); break;
      }
    }

    json_value (string_type member_name, json_type t)
        : json_value (t) {name = move (member_name);}

    explicit
    json_value (std::nullptr_t)
        : type (json_type::null) {}

    json_value (string_type member_name, std::nullptr_t v)
        : json_value (v) {name = move (member_name);}

    explicit
    json_value (bool v)
        : type (json_type::boolean), boolean (v) {}

    json_value (string_type member_name, bool v)
        : json_value (v) {name = move (member_name);}

    explicit
    json_value (int64_t v)
        : type (json_type::signed_number), signed_number (v) {}

    json_value (string_type member_name, int64_t v)
        : json_value (v) {name = move (member_name);}

    explicit
    json_value (uint64_t v)
        : type (json_type::unsigned_number), unsigned_number (v) {}

    json_value (string_type member_name, uint64_t v)
        : json_value (v) {name = move (member_name);}

    explicit
    json_value (string_type v)
        : type (json_type::string), string (move (v)) {}

    json_value (string_type member_name, string_type v)
        : json_value (move (v)) {name = move (member_name);}

    explicit
    json_value (container_type v, json_type t)
        : type (t), container (move (v))
    {
#ifndef NDEBUG
      assert (t == json_type::array || t == json_type::object);

      for (const json_value& e: container)
        assert (e.name.has_value () == (t == json_type::object));
#endif
    }

    json_value (string_type member_name, container_type v, json_type t)
        : json_value (move (v), t) {name = move (member_name);}

    // Note that the moved-from value becomes null.
    //
    json_value (json_value&& v) noexcept
      : type (v.type), name (move (v.name))
    {
      switch (type)
      {
      case json_type::null:
        break;
      case json_type::boolean:
        boolean = v.boolean;
        break;
      case json_type::signed_number:
        signed_number = v.signed_number;
        break;
      case json_type::unsigned_number:
        unsigned_number = v.unsigned_number;
        break;
      case json_type::string:
        new (&string) string_type (move (v.string));
        v.string.~string_type ();
        break;
      case json_type::array:
      case json_type::object:
        new (&container) container_type (move (v.container));
        v.container.~container_type ();
        break;
      }

      v.type = json_type::null;
      v.name = nullopt;
    }

    json_value& operator= (json_value&& v) noexcept
    {
      if (this != &v)
      {
        this->~json_value ();
        new (this) json_value (move (v));
      }
      return *this;
    }

    json_value (const json_value& v)
        : type (v.type), name (v.name)
    {
      switch (type)
      {
      case json_type::null:
        break;
      case json_type::boolean:
        boolean = v.boolean;
        break;
      case json_type::signed_number:
        signed_number = v.signed_number;
        break;
      case json_type::unsigned_number:
        unsigned_number = v.unsigned_number;
        break;
      case json_type::string:
        new (&string) string_type (v.string);
        break;
      case json_type::array:
      case json_type::object:
        new (&container) container_type (v.container);
        break;
      }
    }

    json_value& operator= (const json_value& v)
    {
      if (this != &v)
      {
        this->~json_value ();
        new (this) json_value (v);
      }
      return *this;
    }

    ~json_value () noexcept
    {
      switch (type)
      {
      case json_type::null:
      case json_type::boolean:
      case json_type::signed_number:
      case json_type::unsigned_number:                               break;
      case json_type::string:          string.~string_type ();       break;
      case json_type::array:
      case json_type::object:          container.~container_type (); break;
      }
    }
  };

  // Throws invalid_json_output.
  //
  LIBBUILD2_SYMEXPORT void
  serialize (butl::json::buffer_serializer&, const json_value&);
}

#endif // LIBBUILD2_JSON_HXX