Skip to content

feat: add operator[] access support for TJValue#45

Closed
njp2k5 wants to merge 3 commits into
FFMG:mainfrom
njp2k5:feat/object-operator-access
Closed

feat: add operator[] access support for TJValue#45
njp2k5 wants to merge 3 commits into
FFMG:mainfrom
njp2k5:feat/object-operator-access

Conversation

@njp2k5
Copy link
Copy Markdown

@njp2k5 njp2k5 commented May 20, 2026

Description

Adds support for ergonomic json[key].as<T>() access syntax and uniform indexed access on TJValue.

This introduces:

  • TJValue::as<T>() as an alias for get<T>()
  • TJValue::operator[](const TJCHAR* key)
  • TJValue::operator[](int index)
  • lightweight accessor chaining support

Supported usage examples:

json["name"].as<std::string>();
json["user"]["age"].as<int>();
json[0].as<int>();

This implementation keeps the change minimal and localized to existing access behavior.

Fixes #44

New Package?

  • No

Version Bump?

  • No

Type of Change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • I added new unit tests to cover this change

Added focused tests for:

  • key access with as<T>()
  • array index access
  • object index access
  • missing-key exception behavior

All tests pass locally:

539 tests passed

Suggested Checklist:

  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI review requested due to automatic review settings May 20, 2026 11:56
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR introduces a chainable operator[] accessor API for TJValue to simplify JSON navigation by key and index, and adds tests validating the new behavior (including strict-mode missing-key throwing).

Changes:

  • Added TJValueAccessor and TJValue::operator[] overloads for key/index access with chainable .as<T>().
  • Added TJValue::as<T>() convenience alias for get<T>().
  • Added unit tests covering key/index access and strict missing-key behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
tests/testtinyjsonvaluesget.cpp Adds tests for new operator[] / accessor chaining and strict-mode missing-key throwing.
src/TinyJSON.h Adds TJValueAccessor, TJValue::operator[] overloads, and TJValue::as<T>().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/TinyJSON.h
Comment thread src/TinyJSON.h
Comment on lines +1653 to +1664
template<typename T>
T as() const
{
if (_by_index)
{
auto value = get_value_ptr();
if (value == nullptr)
{
return default_value<T>();
}
return value->get<T>();
}
Comment thread src/TinyJSON.h

const auto* object = dynamic_cast<const TJValueObject*>(_owner);
if (object != nullptr)
{
Comment on lines +372 to +373
ASSERT_EQ(1, (*json)[0].as<int>());

@FFMG
Copy link
Copy Markdown
Owner

FFMG commented May 22, 2026

Hi @njp2k5,

Thank you so much for taking the time to implement this and writing such thorough unit tests! I saw the automated review here as well, and I really appreciate the depth of the work you put into resolving #44.

Because this is a lightweight library, I want to avoid adding proxy accessor classes to keep the core footprint minimal. Your approach actually prompted me to jump in and implement a native operator[] design directly into the core types over in #46.

I'm going to close this one in favour of that lightweight approach, but I sincerely appreciate your initiative and contribution here!

@FFMG FFMG closed this May 22, 2026
@njp2k5
Copy link
Copy Markdown
Author

njp2k5 commented May 22, 2026

Thanks for the thoughtful feedback and explanation!

That makes sense — keeping the library lightweight is definitely a reasonable design goal here. I appreciate you taking the time to review the implementation and explain the direction taken in #46.

Glad the tests and exploration were still useful, and I enjoyed working through the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

For objects, add support for operator[]( const char*)

3 participants