Cthulhu  0.2.10
Cthulhu compiler collection
json.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "json/json.h"
4 
5 #include "std/map.h"
6 
7 #include "base/panic.h"
8 
9 #include <string_view>
10 
11 typedef struct logger_t logger_t;
12 typedef struct io_t io_t;
13 
16 
17 namespace ctu::json {
18  class Json;
19  class Object;
20  class Array;
21 
22  class ObjectIterator;
23  class ArrayIterator;
24 
26  class CT_JSON_API Object {
27  friend class Json;
28 
29  const map_t *m_object;
30 
31  constexpr Object(const map_t *object)
32  : m_object(object)
33  { }
34 
35  public:
43  Json get(const char *key) const;
44 
51  Json operator[](const char *key) const;
52 
58  ObjectIterator begin() const;
59 
63  ObjectIterator end() const;
64 
65  ObjectIterator iter() const;
66  };
67 
69  class CT_JSON_API Array {
70  friend class Json;
71 
72  const typevec_t m_array;
73 
74  constexpr Array(typevec_t array)
75  : m_array(array)
76  { }
77 
78  public:
86  Json get(size_t index) const;
87 
94  Json operator[](size_t index) const;
95 
99  size_t length() const;
100 
105  ArrayIterator begin() const;
106 
110  ArrayIterator end() const;
111  };
112 
118  class CT_JSON_API Json {
119  friend class JsonParser;
120  friend class Object;
121  friend class Array;
122  friend class ObjectIterator;
123  friend class ArrayIterator;
124 
125  json_t *m_ast;
126 
127  public:
128  constexpr Json(json_t *ast)
129  : m_ast(ast)
130  { }
131 
132  constexpr Json()
133  : m_ast(nullptr)
134  { }
135 
139  CT_PUREFN bool is_string() const;
140 
144  CT_PUREFN bool is_integer() const;
145 
149  CT_PUREFN bool is_float() const;
150 
154  CT_PUREFN bool is_bool() const;
155 
159  CT_PUREFN bool is_array() const;
160 
164  CT_PUREFN bool is_object() const;
165 
169  CT_PUREFN bool is_null() const;
170 
176  CT_PUREFN constexpr bool is_valid() const { return m_ast != nullptr; }
177 
183  constexpr operator bool() const { return is_valid(); }
184 
188  CT_PUREFN json_kind_t get_kind() const;
189 
196  CT_PUREFN bool is_kind(json_kind_t kind) const;
197 
203  CT_PUREFN std::string_view as_string() const;
204 
209  void as_integer(mpz_t integer) const;
210 
215  CT_PUREFN float as_float() const;
216 
221  CT_PUREFN bool as_bool() const;
222 
228  CT_PUREFN Array as_array() const;
229 
235  CT_PUREFN Object as_object() const;
236 
241  CT_PUREFN size_t length() const;
242 
249  Json get(const char *key) const;
250 
257  Json get(size_t index) const;
258 
262  constexpr json_t *get_ast() const { return m_ast; }
263 
270  Json operator[](const char *key) const;
271 
278  Json operator[](size_t index) const;
279 
287  template<typename F>
288  auto visit(F&& func) const {
289  switch (get_kind()) {
290  case eJsonString: return func(as_string());
291  case eJsonInteger: return func(m_ast->integer);
292  case eJsonFloat: return func(as_float());
293  case eJsonBoolean: return func(as_bool());
294  case eJsonArray: return func(as_array());
295  case eJsonObject: return func(as_object());
296  case eJsonNull: return func(nullptr);
297  default: CT_NEVER("invalid json kind %d", get_kind());
298  }
299  }
300  };
301 
303  struct CT_JSON_API member_t {
305  std::string_view key;
306 
309  };
310 
312  class CT_JSON_API ObjectIterator {
313  friend class Object;
314 
315  map_iter_t m_iter;
316  map_entry_t m_entry;
317 
318  constexpr ObjectIterator(map_iter_t iter)
319  : m_iter(iter)
320  { }
321 
322  constexpr ObjectIterator()
323  : m_iter()
324  { }
325 
326  public:
327  bool operator!=(const ObjectIterator& other) const;
328  ObjectIterator &operator++();
329  member_t operator*() const;
330 
331  bool has_next() const;
332  member_t next();
333  };
334 
336  class CT_JSON_API ArrayIterator {
337  friend class Array;
338 
339  typevec_t m_array;
340  size_t m_index;
341 
342  constexpr ArrayIterator(typevec_t array, size_t index)
343  : m_array(array)
344  , m_index(index)
345  { }
346 
347  public:
348  bool operator!=(const ArrayIterator& other) const;
349  ArrayIterator &operator++();
350  Json operator*() const;
351  };
352 
354  class CT_JSON_API JsonParser {
355  arena_t *m_arena;
356  logger_t *m_logger;
357 
358  public:
362  JsonParser(arena_t *arena);
363 
371  Json parse(io_t *io);
372 
376  logger_t *get_logger() const { return m_logger; }
377  };
378 }
379 
an iterator over the values of a json array
Definition: json.hpp:336
a json array value
Definition: json.hpp:69
a json parser
Definition: json.hpp:354
logger_t * get_logger() const
get the logger
Definition: json.hpp:376
a json value a json node from a parsed json document
Definition: json.hpp:118
constexpr CT_PUREFN bool is_valid() const
check if the value is valid this should always be checked before using any other methods
Definition: json.hpp:176
constexpr Json()
Definition: json.hpp:132
auto visit(F &&func) const
visit the value calls the appropriate function for the value kind
Definition: json.hpp:288
constexpr json_t * get_ast() const
get the underlying ast node
Definition: json.hpp:262
constexpr Json(json_t *ast)
Definition: json.hpp:128
an iterator over the members of a json object
Definition: json.hpp:312
a json object value
Definition: json.hpp:26
#define CT_PUREFN
mark a function as pure, always returns the same value for the same arguments
Definition: analyze.h:228
json_kind_t
the kind of json value
Definition: json.h:34
#define CT_NEVER(...)
assert that a code path is never reached
Definition: panic.h:136
an allocator object
Definition: arena.h:86
a member of a json object
Definition: json.hpp:303
Json value
the value of the member
Definition: json.hpp:308
std::string_view key
the key of the member
Definition: json.hpp:305
io object implementation
Definition: impl.h:122
a json value
Definition: json.h:43
mpz_t integer
the integer value of this node
Definition: json.h:59
a logging sink
Definition: notify.c:14
a key-value pair in a map
Definition: map.h:175
a map iterator handle
Definition: map.h:183
an unordered hash map
Definition: map.h:38
A vector with a fixed type size.
Definition: vector.h:24