aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/json.cxx
blob: 0fee8af12cd433b34781994c114f5cf7e95b06e2 (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
// file      : libbuild2/json.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/json.hxx>

#ifndef BUILD2_BOOTSTRAP
#  include <libbutl/json/parser.hxx>
#  include <libbutl/json/serializer.hxx>
#endif

using namespace butl::json;

namespace build2
{
#ifndef BUILD2_BOOTSTRAP
  json_value::
  json_value (parser& p)
  {
    // A JSON input text cannot be empty.
    //
    event e (*p.next ());

    switch (e)
    {
    case event::begin_object:
      {
        container_type c; // For exception safety.
        while (*p.next () != event::end_object)
        {
          string_type n (p.name ());
          json_value v (p);
          v.name = move (n);
          c.push_back (move (v));
        }

        new (&container) container_type (move (c));
        type = json_type::object;
        break;
      }
    case event::begin_array:
      {
        container_type c; // For exception safety.
        while (*p.peek () != event::end_array)
          c.push_back (json_value (p));
        p.next (); // Consume end_array.

        new (&container) container_type (move (c));
        type = json_type::array;
        break;
      }
    case event::string:
      {
        string_type& s (p.value ());

        // Don't move if small string optimized.
        //
        if (s.size () > 15)
          new (&string) string_type (move (s));
        else
          new (&string) string_type (s);

        type = json_type::string;
        break;
      }
    case event::number:
      {
        string_type& s (p.value ());

        if (s[0] == '-')
        {
          signed_number = p.value<int64_t> ();
          type = json_type::signed_number;
        }
        else
        {
          unsigned_number = p.value<uint64_t> ();
          type = json_type::unsigned_number;
        }

        break;
      }
    case event::boolean:
      {
        boolean = p.value<bool> ();
        type = json_type::boolean;
        break;
      }
    case event::null:
      {
        type = json_type::null;
        break;
      }
    case event::name:
    case event::end_array:
    case event::end_object:
      {
        assert (false);
        type = json_type::null;
        break;
      }
    }
  }

  void
  serialize (buffer_serializer& s, const json_value& v)
  {
    if (v.name)
      s.member_name (*v.name);

    switch (v.type)
    {
    case json_type::null:
      {
        s.value (nullptr);
        break;
      }
    case json_type::boolean:
      {
        s.value (v.boolean);
        break;
      }
    case json_type::signed_number:
      {
        s.value (v.signed_number);
        break;
      }
    case json_type::unsigned_number:
      {
        s.value (v.unsigned_number);
        break;
      }
    case json_type::string:
      {
        s.value (v.string);
        break;
      }
    case json_type::array:
      {
        s.begin_array ();
        for (const json_value& e: v.container)
          serialize (s, e);
        s.end_array ();
        break;
      }
    case json_type::object:
      {
        s.begin_object ();
        for (const json_value& m: v.container)
          serialize (s, m);
        s.end_object ();
        break;
      }
    }
  }
#else
  json_value::
  json_value (parser&)
  {
    assert (false);
    type = json_type::null;
  }

  void
  serialize (buffer_serializer&, const json_value&)
  {
    assert (false);
  }
#endif
}