Cthulhu  0.2.10
Cthulhu compiler collection
json.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "json/json.hpp"
4 
5 #include "json/json.h"
6 
7 #include "base/panic.h"
8 #include "notify/notify.h"
9 #include "std/vector.h"
10 
11 using namespace ctu;
12 using namespace ctu::json;
13 
14 Json Object::get(const char *key) const { return (json_t*)map_get(m_object, key); }
15 Json Object::operator[](const char *key) const { return get(key); }
16 
17 ObjectIterator Object::begin() const { return map_iter(m_object); }
18 ObjectIterator Object::end() const { return {}; }
19 
20 ObjectIterator Object::iter() const { return begin(); }
21 
23 {
24  return map_has_next(&m_iter);
25 }
26 
28 {
29  m_entry = map_next(&m_iter);
30  return *this;
31 }
32 
34 {
35  const text_view_t *key = (const text_view_t*)m_entry.key;
36  std::string_view view { key->text, key->length };
37  return { view, (json_t*)m_entry.value };
38 }
39 
40 bool ObjectIterator::has_next() const
41 {
42  return map_has_next(&m_iter);
43 }
44 
45 member_t ObjectIterator::next()
46 {
47  m_entry = map_next(&m_iter);
48  const text_view_t *key = (const text_view_t*)m_entry.key;
49  std::string_view view { key->text, key->length };
50  return { view, (json_t*)m_entry.value };
51 }
52 
53 Json Array::get(size_t index) const { return (json_t*)typevec_offset(&m_array, index); }
54 Json Array::operator[](size_t index) const { return get(index); }
55 size_t Array::length() const { return typevec_len(&m_array); }
56 
57 ArrayIterator Array::begin() const { return { m_array, 0 }; }
58 ArrayIterator Array::end() const { return { m_array, typevec_len(&m_array) }; }
59 
60 bool ArrayIterator::operator!=(const ArrayIterator &other) const { return m_index != other.m_index; }
61 ArrayIterator &ArrayIterator::operator++() { m_index++; return *this; }
62 Json ArrayIterator::operator*() const { return (json_t*)typevec_offset(&m_array, m_index); }
63 
64 bool Json::is_string() const { return is_kind(eJsonString); }
65 bool Json::is_integer() const { return is_kind(eJsonInteger); }
66 bool Json::is_float() const { return is_kind(eJsonFloat); }
67 bool Json::is_bool() const { return is_kind(eJsonBoolean); }
68 bool Json::is_array() const { return is_kind(eJsonArray); }
69 bool Json::is_object() const { return is_kind(eJsonObject); }
70 bool Json::is_null() const { return is_kind(eJsonNull); }
71 
72 bool Json::is_kind(json_kind_t kind) const { return get_kind() == kind; }
73 
75  CTASSERT(is_valid());
76  return m_ast->kind;
77 }
78 
79 std::string_view Json::as_string() const {
81  text_view_t *string = &m_ast->string;
82  return { string->text, string->length };
83 }
84 
85 void Json::as_integer(mpz_t integer) const {
87  mpz_init_set(integer, m_ast->integer);
88 }
89 
90 float Json::as_float() const {
91  CTASSERT(is_float());
92  return m_ast->real;
93 }
94 
95 bool Json::as_bool() const {
96  CTASSERT(is_bool());
97  return m_ast->boolean;
98 }
99 
101  CTASSERT(is_array());
102  return { m_ast->array };
103 }
104 
106  CTASSERT(is_object());
107  return { m_ast->object };
108 }
109 
110 Json Json::get(const char *key) const {
111  CTASSERT(is_object());
112  return json_map_get(m_ast, key);
113 }
114 
115 Json Json::get(size_t index) const {
116  CTASSERT(is_array());
117  return (json_t*)typevec_offset(&m_ast->array, index);
118 }
119 
120 size_t Json::length() const {
121  CTASSERT(is_array());
122  return typevec_len(&m_ast->array);
123 }
124 
125 Json Json::operator[](const char *key) const {
126  return get(key);
127 }
128 
129 Json Json::operator[](size_t index) const {
130  return get(index);
131 }
132 
134  : m_arena(arena)
135  , m_logger(logger_new(arena))
136 { }
137 
139  return json_scan(io, m_logger, m_arena);
140 }
an iterator over the values of a json array
Definition: json.hpp:336
Json operator*() const
Definition: json.cpp:62
ArrayIterator & operator++()
Definition: json.cpp:61
bool operator!=(const ArrayIterator &other) const
Definition: json.cpp:60
a json array value
Definition: json.hpp:69
Json operator[](size_t index) const
get a value same as get
Definition: json.cpp:54
Json get(size_t index) const
get a value access a value from the array by index
Definition: json.cpp:53
ArrayIterator begin() const
start iterating iterate over the values of the array
Definition: json.cpp:57
size_t length() const
get the length of the array
Definition: json.cpp:55
ArrayIterator end() const
end iterating
Definition: json.cpp:58
JsonParser(arena_t *arena)
create a json parser
Definition: json.cpp:133
Json parse(io_t *io)
parse a json value parse the contents of an io object into a json value
Definition: json.cpp:138
a json value a json node from a parsed json document
Definition: json.hpp:118
CT_PUREFN bool is_object() const
check if the value is an object
Definition: json.cpp:69
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
CT_PUREFN Object as_object() const
get the object value
Definition: json.cpp:105
CT_PUREFN bool is_float() const
check if the value is a float
Definition: json.cpp:66
CT_PUREFN std::string_view as_string() const
get the string value
Definition: json.cpp:79
void as_integer(mpz_t integer) const
get the integer value
Definition: json.cpp:85
CT_PUREFN bool is_null() const
check if the value is null
Definition: json.cpp:70
CT_PUREFN Array as_array() const
get the array value
Definition: json.cpp:100
CT_PUREFN bool as_bool() const
get the boolean value
Definition: json.cpp:95
Json operator[](const char *key) const
get a value from an object by key same as get(const char*)
Definition: json.cpp:125
Json get(const char *key) const
get a value from an object by key
Definition: json.cpp:110
CT_PUREFN bool is_bool() const
check if the value is a boolean
Definition: json.cpp:67
CT_PUREFN bool is_array() const
check if the value is an array
Definition: json.cpp:68
CT_PUREFN json_kind_t get_kind() const
get the kind of the value
Definition: json.cpp:74
CT_PUREFN bool is_integer() const
check if the value is an integer
Definition: json.cpp:65
CT_PUREFN bool is_string() const
check if the value is a string
Definition: json.cpp:64
CT_PUREFN float as_float() const
get the float value
Definition: json.cpp:90
CT_PUREFN size_t length() const
get the length of an array
Definition: json.cpp:120
CT_PUREFN bool is_kind(json_kind_t kind) const
check if the value is a specific kind
Definition: json.cpp:72
an iterator over the members of a json object
Definition: json.hpp:312
ObjectIterator & operator++()
Definition: json.cpp:27
member_t operator*() const
Definition: json.cpp:33
bool operator!=(const ObjectIterator &other) const
Definition: json.cpp:22
a json object value
Definition: json.hpp:26
Json get(const char *key) const
get a value access a value from the object by key
Definition: json.cpp:14
Json operator[](const char *key) const
get a value same as get
Definition: json.cpp:15
ObjectIterator begin() const
start iterating iterate over the members of the object
Definition: json.cpp:17
ObjectIterator iter() const
Definition: json.cpp:20
ObjectIterator end() const
end iterating
Definition: json.cpp:18
CT_NODISCARD CT_PUREFN CT_STD_API bool map_has_next(const map_iter_t *iter)
check if a map iterator has more elements
Definition: map.c:509
CT_NODISCARD CT_PUREFN CT_STD_API map_iter_t map_iter(const map_t *map)
create a new map iterator
Definition: map.c:456
CT_NODISCARD CT_PUREFN CT_STD_API void * map_get(const map_t *map, const void *key)
get a value from a map
Definition: map.c:324
CT_NODISCARD CT_NOALIAS CT_STD_API map_entry_t map_next(map_iter_t *iter)
get the next key-value pair from a map iterator
Definition: map.c:476
json_kind_t
the kind of json value
Definition: json.h:34
CT_JSON_API json_t * json_map_get(const json_t *json, const char *key)
get a json value from an object by key
Definition: json.c:17
CT_JSON_API json_t * json_scan(io_t *io, logger_t *logger, arena_t *arena)
scan an io into a json value scan the contents of an io object into a json value
Definition: json.c:36
CT_NODISCARD CT_NOTIFY_API logger_t * logger_new(arena_t *arena)
create a new logger
Definition: notify.c:21
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
CT_NODISCARD CT_PUREFN CT_STD_API size_t typevec_len(const typevec_t *vec)
get the length of a vector
Definition: vector.c:120
CT_NODISCARD CT_PUREFN CT_STD_API void * typevec_offset(const typevec_t *vec, size_t index)
get a pointer to the value at the given index
Definition: vector.c:191
Definition: io.hpp:7
an allocator object
Definition: arena.h:86
a member of a json object
Definition: json.hpp:303
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
text_view_t string
the string value of this node
Definition: json.h:55
typevec_t array
the array value of this node
Definition: json.h:71
bool boolean
the boolean value of this node
Definition: json.h:67
json_kind_t kind
the kind of json value
Definition: json.h:45
const map_t * object
the object value of this node
Definition: json.h:75
float real
the float value of this node
Definition: json.h:63
const void * key
the key of this entry
Definition: map.h:176
a non-owning view of text
Definition: text.h:24
size_t length
the number of characters in the text
Definition: text.h:30