Skip to content

feat(type-safety): document JSON empty-object vs empty-array trap#69

Merged
CybotTM merged 2 commits into
mainfrom
feat/json-empty-object-vs-array
Jul 5, 2026
Merged

feat(type-safety): document JSON empty-object vs empty-array trap#69
CybotTM merged 2 commits into
mainfrom
feat/json-empty-object-vs-array

Conversation

@CybotTM

@CybotTM CybotTM commented Jul 5, 2026

Copy link
Copy Markdown
Member

Adds a JSON: Empty Object vs. Empty Array section to references/type-safety.md.

PHP conflates [] and {} (both an empty array): json_encode([])[], and json_decode('{}', true) === []. Strict JSON consumers — LLM tool-calling (OpenAI/Ollama), JSON Schema validators — reject [] where an object is required (Ollama: HTTP 400 Value looks like object, but can't find closing '}' symbol).

Rules added: use stdClass for empty JSON objects; decode without associative=true when a value must round-trip as an object; normalise in the shared value object's constructor so direct-property readers and fromArray(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).

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>
Copilot AI review requested due to automatic review settings July 5, 2026 11:18
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

github-actions[bot]
github-actions Bot previously approved these changes Jul 5, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation skill labels Jul 5, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 without associative=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.

Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated
Comment thread skills/php-modernization/references/type-safety.md Outdated
…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>
@sonarqubecloud

sonarqubecloud Bot commented Jul 5, 2026

Copy link
Copy Markdown

@CybotTM
CybotTM merged commit 61180da into main Jul 5, 2026
20 checks passed
@CybotTM
CybotTM deleted the feat/json-empty-object-vs-array branch July 5, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation skill

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants