Skip to content

Commit b34d892

Browse files
committed
Rename from constexpr_json to json2cpp, cache size()
1 parent 8de13fa commit b34d892

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

include/json2cpp/constexpr_json.hpp renamed to include/json2cpp/json2cpp.hpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ SOFTWARE.
3434
#include <stdexcept>
3535
#include <string_view>
3636

37-
namespace constexpr_json {
37+
namespace json2cpp {
3838
template<typename First, typename Second> struct pair
3939
{
4040
First first;
@@ -289,13 +289,19 @@ template<typename CharType> struct basic_json
289289

290290
[[nodiscard]] constexpr std::size_t size() const noexcept
291291
{
292-
if (is_null()) { return 0; }
293-
if (is_object()) { return data.get_if_object()->size(); }
294-
if (is_array()) { return data.get_if_array()->size(); }
292+
return size_;
293+
}
294+
295+
[[nodiscard]] static constexpr std::size_t size(const basic_json &obj) noexcept
296+
{
297+
if (obj.is_null()) { return 0; }
298+
if (obj.is_object()) { return obj.data.get_if_object()->size(); }
299+
if (obj.is_array()) { return obj.data.get_if_array()->size(); }
295300

296301
return 1;
297302
}
298303

304+
299305
[[nodiscard]] constexpr const basic_json &operator[](const std::size_t idx) const
300306
{
301307
if (const auto &children = array_data(); idx < children.size()) {
@@ -413,7 +419,9 @@ template<typename CharType> struct basic_json
413419
return is_null() || is_string() || is_boolean() || is_number() || is_binary();
414420
}
415421

422+
416423
data_t data;
424+
std::size_t size_{size(*this)};
417425
};
418426

419427
using json = basic_json<char>;

include/json2cpp/json2cpp_adapter.hpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ SOFTWARE.
4949

5050
#pragma once
5151

52-
#include <json2cpp/constexpr_json.hpp>
52+
#include <json2cpp/json2cpp.hpp>
5353
#include <string>
5454

5555
#include <utility>
@@ -96,7 +96,7 @@ namespace adapters {
9696
* Note that this constructor will throw an exception if the value is not
9797
* an array.
9898
*/
99-
explicit json2cppJsonArray(const constexpr_json::json &value) : m_value(value)
99+
explicit json2cppJsonArray(const json2cpp::json &value) : m_value(value)
100100
{
101101
if (!value.is_array()) { throwRuntimeError("Value is not an array."); }
102102
}
@@ -126,14 +126,14 @@ namespace adapters {
126126
*
127127
* Note that the value returned by this function is a singleton.
128128
*/
129-
static const constexpr_json::json &emptyArray()
129+
static const json2cpp::json &emptyArray()
130130
{
131-
static const constexpr_json::json array = constexpr_json::json::array();
131+
static const json2cpp::json array = json2cpp::json::array();
132132
return array;
133133
}
134134

135135
/// Reference to the contained value
136-
const constexpr_json::json &m_value;
136+
const json2cpp::json &m_value;
137137
};
138138

139139
/**
@@ -165,7 +165,7 @@ namespace adapters {
165165
* Note that this constructor will throw an exception if the value is not
166166
* an object.
167167
*/
168-
explicit json2cppJsonObject(const constexpr_json::json &value) : m_value(value)
168+
explicit json2cppJsonObject(const json2cpp::json &value) : m_value(value)
169169
{
170170
if (!value.is_object()) { throwRuntimeError("Value is not an object."); }
171171
}
@@ -207,14 +207,14 @@ namespace adapters {
207207
*
208208
* Note that the value returned by this function is a singleton.
209209
*/
210-
static const constexpr_json::json &emptyObject()
210+
static const json2cpp::json &emptyObject()
211211
{
212-
static const constexpr_json::json object = constexpr_json::json::object();
212+
static const json2cpp::json object = json2cpp::json::object();
213213
return object;
214214
}
215215

216216
/// Reference to the contained object
217-
const constexpr_json::json &m_value;
217+
const json2cpp::json &m_value;
218218
};
219219

220220

@@ -235,15 +235,15 @@ namespace adapters {
235235
*
236236
* @param source the json2cppJson value to be copied
237237
*/
238-
explicit json2cppJsonFrozenValue(constexpr_json::json source) : m_value(std::move(source)) {}
238+
explicit json2cppJsonFrozenValue(json2cpp::json source) : m_value(std::move(source)) {}
239239

240240
FrozenValue *clone() const override { return new json2cppJsonFrozenValue(m_value); }
241241

242242
bool equalTo(const Adapter &other, bool strict) const override;
243243

244244
private:
245245
/// Stored json2cppJson value
246-
constexpr_json::json m_value;
246+
json2cpp::json m_value;
247247
};
248248

249249

@@ -268,7 +268,7 @@ namespace adapters {
268268
json2cppJsonValue() : m_value(emptyObject()) {}
269269

270270
/// Construct a wrapper for a specific json2cppJson value
271-
explicit json2cppJsonValue(const constexpr_json::json &value) : m_value(value) {}
271+
explicit json2cppJsonValue(const json2cpp::json &value) : m_value(value) {}
272272

273273
/**
274274
* @brief Create a new json2cppJsonFrozenValue instance that contains the
@@ -413,14 +413,14 @@ namespace adapters {
413413

414414
private:
415415
/// Return a reference to an empty object singleton
416-
static const constexpr_json::json &emptyObject()
416+
static const json2cpp::json &emptyObject()
417417
{
418-
static const constexpr_json::json object = constexpr_json::json::object();
418+
static const json2cpp::json object = json2cpp::json::object();
419419
return object;
420420
}
421421

422422
/// Reference to the contained json2cppJson value.
423-
const constexpr_json::json &m_value;
423+
const json2cpp::json &m_value;
424424
};
425425

426426
/**
@@ -444,7 +444,7 @@ namespace adapters {
444444
json2cppJsonAdapter() : BasicAdapter() {}
445445

446446
/// Construct a json2cppJsonAdapter containing a specific json2cpp Json object
447-
explicit json2cppJsonAdapter(const constexpr_json::json &value) : BasicAdapter(json2cppJsonValue{ value }) {}
447+
explicit json2cppJsonAdapter(const json2cpp::json &value) : BasicAdapter(json2cppJsonValue{ value }) {}
448448
};
449449

450450
/**
@@ -471,7 +471,7 @@ namespace adapters {
471471
*
472472
* @param itr json2cppJson iterator to store
473473
*/
474-
explicit json2cppJsonArrayValueIterator(const constexpr_json::json::const_iterator &itr) : m_itr(itr) {}
474+
explicit json2cppJsonArrayValueIterator(const json2cpp::json::const_iterator &itr) : m_itr(itr) {}
475475

476476
/// Returns a json2cppJsonAdapter that contains the value of the current
477477
/// element.
@@ -522,7 +522,7 @@ namespace adapters {
522522
void advance(std::ptrdiff_t n) { m_itr += n; }
523523

524524
private:
525-
constexpr_json::json::const_iterator m_itr;
525+
json2cpp::json::const_iterator m_itr;
526526
};
527527

528528

@@ -550,7 +550,7 @@ namespace adapters {
550550
*
551551
* @param itr json2cppJson iterator to store
552552
*/
553-
explicit json2cppJsonObjectMemberIterator(const constexpr_json::json::const_iterator &itr) : m_itr(itr) {}
553+
explicit json2cppJsonObjectMemberIterator(const json2cpp::json::const_iterator &itr) : m_itr(itr) {}
554554

555555
/**
556556
* @brief Returns a json2cppJsonObjectMember that contains the key and value
@@ -601,13 +601,13 @@ namespace adapters {
601601

602602
private:
603603
/// Iternal copy of the original json2cppJson iterator
604-
constexpr_json::json::const_iterator m_itr;
604+
json2cpp::json::const_iterator m_itr;
605605
};
606606

607607
/// Specialisation of the AdapterTraits template struct for json2cppJsonAdapter.
608608
template<> struct AdapterTraits<valijson::adapters::json2cppJsonAdapter>
609609
{
610-
typedef constexpr_json::json DocumentType;
610+
typedef json2cpp::json DocumentType;
611611

612612
static std::string adapterName() { return "json2cppJsonAdapter"; }
613613
};

src/json2cpp.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
9696
results.hpp.push_back(fmt::format("#ifndef {}_COMPILED_JSON", document_name));
9797
results.hpp.push_back(fmt::format("#define {}_COMPILED_JSON", document_name));
9898

99-
results.hpp.emplace_back("#include <json2cpp/constexpr_json.hpp>");
99+
results.hpp.emplace_back("#include <json2cpp/json2cpp.hpp>");
100100

101101
results.hpp.push_back(fmt::format("namespace compiled_json::{} {{", document_name));
102-
results.hpp.push_back(fmt::format(" const constexpr_json::json &get_{}();", document_name));
102+
results.hpp.push_back(fmt::format(" const json2cpp::json &get_{}();", document_name));
103103
results.hpp.emplace_back("}");
104104

105105
results.hpp.emplace_back("#endif");
@@ -110,16 +110,16 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
110110
results.impl.push_back(fmt::format("#ifndef {}_COMPILED_JSON_IMPL", document_name));
111111
results.impl.push_back(fmt::format("#define {}_COMPILED_JSON_IMPL", document_name));
112112

113-
results.impl.emplace_back("#include <json2cpp/constexpr_json.hpp>");
113+
results.impl.emplace_back("#include <json2cpp/json2cpp.hpp>");
114114

115-
results.impl.push_back(fmt::format("namespace compiled_json::{} {{\nusing json = constexpr_json::basic_json<char>;\nusing data_t=constexpr_json::data_variant<char>;\nusing string_view=std::basic_string_view<char>;\nusing array_t=constexpr_json::basic_array_t<char>;\nusing object_t=constexpr_json::basic_object_t<char>;\nusing value_pair_t=constexpr_json::basic_value_pair_t<char>;\n", document_name));
115+
results.impl.push_back(fmt::format("namespace compiled_json::{} {{\nusing json = json2cpp::basic_json<char>;\nusing data_t=json2cpp::data_variant<char>;\nusing string_view=std::basic_string_view<char>;\nusing array_t=json2cpp::basic_array_t<char>;\nusing object_t=json2cpp::basic_object_t<char>;\nusing value_pair_t=json2cpp::basic_value_pair_t<char>;\n", document_name));
116116

117117

118118
const auto last_obj_name = compile(json, obj_count, results.impl);
119119

120120
results.impl.push_back(fmt::format("static constexpr auto document = json{{{{{}}}}};", last_obj_name));
121121

122-
results.impl.push_back(fmt::format("const constexpr_json::json &get_{}() {{ return document; }}", document_name));
122+
results.impl.push_back(fmt::format("const json2cpp::json &get_{}() {{ return document; }}", document_name));
123123
results.impl.emplace_back("}");
124124
results.impl.emplace_back("#endif");
125125

0 commit comments

Comments
 (0)