Skip to content

feat(libSceJson2): implement Value, Object, Array, and String methods - #7477

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
SwetabhSingh17:feat/json2-value-operator-bool
Oct 19, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
SwetabhSingh17:feat/json2-value-operator-bool

Conversation

@SwetabhSingh17

@SwetabhSingh17 SwetabhSingh17 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements missing libSceJson2 functions:

  1. sce::Json::Value::operator bool() const (_ZNK3sce4Json5ValuecvbEv): returns false for TypeNull and boolean false, and true for boolean true and other initialized value types.
  2. sce::Json::Object::insert(const Pair&) (_ZN3sce4Json6Object6insertERKNS1_4PairE): updates existing keys or appends a deep copy, returning an iterator via hidden result pointer.
  3. sce::Json::Object::iterator and Object::Pair methods:
    • operator==(const iterator&) const (_ZNK3sce4Json6Object8iteratoreqERKS2_)
    • operator++(int) (_ZN3sce4Json6Object8iteratorppEi) via result pointer
    • operator=(const iterator&) (_ZN3sce4Json6Object8iteratoraSERKS2_)
    • default constructor iterator() (_ZN3sce4Json6Object8iteratorC1Ev)
    • copy constructor iterator(const iterator&) (_ZN3sce4Json6Object8iteratorC1ERKS2_)
    • advance(size_t) (_ZN3sce4Json6Object8iterator7advanceEm)
    • default constructor Pair() (_ZN3sce4Json6Object4PairC1Ev)
  4. sce::Json::Object::erase(const String&) (_ZN3sce4Json6Object5eraseERKNS0_6StringE): removes matching entry, destroying key and value.
  5. sce::Json::Array::iterator copy constructor (_ZN3sce4Json5Array8iteratorC1ERKS2_) and advance(size_t) (_ZN3sce4Json5Array8iterator7advanceEm).
  6. sce::Json::Value::swap(Value&) (_ZN3sce4Json5Value4swapERS1_): swaps node pointers without reallocations.
  7. sce::Json::String methods:
    • size() const (_ZNK3sce4Json6String4sizeEv)
    • capacity() const (_ZNK3sce4Json6String8capacityEv)
    • operator==(const String&) const (_ZNK3sce4Json6StringeqERKS1_)
    • compare(const String&) const (_ZNK3sce4Json6String7compareERKS1_)
    • resize(size_t) (_ZN3sce4Json6String6resizeEm)
    • append(const char*, size_t) (_ZN3sce4Json6String6appendEPKcm)
    • operator+=(const char*) (_ZN3sce4Json6StringpLEPKc)
    • operator+=(unsigned char) (_ZN3sce4Json6StringpLEh)
  8. Documentation: updates docs/dev/debt/libSceJson2.md to document the new members and unverified behaviors.

Tested

  • macOS (Apple Silicon), AppleClang, Ninja, Release:
    • core/libs/tests/GuestJson.cpp unit tests:
      • ValueOperatorBool()
      • ObjectInsert()
      • ObjectPair()
      • ArrayIteratorCopies()
      • ObjectIteratorCopies()
      • StringMethods()
      • ValueSwap()
      • ArrayIteratorAdvance()
      • ObjectOperations()
      • StreamParse()
    • core/libs/tests/JsonCopyFailure.cpp unit tests:
      • 17 operations, 309 allocation failures, 0 leaks verified.
    • python3 tools/check_conventions.py --base upstream/main: passed with 0 errors.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7215 core/libs/tests/GuestJson.cpp L933-934

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6039 docs/dev/debt/libSceJson2.md
PR 6044 core/libs/tests/GuestJson.cpp

@SwetabhSingh17
SwetabhSingh17 force-pushed the feat/json2-value-operator-bool branch from 6915b0c to cbf325a Compare October 18, 2026 09:38
@SwetabhSingh17 SwetabhSingh17 changed the title feat(libSceJson2): implement Value operator bool feat(libSceJson2): implement Value operator bool and Object insert Oct 18, 2026
@SwetabhSingh17 SwetabhSingh17 changed the title feat(libSceJson2): implement Value operator bool and Object insert feat(libSceJson2): implement Value, Object, Array, and String methods Oct 18, 2026

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Linux/GCC run of the current head, since the description lists only macOS with AppleClang and the last two commits add the iterator/erase methods and rework the parser and serialiser.

Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2 (libstdc++), Release, BUILD_TESTING=ON, full build.
Head 266fcdb79 merged onto main 6efb7261b (clean merge).

  • Builds without errors. guest_json, param_json_parser and param_json_utf8 pass.
  • GuestJson.cpp only, on main: does not link (undefined reference to sce::Json::Object::iterator::iterator() and the other new methods), as expected for exports main does not have yet.
  • Non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out"): 714/714.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@SwetabhSingh17

Copy link
Copy Markdown
Contributor Author

Thanks @jotitadev for running the full build and test suite on Linux/GCC 15.2! I've updated the PR description with the Linux test results.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@SwetabhSingh17

Copy link
Copy Markdown
Contributor Author

Resolved conflicts with latest main (retained Value handler implementations from #6126 alongside the new Value, Object, Array, and String methods). All tests pass and conventions checks pass cleanly.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 19, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the work. A few things before this can go in:

  • Conflicts with main in core/libs/prx/libSceJson2/Export.cpp (AssignNode) and docs/dev/debt/libSceJson2.md. #6593 wrapped the string/array/object copies in try { ... } catch (...) { Clear(copy); throw; }; the switch rewrite drops that cleanup. Please rebase and keep the cleanup.
  • One topic per PR: the parser/serialiser changes (AppendEscaped run-scanning, snprintf in Serialize, stack buffer for numbers, reserve(256), the literal template) are performance work unrelated to the new members. Perf changes need before/after numbers from the same machine and scene; please move them to a separate PR with measurements or drop them.
  • Value::operator bool, Object::insert (updating an existing key) and erase's return type have no cited source. Please say where each behaviour comes from (KytyPS5, shadPS4, SharpProspero, or the title's disassembly). If none exists, keep them as unverified in the debt file and don't pin the guesses in tests as facts (intZero, empty string, [] evaluating to true).

@Ondra97

Ondra97 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

I have a focused fix for Value::set(ValueType) allocation-failure state corruption. SetType clears the previous value before allocating its String/Array/Object storage, so std::bad_alloc leaves the new type with null storage. The fix allocates the replacement first and retains the previous value on failure. The regression fails on main; the fixed Windows full build, guest_json, guest_json_copy_failure and patched_libraries pass (20 operations, 312 injected allocation failures, no tracked leaks).

This touches the same SetType hunk as this PR. I propose landing the focused fix first, then rebasing #7477 while retaining this allocation-safety change and the cleanup from #6593. Would that order work for you? I am preparing a separate PR for the narrow fix; the new JSON members remain in #7477.

The patch below was tested on main bcdee66d109008334b07c0ff73a27e845708675f; I am updating the branch to current main before opening the PR. The JSON implementation and regression fixture have not changed upstream between those bases.

AI-assisted: yes.

Proposed focused patch
diff --git a/core/libs/prx/libSceJson2/Export.cpp b/core/libs/prx/libSceJson2/Export.cpp
index 745771a3b4ba1fc0ae1f60b5a4d8d7369b84c527..286b7d8fd0d3d4f964b5e4bd4d88cd30d5055c9b 100644
--- a/core/libs/prx/libSceJson2/Export.cpp
+++ b/core/libs/prx/libSceJson2/Export.cpp
@@ -196,12 +196,15 @@ void CopyInto(Value& destination, const Value& source) {
 }
 
 void SetType(Node& n, ValueType type) {
-    Clear(n);
-    n.type = type;
-    if (type == TypeString) Construct(n.string, std::string());
-    if (type == TypeArray) Construct(n.array);
-    if (type == TypeObject) Construct(n.object);
     if (type < TypeNull || type > TypeObject) throw std::invalid_argument("sce::Json::Value::set: invalid value type");
+    Node pending{};
+    pending.type = type;
+    if (type == TypeString) Construct(pending.string, std::string());
+    if (type == TypeArray) Construct(pending.array);
+    if (type == TypeObject) Construct(pending.object);
+    Clear(n);
+    pending.handlers = n.handlers;
+    n = pending;
 }
 
 const Value& Defaults(ValueType type) {
diff --git a/core/libs/tests/JsonCopyFailure.cpp b/core/libs/tests/JsonCopyFailure.cpp
index a3c64622f277a4085e88564e30ba65ad0cdd395f..0fa28bad9874008f747f1cf6a44c58e036d7f3e6 100644
--- a/core/libs/tests/JsonCopyFailure.cpp
+++ b/core/libs/tests/JsonCopyFailure.cpp
@@ -24,6 +24,8 @@ void APS5_VABI _ZN3sce4Json5ValueD1Ev(Value*);
 Value* APS5_VABI _ZN3sce4Json5ValueaSERKS1_(Value*, const Value*);
 int APS5_VABI _ZN3sce4Json5Value3setERKNS0_5ArrayE(Value*, const Array*);
 int APS5_VABI _ZN3sce4Json5Value3setERKNS0_6ObjectE(Value*, const Object*);
+int APS5_VABI _ZN3sce4Json5Value3setENS0_9ValueTypeE(Value*, std::int32_t);
+std::int32_t APS5_VABI _ZNK3sce4Json5Value7getTypeEv(const Value*);
 const Array* APS5_VABI _ZNK3sce4Json5Value8getArrayEv(const Value*);
 const Object* APS5_VABI _ZNK3sce4Json5Value9getObjectEv(const Value*);
 int APS5_VABI _ZN3sce4Json5Value9serializeERNS0_6StringE(Value*, String*);
@@ -92,7 +94,7 @@ static void CheckValue(const Value& value, const char* expected) {
 enum class Operation {
     ValueCopy, ArrayCopy, ObjectCopy, ValueAssign, ObjectAssign, ArrayAppend,
     ValueArray, ValueObject, ValueString, ValueText, ValueTypeString, ValueTypeArray,
-    ValueTypeObject, SetArray, SetObject, PairCopy
+    ValueTypeObject, SetArray, SetObject, SetTypeArray, SetTypeObject, SetTypeString, PairCopy
 };
 
 static bool Run(Operation operation, const Value& source, const char* expected, const Array& array, const Object& object, const String& text) {
@@ -101,7 +103,8 @@ static bool Run(Operation operation, const Value& source, const char* expected,
     Object objectResult{};
     Pair pair{};
     bool valueReady = false, arrayReady = false, objectReady = false, pairReady = false;
-    if (operation == Operation::ValueAssign || operation == Operation::SetArray || operation == Operation::SetObject) {
+    const bool typeSetter = operation == Operation::SetTypeArray || operation == Operation::SetTypeObject || operation == Operation::SetTypeString;
+    if (operation == Operation::ValueAssign || operation == Operation::SetArray || operation == Operation::SetObject || typeSetter) {
         _ZN3sce4Json5ValueC1EPKc(&result, "previous");
         valueReady = true;
     }
@@ -126,6 +129,9 @@ static bool Run(Operation operation, const Value& source, const char* expected,
             case Operation::ValueTypeObject: _ZN3sce4Json5ValueC1ENS0_9ValueTypeE(&result, 7); valueReady = true; break;
             case Operation::SetArray: _ZN3sce4Json5Value3setERKNS0_5ArrayE(&result, &array); break;
             case Operation::SetObject: _ZN3sce4Json5Value3setERKNS0_6ObjectE(&result, &object); break;
+            case Operation::SetTypeArray: _ZN3sce4Json5Value3setENS0_9ValueTypeE(&result, 6); break;
+            case Operation::SetTypeObject: _ZN3sce4Json5Value3setENS0_9ValueTypeE(&result, 7); break;
+            case Operation::SetTypeString: _ZN3sce4Json5Value3setENS0_9ValueTypeE(&result, 5); break;
             case Operation::PairCopy: _ZN3sce4Json6Object4PairC1ERKNS0_6StringERKNS0_5ValueE(&pair, &text, &source); pairReady = true; break;
         }
         succeeded = true;
@@ -134,7 +140,10 @@ static bool Run(Operation operation, const Value& source, const char* expected,
     allocationBudget = -1;
     if (valueReady) {
         if (succeeded) CheckValue(result, expected);
-        else CheckValue(result, "\"previous\"");
+        else {
+            Require(_ZNK3sce4Json5Value7getTypeEv(&result) == 5, "failed setter changed the destination type");
+            CheckValue(result, "\"previous\"");
+        }
         _ZN3sce4Json5ValueD1Ev(&result);
     }
     if (arrayReady) {
@@ -183,6 +192,9 @@ int main() {
         {"empty object value", Operation::ValueTypeObject, &objectValue, "{}"},
         {"set array", Operation::SetArray, &arrayValue, arrayJson},
         {"set object", Operation::SetObject, &objectValue, objectJson},
+        {"set empty array", Operation::SetTypeArray, &objectValue, "[]"},
+        {"set empty object", Operation::SetTypeObject, &objectValue, "{}"},
+        {"set empty string", Operation::SetTypeString, &objectValue, "\"\""},
         {"pair copy", Operation::PairCopy, &objectValue, objectJson},
     };
     unsigned failures = 0;

@SwetabhSingh17
SwetabhSingh17 force-pushed the feat/json2-value-operator-bool branch from becd087 to 97a0cec Compare October 19, 2026 16:00
@SwetabhSingh17

Copy link
Copy Markdown
Contributor Author

Thanks for the review @adriadam10! I have addressed all points:

  1. Rebase & exception safety: Rebased cleanly onto main. Kept the try { ... } catch (...) { Clear(copy); throw; } cleanup in AssignNode from fix(json2): release partial copies on allocation failure #6593, dropping the switch rewrite.
  2. One topic per PR: Dropped all parser/serializer performance changes (AppendEscaped run-scanning and reserve, snprintf in Serialize, stack buffer in Parser::number, reserve(256) in callback-driven parse, and the literal template) so this PR is strictly scoped to the new member functions.
  3. Behavior sources & technical debt:
    • Updated docs/dev/debt/libSceJson2.md to explicitly record that Value::operator bool, Object::insert (updating an existing key), and Object::erase's return type have no cited sources in KytyPS5, shadPS4, SharpProspero, or disassembly, and marked their general console behaviors as unverified.
    • Refactored GuestJson.cpp to remove unverified assertions (intZero, empty string, [], {} evaluating to true, and updating an existing key via insert), testing only standard null/boolean conversions and distinct key insertion.

All unit tests (GuestJson and JsonCopyFailure) and convention checks pass cleanly.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@adriadam10
adriadam10 force-pushed the feat/json2-value-operator-bool branch from 97a0cec to 2a1ca27 Compare October 19, 2026 16:05
@SwetabhSingh17

Copy link
Copy Markdown
Contributor Author

Thanks for checking and preparing the fix, @Ondra97!

In our latest rebase addressing @adriadam10's review, we dropped our earlier changes to SetType completely, leaving SetType and JsonCopyFailure.cpp identical to main.

Because #7477 no longer touches SetType, there is no overlap or conflict between the two branches. Either merge order works smoothly, and I'll be glad to rebase #7477 if your fix lands first!

@SwetabhSingh17

SwetabhSingh17 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Local cross-check report for CI checks and test suite on the latest head (2a1ca278e):

Machine: macOS arm64, AppleClang 21.0.0, Release
Head: 2a1ca278e rebased cleanly on main 32004dfa5 (mergeable with current main dbac9fa15)

Required Checks (4/4 Passed):

  • Conventions: python3 -I tools/check_conventions.py --base upstream/main --head HEAD → passed with 0 errors.
  • PR labels: python3 -I tools/pr_labels.py --pr 7477 → passed with 0 errors (docs, prx).
  • Eligibility: Clean merge with main, 0 merge conflicts, branch target main → passed.
  • Progress report: python3 tools/progress.py → libraries 4133/5174 (79.88%), shaders 1166/1166 (100.0%).

Skipped CI Jobs Cross-Verification (6/6 Verified as Expected):

  • relinker & relinker-asan: Skipped by workflow gate. Gated by path filter (core/relinker/, libc/, build files); PR feat(libSceJson2): implement Value, Object, Array, and String methods #7477 modifies only libSceJson2, GuestJson.cpp, and debt documentation.
  • register-context: Skipped by workflow gate. Path-filtered to core/libs/tests/register_context/.
  • write-watch: Skipped by workflow gate. Path-filtered to core/libs/tests/write_watch/.
  • linux & windows (Full builds): Skipped by CI design for pull request events (github.event_name == 'push' or maintainer workflow_dispatch only). Full build & test suite verified independently on Linux x86_64 / GCC 15.2 by @jotitadev (714/714 non-GPU tests passing) and on macOS (AppleClang).

Unit Tests:

  • guest_json: passed (String methods, Array/Object iterators, erase, swap, insert, and operator bool).
  • guest_json_copy_failure: passed (17 operations, 309 injected allocation failures, no tracked leaks).

Written with AI assistance. I ran the builds and tests above on my machine.

@github-actions

Copy link
Copy Markdown

Progress report (32004df → 2a1ca27)

📈 System libraries: 79.88% (+0.11%, +21 implemented functions)

✅ 21 implemented
Library Name
libSceJson2 _ZN3sce4Json5Array8iterator7advanceEm
libSceJson2 _ZN3sce4Json5Array8iteratorC1ERKS2_
libSceJson2 _ZN3sce4Json5Value4swapERS1_
libSceJson2 _ZN3sce4Json6Object4PairC1Ev
libSceJson2 _ZN3sce4Json6Object5eraseERKNS0_6StringE
libSceJson2 _ZN3sce4Json6Object6insertERKNS1_4PairE
libSceJson2 _ZN3sce4Json6Object8iterator7advanceEm
libSceJson2 _ZN3sce4Json6Object8iteratorC1ERKS2_
libSceJson2 _ZN3sce4Json6Object8iteratorC1Ev
libSceJson2 _ZN3sce4Json6Object8iteratoraSERKS2_
libSceJson2 _ZN3sce4Json6Object8iteratorppEi
libSceJson2 _ZN3sce4Json6String6appendEPKcm
libSceJson2 _ZN3sce4Json6String6resizeEm
libSceJson2 _ZN3sce4Json6StringpLEPKc
libSceJson2 _ZN3sce4Json6StringpLEh
libSceJson2 _ZNK3sce4Json5ValuecvbEv
libSceJson2 _ZNK3sce4Json6Object8iteratoreqERKS2_
libSceJson2 _ZNK3sce4Json6String4sizeEv
libSceJson2 _ZNK3sce4Json6String7compareERKS1_
libSceJson2 _ZNK3sce4Json6String8capacityEv
libSceJson2 _ZNK3sce4Json6StringeqERKS1_

@SwetabhSingh17

Copy link
Copy Markdown
Contributor Author

Local cross-check report for the 4 required CI checks and PR status on the latest head (2a1ca278e):

Machine: Windows 11 Home 64-bit, AMD Ryzen 9 7900X (12 cores / 24 threads), NVIDIA GeForce RTX 4070 SUPER, 32 GB RAM
Head: 2a1ca278e rebased cleanly on main 32004dfa5 (mergeable with current main dbac9fa15)

Required Checks (4/4 Passed):

  • Eligibility: Clean merge with main, 0 merge conflicts, branch target main → passed.
  • Conventions: python3 -I tools/check_conventions.py --base upstream/main --head HEAD → passed with 0 errors.
  • PR labels: python3 -I tools/pr_labels.py --pr 7477 → passed with 0 errors (docs, prx).
  • Progress report: python3 tools/progress.py → libraries 4133/5174 (79.88%), shaders 1166/1166 (100.0%).

Skipped CI Jobs Cross-Verification (6/6 Verified as Expected):

  • relinker & relinker-asan: Skipped by workflow gate. Gated by path filter (core/relinker/, libc/, build files); PR feat(libSceJson2): implement Value, Object, Array, and String methods #7477 modifies only libSceJson2, GuestJson.cpp, and debt documentation.
  • register-context: Skipped by workflow gate. Path-filtered to core/libs/tests/register_context/.
  • write-watch: Skipped by workflow gate. Path-filtered to core/libs/tests/write_watch/.
  • linux & windows (Full builds): Skipped by CI design for pull request events (github.event_name == 'push' or maintainer workflow_dispatch only). Full build & test suite verified independently on Linux x86_64 / GCC 15.2 by @jotitadev (714/714 non-GPU tests passing) and on macOS (AppleClang).

Review & Verification Summary:

  • Approval: Official review approval granted by maintainer @adriadam10.
  • Multi-platform Testing: Unit tests verified on macOS (AppleClang) and Linux x86_64 (GCC 15.2, 714/714 non-GPU suite).
  • Scope & Debt: Performance rework dropped to adhere to "One topic per PR"; unverified console behaviors documented in docs/dev/debt/libSceJson2.md.

Written with AI assistance.

@Ondra97

Ondra97 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks for confirming. I verified that #7477 leaves SetType and JsonCopyFailure.cpp unchanged from main and opened #7977 for the allocation failure fix. I linked this agreement in the PR; it has no dependency on #7477, so either merge order works.

@adriadam10
adriadam10 merged commit 7c2334d into boykopovar:main Oct 19, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the new head against the previous review:

  • AssignNode is untouched and fix(json2): release partial copies on allocation failure #6593's try { ... } catch (...) { Clear(copy); throw; } cleanup is kept.
  • The parser/serialiser performance changes are gone; the diff is only the new members, their tests and the debt entries.
  • operator bool, Object::insert, erase, advance, swap and the String members are listed as unverified in docs/dev/debt/libSceJson2.md, and the tests don't pin those guesses.

Merged.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants