feat(json2): implement sce::Json values, strings, arrays, objects, parser and null-access callback #57

Merged
oneandonlydean merged 1 commit from upstream-pr/57 into main 2026-09-28 06:42:22 +00:00
oneandonlydean commented 2026-09-28 03:42:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-28T03:42:52Z.


What

libSceJson2 exported only the Initializer and MemAllocator constructors and destructors, plus initialize. A retail title parses and builds JSON through sce::Json and needs the rest. This implements:

  • Value: constructors for every type and copy, assignment, set overloads, typed getters, getType, count, operator[] by key and index, referValue/referArray/referObject, serialize, toString.
  • String, Array and Object with their iterators.
  • Parser::parse.
  • Initializer::terminate and Initializer::setGlobalNullAccessCallBack.

How

  • Layouts: String, Array, Object and Value are pointer-sized handles to host-side storage. The title stores String/Array/Object at an 8-byte stride and Value in 0x20-byte slots, so none of them can be bigger. The pair an Object::iterator dereferences to has its key at +0x00 and its Value at +0x10, which the title reads directly. Array and object iterators are 8 bytes.
  • Parser: a strict RFC 8259 parser.
    • Numbers: integers that fit int64 become Integer, larger non-negative ones UInteger, and everything else Real.
    • Strings: \u escapes, including surrogate pairs, are decoded to UTF-8.
    • Errors: on any error parse returns a negative code (0x80920101) and leaves the target value untouched.
  • Serialisation: compact output. Strings are escaped; reals use %.17g, and a non-finite real throws.
  • Null access: a typed getter on a value of another type, a missing key or an out-of-range index goes through the null-access callback. The callback's result is used when its type matches (any type for operator[]). Otherwise a static default value of the requested type is returned. terminate clears the callback.
  • Integer getters: getInteger/getUInteger/getReal convert between the integer kinds and to real where the value fits.
  • setGlobalNullAccessCallBack (NID 00oCq0RwSAY) was identified by hashing its mangled name.
  • MemAllocator's base constructor and destructor install the abstract class's vtable. A derived allocator replaces it with its own, so its entries (which throw, like a pure virtual call) run only if a pure virtual is called. Initializer::initialize throws on a null parameter instead of silently accepting it.
  • Mutating a value that isn't the expected type (referArray on a string, etc.) throws.

Testing

New guest_json test:

  • Round trip: a document with every value type is parsed and checked through the typed getters, including UInteger above INT64_MAX, a \u escape and nested arrays. It is serialised to an exact expected string, re-parsed and serialised again to the same text.
  • Parse errors: six malformed inputs return a negative code and leave the value unchanged.
  • Objects: operator[] inserts in order and returns the same slot for an existing key; iteration yields the keys, with each pair's Value at +0x10.
  • Arrays: push_back, size, back and iteration.
  • Null-access callback: a missing key, a wrong-type getter and an index into an object each call the callback with the requested type, the parent value and the context. A wrong-type callback result falls back to the default, and after terminate the callback isn't called.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4658973926 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/57). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-28T03:42:52Z. --- ### What `libSceJson2` exported only the `Initializer` and `MemAllocator` constructors and destructors, plus `initialize`. A retail title parses and builds JSON through `sce::Json` and needs the rest. This implements: - `Value`: constructors for every type and copy, assignment, `set` overloads, typed getters, `getType`, `count`, `operator[]` by key and index, `referValue`/`referArray`/`referObject`, `serialize`, `toString`. - `String`, `Array` and `Object` with their iterators. - `Parser::parse`. - `Initializer::terminate` and `Initializer::setGlobalNullAccessCallBack`. ### How - **Layouts:** `String`, `Array`, `Object` and `Value` are pointer-sized handles to host-side storage. The title stores `String`/`Array`/`Object` at an 8-byte stride and `Value` in 0x20-byte slots, so none of them can be bigger. The pair an `Object::iterator` dereferences to has its key at +0x00 and its `Value` at +0x10, which the title reads directly. Array and object iterators are 8 bytes. - **Parser:** a strict RFC 8259 parser. - Numbers: integers that fit `int64` become `Integer`, larger non-negative ones `UInteger`, and everything else `Real`. - Strings: `\u` escapes, including surrogate pairs, are decoded to UTF-8. - Errors: on any error `parse` returns a negative code (`0x80920101`) and leaves the target value untouched. - **Serialisation:** compact output. Strings are escaped; reals use `%.17g`, and a non-finite real throws. - **Null access:** a typed getter on a value of another type, a missing key or an out-of-range index goes through the null-access callback. The callback's result is used when its type matches (any type for `operator[]`). Otherwise a static default value of the requested type is returned. `terminate` clears the callback. - **Integer getters:** `getInteger`/`getUInteger`/`getReal` convert between the integer kinds and to real where the value fits. - `setGlobalNullAccessCallBack` (NID `00oCq0RwSAY`) was identified by hashing its mangled name. - `MemAllocator`'s base constructor and destructor install the abstract class's vtable. A derived allocator replaces it with its own, so its entries (which throw, like a pure virtual call) run only if a pure virtual is called. `Initializer::initialize` throws on a null parameter instead of silently accepting it. - Mutating a value that isn't the expected type (`referArray` on a string, etc.) throws. ### Testing New `guest_json` test: - **Round trip:** a document with every value type is parsed and checked through the typed getters, including `UInteger` above `INT64_MAX`, a `\u` escape and nested arrays. It is serialised to an exact expected string, re-parsed and serialised again to the same text. - **Parse errors:** six malformed inputs return a negative code and leave the value unchanged. - **Objects:** `operator[]` inserts in order and returns the same slot for an existing key; iteration yields the keys, with each pair's `Value` at +0x10. - **Arrays:** `push_back`, `size`, `back` and iteration. - **Null-access callback:** a missing key, a wrong-type getter and an index into an object each call the callback with the requested type, the parent value and the context. A wrong-type callback result falls back to the default, and after `terminate` the callback isn't called.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!57
No description provided.