aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-json.cxx
blob: 7551fa12b1a1f0ad5383e86fefc0ff9f0e37a100 (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
234
235
236
237
238
239
240
// file      : libbuild2/functions-json.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/function.hxx>
#include <libbuild2/variable.hxx>

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

using namespace std;

namespace build2
{
  static size_t
  find_index (const json_value& a, value v)
  {
    if (a.type != json_type::array)
      fail << "expected json array instead of " << to_string (a.type)
           << " as first argument";

    auto b (a.array.begin ()), e (a.array.end ());
    auto i (find (b, e, convert<json_value> (move (v))));
    return i != e ? i - b : a.array.size ();
  };

  void
  json_functions (function_map& m)
  {
    function_family f (m, "json");

    // $value_type(<json>[, <distinguish_numbers>])
    //
    // Return the type of a JSON value: `null`, `boolean`, `number`, `string`,
    // `array`, or `object`. If the <distinguish_numbers> argument is `true`,
    // then instead of `number` return `signed number`, `unsigned number`, or
    // `hexadecimal number`.
    //
    f["value_type"] += [] (json_value v, optional<value> distinguish_numbers)
    {
      bool dn (distinguish_numbers &&
               convert<bool> (move (*distinguish_numbers)));

      return to_string (v.type, dn);
    };

    // $member_name(<json>)
    //
    // Return the name of a JSON object member.
    //
    f["member_name"] += [] (json_value v)
    {
      // A member becomes an object with a single member (see json_reverse()
      // for details).
      //
      if (v.type == json_type::object && v.object.size () == 1)
        return move (v.object.front ().name);

      fail << "json object member expected instead of " << v.type << endf;
    };

    // $member_value(<json>)
    //
    // Return the value of a JSON object member.
    //
    f["member_value"] += [] (json_value v)
    {
      // A member becomes an object with a single member (see json_reverse()
      // for details).
      //
      if (v.type == json_type::object && v.object.size () == 1)
        return move (v.object.front ().value);

      fail << "json object member expected instead of " << v.type << endf;
    };

    // $size(<json>)
    //
    // Return the size of a JSON value.
    //
    // The size of a `null` value is `0`. The sizes of simple values
    // (`boolean`, `number`, and `string`) is `1`. The size of `array` and
    // `object` values is the number of elements and members, respectively.
    //
    // Note that the size of a `string` JSON value is not the length of the
    // string. To get the length call `$string.size()` instead by casting the
    // JSON value to the `string` value type.
    //
    f["size"] += [] (json_value v) -> size_t
    {
      // Note: should be consistent with value_traits<json_value>::empty(),
      //       json_subscript().
      //
      switch (v.type)
      {
      case json_type::null:               return 0;
      case json_type::boolean:
      case json_type::signed_number:
      case json_type::unsigned_number:
      case json_type::hexadecimal_number:
      case json_type::string:             break;
      case json_type::array:              return v.array.size ();
      case json_type::object:             return v.object.size ();
      }

      return 1;
    };

    // $find(<json-array>, <json>)
    //
    // Return true if the JSON array contains the specified JSON value.
    //
    f["find"] += [](json_value a, value v)
    {
      size_t i (find_index (a, move (v)));
      return i != a.array.size (); // We now know it's an array.
    };

    // $find_index(<json-array>, <json>)
    //
    // Return the index of the first element in the JSON array that is equal
    // to the specified JSON value or `$size(json-array)` if none is found.
    //
    f["find_index"] += [](json_value a, value v)
    {
      return find_index (a, move (v));
    };

#ifndef BUILD2_BOOTSTRAP

    // @@ Flag to support multi-value (returning it as JSON array)? Then
    //    probably also in $serialize().
    //
    // @@ Flag to override duplicates instead of failing?

    // $json.load(<path>)
    //
    // Parse the contents of the specified file as JSON input text and return
    // the result as a value of the `json` type.
    //
    // See also `$json.parse()`.
    //
    // Note that this function is not pure.
    //
    f.insert (".load", false) += [] (names xf)
    {
      path f (convert<path> (move (xf)));

      try
      {
        ifdstream is (f);
        json_parser p (is, f.string ());
        return json_value (p);
      }
      catch (const invalid_json_input& e)
      {
        fail (location (f, e.line, e.column)) << "invalid json input: " << e <<
          info << "byte offset " << e.position << endf;
      }
      catch (const io_error& e)
      {
        fail << "unable to read from " << f << ": " << e << endf;
      }
    };

    // $json.parse(<text>)
    //
    // Parse the specified JSON input text and return the result as a value of
    // the `json` type.
    //
    // See also `$json.load()` and `$json.serialize()`.
    //
    f[".parse"] += [] (names text)
    {
      string t (convert<string> (move (text)));

      try
      {
        json_parser p (t, nullptr /* name */);
        return json_value (p);
      }
      catch (const invalid_json_input& e)
      {
        fail << "invalid json input: " << e <<
          info << "line " << e.line
               << ", column " << e.column
               << ", byte offset " << e.position << endf;
      }
    };

    // $serialize(<json>[, <indentation>])
    //
    // Serialize the specified JSON value and return the resulting JSON output
    // text.
    //
    // The optional <indentation> argument specifies the number of indentation
    // spaces that should be used for pretty-printing. If `0` is passed, then
    // no pretty-printing is performed. The default is `2` spaces.
    //
    // See also `$json.parse()`.
    //
    f["serialize"] += [] (json_value v, optional<value> indentation)
    {
      uint64_t i (indentation ? convert<uint64_t> (*indentation) : 2);

      try
      {
        // For the diagnostics test.
        //
#if 0
        if (v.type == json_type::string && v.string == "deadbeef")
        {
          v.string[4] = 0xe0;
          v.string[5] = 0xe0;
        }
#endif

        string o;
        json_buffer_serializer s (o, i);
        v.serialize (s);
        return o;
      }
      catch (const invalid_json_output& e)
      {
        diag_record dr;
        dr << fail << "invalid json value: " << e;

        if (e.event)
          dr << info << "while serializing " << to_string (*e.event);

        if (e.offset != string::npos)
          dr << info << "offending byte offset " << e.offset;

        dr << endf;
      }
    };
#endif
  }
}