Skip to content

Add toJSON() to Struct and document architectural trade-offs#9

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/reconsider-architectural-foundations
Draft

Add toJSON() to Struct and document architectural trade-offs#9
Copilot wants to merge 2 commits intomainfrom
copilot/reconsider-architectural-foundations

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

Architectural review of property descriptors vs proxies vs instance descriptors, with a concrete fix for the main ergonomic gap.

Analysis (ARCHITECTURE.md)

Prototype property descriptors remain the right call. Proxies would fix spread/JSON ergonomics but cost 5–10× on property access, break TypeScript inference, and don't compose with class/instanceof. Instance descriptors would fix spread but add per-instance GC pressure—bad for large struct arrays.

toJSON() on Struct

The biggest practical gap: JSON.stringify(struct) returned {} because it only sees own enumerable properties. toJSON() iterates inherited enumerable fields into a plain object. Nested substructs serialize recursively via the standard toJSON protocol.

const rect = new Rect(buf)
JSON.stringify(rect)
// {"origin":{"x":1,"y":2},"size":{"x":3,"y":4}}

// Also works as a spread workaround
const copy = { ...rect.toJSON() }

- Add toJSON() method to Struct so JSON.stringify works out of the box
- Add ARCHITECTURE.md documenting property descriptors vs proxies trade-offs
- Update README gotcha about JSON.stringify now working
- Add 3 tests for toJSON (primitives, substructs, empty struct)
- Update CHANGELOG with 0.17.0 entry

Agent-Logs-Url: https://github.com/rotu/structview/sessions/31ab83a8-abbb-4e43-9fe6-13b54ee4f3ae

Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
Copilot AI changed the title [WIP] Reconsider architectural foundations of structview library Add toJSON() to Struct and document architectural trade-offs Apr 2, 2026
Copilot AI requested a review from rotu April 2, 2026 22:52
Copilot finished work on behalf of rotu April 2, 2026 22:52
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.

2 participants