feat(type-safety): document JSON empty-object vs empty-array trap#69
Conversation
PHP's [] and {} are the same empty array: json_encode([]) yields [], and
json_decode('{}', true) === []. Strict JSON consumers (LLM tool-calling,
JSON Schema) reject [] where {} is required. Add rules: use stdClass for
empty JSON objects, decode without associative when a value must round-trip
as an object, and normalise in the shared value object's constructor.
Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
There was a problem hiding this comment.
Code Review
This pull request adds a new section to the PHP modernization reference documentation explaining the differences between empty JSON objects and empty JSON arrays in PHP, highlighting common serialization/deserialization traps and providing rules to avoid them. The review feedback suggests standardizing spelling to American English (e.g., 'serialize', 'normalize', 'recognize'), using the fully qualified '\stdClass' in the PHP code snippet to prevent namespace resolution issues, and adding a concrete constructor normalization example.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Pull request overview
Adds guidance to the PHP modernization “type safety” reference about a common JSON interoperability pitfall: PHP’s inability to distinguish empty arrays ([]) from empty objects ({}) when encoding/decoding, which can break strict JSON consumers (LLM tool-calling APIs, JSON Schema validators).
Changes:
- Introduces a new section “JSON: Empty Object vs. Empty Array” with minimal PHP examples demonstrating the encode/decode traps.
- Documents concrete rules for preserving/producing
{}when an empty JSON object is required (e.g.,stdClass, decoding withoutassociative=true, constructor-level normalization). - Adds a practical “symptom to recognize” heuristic to aid debugging of intermittent-looking 4xx failures.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…r example Address review: use American spelling (serialize/normalize) to match the rest of the references, fully-qualify \stdClass so snippets work inside namespaced files, and add a concrete constructor normalization example. Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
|



Adds a JSON: Empty Object vs. Empty Array section to
references/type-safety.md.PHP conflates
[]and{}(both an empty array):json_encode([])→[], andjson_decode('{}', true) === []. Strict JSON consumers — LLM tool-calling (OpenAI/Ollama), JSON Schema validators — reject[]where an object is required (Ollama: HTTP 400Value looks like object, but can't find closing '}' symbol).Rules added: use
stdClassfor empty JSON objects; decode withoutassociative=truewhen a value must round-trip as an object; normalise in the shared value object's constructor so direct-property readers andfromArray(toArray())stay correct. Includes the symptom-recognition tell (a 4xx that correlates with which empty field is present, not timing).Source: real two-site bug in t3x-nr-llm's tool runtime (fixed in its PR #308).