Skip to content

Commit 20be481

Browse files
committed
feat: adds front and back methods to Value type
1 parent 8190e06 commit 20be481

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Diff for: include/json/value.h

+16
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@ class JSON_API Value {
585585
iterator begin();
586586
iterator end();
587587

588+
/// @brief Returns a reference to the first element in the container.
589+
/// Calling front on an empty container is undefined behavior.
590+
Value const& front() const;
591+
592+
/// @brief Returns a reference to the last element in the container.
593+
/// Calling back on an empty container is undefined behavior.
594+
Value const& back() const;
595+
596+
/// @brief Returns a reference to the first element in the container.
597+
/// Calling front on an empty container is undefined behavior.
598+
Value& front();
599+
600+
/// @brief Returns a reference to the last element in the container.
601+
/// Calling back on an empty container is undefined behavior.
602+
Value& back();
603+
588604
// Accessors for the [start, limit) range of bytes within the JSON text from
589605
// which this value was parsed, if any.
590606
void setOffsetStart(ptrdiff_t start);

Diff for: src/lib_json/json_value.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,14 @@ Value::iterator Value::end() {
14951495
return iterator();
14961496
}
14971497

1498+
Value const& Value::front() const { return *begin(); }
1499+
1500+
Value& Value::front() { return *begin(); }
1501+
1502+
Value const& Value::back() const { return *(--end()); }
1503+
1504+
Value& Value::back() { return *(--end()); }
1505+
14981506
// class PathArgument
14991507
// //////////////////////////////////////////////////////////////////
15001508

Diff for: src/test_lib_json/main.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,14 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrays) {
310310
const Json::Value& constArray = array1_;
311311
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[index0]);
312312
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray[0]);
313+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray.front());
314+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), constArray.back());
313315

314316
// Access through non-const reference
315317
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[index0]);
316318
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_[0]);
319+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_.front());
320+
JSONTEST_ASSERT_EQUAL(Json::Value(1234), array1_.back());
317321

318322
array1_[2] = Json::Value(17);
319323
JSONTEST_ASSERT_EQUAL(Json::Value(), array1_[1]);
@@ -356,6 +360,8 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, resizePopulatesAllMissingElements) {
356360
v.resize(n);
357361
JSONTEST_ASSERT_EQUAL(n, v.size());
358362
JSONTEST_ASSERT_EQUAL(n, std::distance(v.begin(), v.end()));
363+
JSONTEST_ASSERT_EQUAL(v.front(), Json::Value{});
364+
JSONTEST_ASSERT_EQUAL(v.back(), Json::Value{});
359365
for (const Json::Value& e : v)
360366
JSONTEST_ASSERT_EQUAL(e, Json::Value{});
361367
}
@@ -406,13 +412,17 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
406412
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append
407413
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
408414
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
415+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array.front());
416+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array.back());
409417

410418
// insert lvalue at the head
411419
JSONTEST_ASSERT(array.insert(0, str1));
412420
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
413421
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
414422
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
415423
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]);
424+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array.front());
425+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array.back());
416426
// checking address
417427
for (Json::ArrayIndex i = 0; i < 3; i++) {
418428
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
@@ -425,6 +435,8 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
425435
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
426436
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
427437
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
438+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array.front());
439+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array.back());
428440
// checking address
429441
for (Json::ArrayIndex i = 0; i < 4; i++) {
430442
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
@@ -438,6 +450,8 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, arrayInsertAtRandomIndex) {
438450
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
439451
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
440452
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
453+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array.front());
454+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array.back());
441455
// checking address
442456
for (Json::ArrayIndex i = 0; i < 5; i++) {
443457
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);

0 commit comments

Comments
 (0)